我看过JOOQ 事务管理部分
它使用jooq事务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吗?
// 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,我不知道这是什么.
发布于 2020-07-07 11:19:20
使用jOOQ的隐式事务
您可以使用jOOQ的事务API来实现如下所需的功能:
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的隐式嵌套事务。如果捕捉到自己抛出的异常,则不会回滚整个事务,而只回滚嵌套的事务。
当然,可以使用类似的方法回滚整个事务。
使用jOOQ的过程事务
除了基于https://github.com/jOOQ/jOOQ/issues/5376的隐式lambda / exception之外,还有一个挂起的特性请求来添加对过程事务API的支持,类似于JDBC的支持:
目前,您无法单独使用jOOQ来实现您的目的,但是您总是可以将JDBC与jOOQ结合使用,例如(假设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,因为我从您的示例中假设这是您想要做的事情。当然,如果不是,您可以省略保存点,始终回滚整个事务。
https://stackoverflow.com/questions/62771908
复制相似问题