JOOQ&交易 [英] JOOQ & Transaction

查看:65
本文介绍了JOOQ&交易的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我看过JOOQ 交易管理部分
它是开始交易,请使用jooq交易api,

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

create.transaction(configuration -> {

    // Wrap configuration in a new DSLContext:
    DSL.using(configuration).insertInto(...);
    DSL.using(configuration).insertInto(...);
    
    // Or, reuse the new DSLContext within the transaction scope:
    DSLContext ctx = DSL.using(configuration);
    ctx.insertInto(...);
    ctx.insertInto(...);
    
    // ... but avoid using the scope from outside the transaction:
    create.insertInto(...);
    create.insertInto(...);
});

回滚是隐式的.
但是我想自己提交和回滚. 我的项目不是基于Spring和Spring引导. JOOQ有这样的api吗?

It's rollback is implicit.
But I want to submit and rollback by myself. My project isn't base on Spring and Spring boot. Is JOOQ has some api like this?

// start transaction
    api.begin()
// will do handle like this many times
    dslContext.update(...)
    Object obj = dslContext.select(...)
    if (obj == null) {
        api.rollback;
    }
    dslContext.insert(...)
    Object obj1 = dslContext.select(...)
    if (obj1 == null) {
        api.rollback;
    }
    ...
    ...
// finally, if no problem:
    api.commit

本节告诉我可以实现您自己的org.jooq.TransactionProvider并将其提供给您的配置,以覆盖jOOQ的默认行为.
此接口方法的参数是TransactionContext,我不知道这是什么...

this section tell I can implement your own org.jooq.TransactionProvider and supply that to your Configuration to override jOOQ's default behaviour.
this interface method params is TransactionContext, I dont known what's this...

推荐答案

使用jOOQ隐式处理事务

您可以使用jOOQ的事务API来实现您想要的目标:

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) { ... }
});

上面的示例在创建嵌套的transaction()时使用了使用JDBC保存点的隐式嵌套事务.如果发现抛出自己的异常,则不会回滚整个事务,而只会回滚嵌套的事务.

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.

除了您已经看到的基于隐式lambda/exception的请求之外,还有一个待处理的功能请求,以增加对过程事务API的支持,类似于JDBC的过程. https://github.com/jOOQ/jOOQ/issues/5376

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

当前,您不能仅凭jOOQ来完成您想做的事情,但是您始终可以在jOOQ上使用JDBC,例如(假设autoCommit = false):

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();
});

注意,我正在使用JDBC Savepoint,因为我从您的示例中假设这就是您想要做的.当然,如果不是,则可以省略保存点,并始终回滚整个事务.

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.

这篇关于JOOQ&交易的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆