从 jOOQ 事务访问 JDBC 连接

Access JDBC Connection from jOOQ transaction

是否可以从 jOOQ TransactionRunnableTransactionCallable 中访问底层 JDBC 连接?

我希望能够访问您从连接获得的 PostgreSQL 大对象 API:

final LargeObjectManager manager = connection.unwrap(PGConnection.class).getLargeObjectAPI();

Afaict 你只能访问 ConnectionProvider,如果我调用 aquire(),看起来我的 LOB 不会得到与我的 LOB 相同的连接声明。

在 jOOQ 3.7 中,#4552 was implemented to allow for accessing a Configuration's Connection via a ConnectionRunnable。例如:

DSL.using(configuration)
   .transaction((Configuration c) -> {
       DSL.using(c).connection(connection -> {
           LargeObjectManager manager = connection.unwrap(PGConnection.class).getLargeObjectAPI();
           // ...
       });
   });

为了方便起见,这个API会在内部调用acquire()release(),所以下面的解决方案是等效的,但更冗长:

DSL.using(configuration)
   .transaction((Configuration c) -> {
       Connection connection = c.connectionProvider().acquire();
       try {
           LargeObjectManager manager = connection.unwrap(PGConnection.class).getLargeObjectAPI();
           // ...
       }
       finally {
           c.connectionProvider().release(connection);
       }
   });

由于Connection是从"transacted"Configuration c中得到的,所以您可以确定您将访问正确的Connection。即使您多次访问它,它也始终与 jOOQ 在内部将您自己的 ConnectionProvider 替换为 DefaultConnectionProvider 作用于已交易的 Connection

相同