如何使用JOOQ启动事务和回滚? [英] How to start transaction and rollback with JOOQ?
问题描述
是的!我已经阅读了关于
Yes! I have read the docs about
- jOOQ永远不会在连接上提交或回滚的文档($ b $除外) b CSV导入,如果在导入API中明确配置)
- jOOQ永远不会开始任何交易。
- ...
但是当我需要一些交易管理时,最好的做法是什么?
but when I need some transaction management, what is the best practice to do this?
我说过我是JOOQ方式的粉丝!
Have I said that I'm a big fan of a way of JOOQ!
推荐答案
在jOOQ尚未实现事务API的时候问过这个问题。从jOOQ 3.4开始,这样的API可用并在此处记录:
https://www.jooq.org/doc/latest/manual/sql-execution/transaction-management
默认情况下,jOOQ将其(嵌套)事务支持绑定到JDBC API直接通过简单,实用的API:
By default, jOOQ binds its (nested) transaction support to the JDBC API directly through a simple, functional API:
DSL.using(configuration)
.transaction(c -> {
c.dsl().insertInto(...).execute();
c.dsl().update(...).execute();
});
... lambda表达式(或更具体地说, TransactionalRunnable
)在其上创建一个新交易在正常完成时开始并提交,或者在例外情况下回滚。
... the lambda expression (or more specifically, the TransactionalRunnable
) creates a new transaction at its beginning and commits it upon normal completion, or rolls it back upon exception.
此类交易可以嵌套
DSL.using(configuration)
.transaction(c1 -> {
c1.dsl().insertInto(...).execute();
c1.dsl().transaction(c2 -> {
c2.dsl().insertInto(...).execute();
});
c1.dsl().update(...).execute();
});
...如果 Savepoint
将在开头创建嵌套事务和嵌套事务在正常完成时丢弃保存点,或在异常时回滚到它。
... in case of which a Savepoint
will be created at the beginning of the nested transaction and the nested transaction discards the savepoint upon normal completion, or rolls back to it upon exception.
在许多应用程序中,您已经拥有一个预先存在的事务管理系统,例如JTA或Spring TX或其他东西。在这种情况下,你可以:
In many applications, you will already have a pre-existing transaction management system, e.g. JTA or Spring TX or something else. In this case, you can either:
- 根本不使用jOOQ交易API
- 实施你自己的
TransactionProvider
实现begin()
,commit()
和的语义> rollback()
操作,例如通过将它们绑定到Spring。
- Not use the jOOQ transaction API at all
- Implement your own
TransactionProvider
which implements the semantics of thebegin()
,commit()
, androllback()
operations, e.g. by binding them to Spring.
这篇关于如何使用JOOQ启动事务和回滚?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!