JOOQ & Transaction

前端 未结 1 1480
我在风中等你
我在风中等你 2021-01-27 06:16

I have seen JOOQ Transaction Management section
it\'s start transaction use jooq transaction api,

create.transaction(configuration -> {

    // Wrap conf         


        
1条回答
  •  鱼传尺愫
    2021-01-27 06:39

    Implicit transactions with jOOQ

    You can use jOOQ's transaction API to achieve what you want to do like this:

    create.transaction(c1 -> {
        try {
            c1.dsl().transaction(c2 -> {
                c2.dsl().update(...);
                Object obj = c2.dsl().select(...);
                if (obj == null)
                    throw new MyRollbackException();
            });
        }
        catch (MyRollbackException e) { ... }
    
        try {
            c1.dsl().transaction(c2 -> {
                c2.dsl().insert(...);
                Object obj = c2.dsl().select(...);
                if (obj == null)
                    throw new MyRollbackException();
            });
        }
        catch (MyRollbackException e) { ... }
    });
    

    The above example is using implicit nested transaction using JDBC savepoints, whenever you create a nested transaction(). If you catch the exception that you're throwing yourself, then you won't roll back the entire transaction, but just the nested one.

    Of course, it's possible to use a similar approach to roll back the entire transaction.

    Procedural transactions with jOOQ

    There's a pending feature request to add support for a procedural transaction API, similar to that of JDBC, in addition to the implicit lambda / exception based one that you've seen: https://github.com/jOOQ/jOOQ/issues/5376

    Currently, you can't do what you intended with jOOQ alone, but you can always resort to using JDBC with jOOQ, e.g. (assuming autoCommit = false):

    // Obtain a JDBC connection from jOOQ (alternatively, use your own)
    dslContext.connection(con -> {
    
        // Depending on your DataSource, make sure you're always using the same JDBC connection
        DSLContext ctx = DSL.using(con);
    
    // start transaction
        // This isn't needed with JDBC, as a Connection always implicitly starts transactions
        // when autoCommit = false
    
    // will do handle like this many times
        Savepoint sp1 = con.setSavepoint();
        ctx.update(...)
        Object obj1 = ctx.select(...)
        if (obj1 == null) {
            con.rollback(sp1);
        }
        Savepoint sp2 = con.setSavepoint();
        ctx.insert(...)
        Object obj2 = ctx.select(...)
        if (obj2 == null) {
            con.rollback(sp2);
        }
        ...
        ...
    // finally, if no problem:
        con.commit();
    });
    

    Notice, I'm using JDBC Savepoint because I'm assuming from your example that this is what you want to do. If it's not, of course, you can just omit the savepoints and always roll back the entire transaction.

    0 讨论(0)
提交回复
热议问题