jOOQ 3.4交易API
使用jOOQ 3.4,已添加事务API以通过JDBC,Spring或JTA事务管理器进行抽象。该API可以与Java
8一起使用,例如:
DSL.using(configuration) .transaction(ctx -> { DSL.using(ctx) .update(TABLE) .set(TABLE.COL, newValue) .where(...) .execute(); });或使用Java 8之前的语法
DSL.using(configuration) .transaction(new TransactionRunnable() { @Override public void run(Configuration ctx) {DSL.using(ctx) .update(TABLE) .set(TABLE.COL, newValue) .where(...) .execute(); } });这个想法是由lambda表达式(或匿名类)形成事务代码,该代码:
- 在正常完成时提交
- 发生异常时回滚
该
org.jooq.TransactionProviderSPI可用于覆盖默认行为,它通过JDBC使用实现嵌套事务
Savepoints。
春天的例子
当前文档显示了使用Spring进行事务处理时的示例:
- http://www.jooq.org/doc/latest/manual/getting-started/tutorials/jooq-with-spring/
这个例子实质上归结为使用Spring
TransactionAwareDataSourceProxy
<!-- Using Apache DBCP as a connection pooling library. Replace this with your preferred DataSource implementation --><bean id="dataSource" init-method="createDataSource" destroy-method="close"> <property name="driverClassName" value="org.h2.Driver" /> <property name="url" value="jdbc:h2:~/maven-test" /> <property name="username" value="sa" /> <property name="password" value="" /></bean><!-- Using Spring JDBC for transaction management --><bean id="transactionManager" > <property name="dataSource" ref="dataSource" /></bean><bean id="transactionAwareDataSource" > <constructor-arg ref="dataSource" /></bean><!-- Bridging Spring JDBC data sources to jOOQ's ConnectionProvider --><bean name="connectionProvider"> <constructor-arg ref="transactionAwareDataSource" /></bean>
可以从GitHub获得运行示例:
- https://github.com/jOOQ/jOOQ/tree/master/jOOQ-examples/jOOQ-spring-example
Spring and Guice示例
尽管我个人不建议这样做,但是某些用户已经成功地用Guice替换了Spring
DI的一部分,并与Guice处理了交易。在此用例上,GitHub上还有一个经过集成测试的运行示例:
- https://github.com/jOOQ/jOOQ/tree/master/jOOQ-examples/jOOQ-spring-guice-example



