JOOQ事务中使用底层JDBC Connection,操作是否纳入同一事务?
Question 1: Does transaction state persist when using the underlying Connection directly in jOOQ?
Absolutely. When you’re working within a jOOQ transaction context (like using dsl.transaction {}), the underlying JDBC Connection that jOOQ uses is already enrolled in the transaction. If you retrieve this Connection directly (via methods like DSLContext.connection() or Configuration.connection()), it retains the transaction state of the jOOQ context. Any operations you perform on this Connection will be part of the same transaction—committing or rolling back alongside jOOQ’s own queries.
Question 2: Will CopyManager operations on the Connection be part of the same transaction?
Yes, in your provided Kotlin example, the CopyManager operations are fully included in the jOOQ transaction. Here’s a breakdown of why this works:
- The
dsl.transaction {}lambda initializes a transaction using jOOQ’s default transaction provider, which binds a single JDBC Connection to the transaction context. transactionalDSL = DSL.using(transactionConfig)creates a newDSLContextthat inherits the transactional Connection from the providedtransactionConfig.- Calling
transactionalDSL.connection { connection -> ... }gives you direct access to that same transactional Connection. Casting it toBaseConnection(Postgres’s underlying driver implementation) is safe here because it’s the actual connection jOOQ is using for the transaction.
All data changes made via CopyManager.copyInto() will follow the transaction’s fate: if the transaction completes without exceptions, both jOOQ queries and CopyManager operations commit together. If an exception is thrown, everything rolls back as a single unit.
A critical note: This behavior relies on jOOQ’s default transaction management, which leverages the JDBC Connection’s native transaction support. As long as you’re using the Connection provided by the transactional DSLContext, you’re operating within the same transaction boundary.
内容的提问来源于stack exchange,提问作者Martin Eden

