[英]JOOQ & transactions
我一直在閱讀關於交易和jooq但我很難看到如何在實踐中實現它。
假設我提供了一個自定義ConnectionProvider
JOOQ,它恰好使用自動提交設置為false的連接池。
實施大致如下:
@Override public Connection acquire() throws DataAccessException {
return pool.getConnection();
}
@Override public void release(Connection connection) throws DataAccessException {
connection.commit();
connection.close();
}
如何將兩個jooq查詢包裝到單個事務中?
使用DefaultConnectionProvider很容易,因為只有一個連接 - 但是對於一個池我不知道如何去做。
使用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();
});
或者使用pre-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.TransactionProvider
SPI可用於覆蓋默認行為,該行為使用Savepoints
通過JDBC實現可嵌套事務。
當前文檔顯示了使用Spring進行事務處理時的示例:
這個例子基本上歸結為使用Spring TransactionAwareDataSourceProxy
<!-- Using Apache DBCP as a connection pooling library.
Replace this with your preferred DataSource implementation -->
<bean id="dataSource" class="org.apache.commons.dbcp.BasicDataSource"
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"
class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
<property name="dataSource" ref="dataSource" />
</bean>
<bean id="transactionAwareDataSource"
class="org.springframework.jdbc.datasource.TransactionAwareDataSourceProxy">
<constructor-arg ref="dataSource" />
</bean>
<!-- Bridging Spring JDBC data sources to jOOQ's ConnectionProvider -->
<bean class="org.jooq.impl.DataSourceConnectionProvider"
name="connectionProvider">
<constructor-arg ref="transactionAwareDataSource" />
</bean>
GitHub提供了一個運行示例:
雖然我個人不會推薦它,但有些用戶已經成功用Guice取代Spring的DI部分並處理與Guice的交易。 在GitHub上還有針對此用例的集成測試運行示例:
這可能不是最好的方式,但似乎有效。 需要注意的是,它不是release
而是關閉連接並將其返回到池的commit
方法,這非常令人困惑,如果某些代碼“忘記”提交,可能會導致問題...
所以客戶端代碼看起來像:
final PostgresConnectionProvider postgres =
new PostgresConnectionProvider("localhost", 5432, params.getDbName(), params.getUser(), params.getPass())
private static DSLContext sql = DSL.using(postgres, SQLDialect.POSTGRES, settings);
//execute some statements here
sql.execute(...);
//and don't forget to commit or the connection will not be returned to the pool
PostgresConnectionProvider p = (PostgresConnectionProvider) sql.configuration().connectionProvider();
p.commit();
和ConnectionProvider:
public class PostgresConnectionProvider implements ConnectionProvider {
private static final Logger LOG = LoggerFactory.getLogger(PostgresConnectionProvider.class);
private final ThreadLocal<Connection> connections = new ThreadLocal<>();
private final BoneCP pool;
public PostgresConnectionProvider(String serverName, int port, String schema, String user, String password) throws SQLException {
this.pool = new ConnectionPool(getConnectionString(serverName, port, schema), user, password).pool;
}
private String getConnectionString(String serverName, int port, String schema) {
return "jdbc:postgresql://" + serverName + ":" + port + "/" + schema;
}
public void close() {
pool.shutdown();
}
public void commit() {
LOG.debug("Committing transaction in {}", Thread.currentThread());
try {
Connection connection = connections.get();
if (connection != null) {
connection.commit();
connection.close();
connections.set(null);
}
} catch (SQLException ex) {
throw new DataAccessException("Could not commit transaction in postgres pool", ex);
}
}
@Override
public Connection acquire() throws DataAccessException {
LOG.debug("Acquiring connection in {}", Thread.currentThread());
try {
Connection connection = connections.get();
if (connection == null) {
connection = pool.getConnection();
connection.setAutoCommit(false);
connections.set(connection);
}
return connection;
} catch (SQLException ex) {
throw new DataAccessException("Can't acquire connection from postgres pool", ex);
}
}
@Override
//no-op => the connection won't be released until it is commited
public void release(Connection connection) throws DataAccessException {
LOG.debug("Releasing connection in {}", Thread.currentThread());
}
}
最簡單的方法,(我發現)使用jOOQ的Spring Transactions,這里給出: http ://blog.liftoffllc.in/2014/06/jooq-and-transactions.html
基本上我們實現了一個ConnectionProvider
,它使用org.springframework.jdbc.datasource.DataSourceUtils.doGetConnection(ds)
方法來查找並返回保存Spring創建的事務的數據庫連接。
為您的DataSource
創建一個TransactionManager
bean,如下所示:
<bean
id="dataSource"
class="org.apache.tomcat.jdbc.pool.DataSource"
destroy-method="close"
p:driverClassName="com.mysql.jdbc.Driver"
p:url="mysql://locahost:3306/db_name"
p:username="root"
p:password="root"
p:initialSize="2"
p:maxActive="10"
p:maxIdle="5"
p:minIdle="2"
p:testOnBorrow="true"
p:validationQuery="/* ping */ SELECT 1"
/>
<!-- Configure the PlatformTransactionManager bean -->
<bean
id="transactionManager"
class="org.springframework.jdbc.datasource.DataSourceTransactionManager"
p:dataSource-ref="dataSource"
/>
<!-- Scan for the Transactional annotation -->
<tx:annotation-driven/>
現在,您可以注釋使用jOOQ的DSLContext
所有類或方法
@Transactional(rollbackFor = Exception.class)
在創建DSLContext
對象時,jOOQ將使用Spring創建的事務。
雖然這是一個老問題,但請查看此鏈接以幫助配置JOOQ以使用spring提供的事務管理器。 您的數據源和DSLContext必須了解Transacation。
https://www.baeldung.com/jooq-with-spring
你可能不得不改變
@Bean
public DefaultDSLContext dsl() {
return new DefaultDSLContext(configuration());
}
至
@Bean
public DSLContext dsl() {
return new DefaultDSLContext(configuration());
}
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.