ThreadLocalTransactionProvider permanently poisons a thread when nested transaction rollback throws on a broken connection
Expected behavior
When a database connection dies while a transaction is in flight (e.g. a failover or network blip) and the subsequent rollback itself throws, ThreadLocalTransactionProvider should still clean up its thread-local state, so the next transaction started on that thread acquires a fresh connection from the ConnectionProvider and succeeds.
This is what happens for top-level transactions: DefaultTransactionProvider.brace(ctx, false) releases the connection and removes it from the configuration data map even when rollback throws, and the thread recovers.
Actual behavior
If a nested (savepoint) transaction is in flight when the connection dies, the thread is poisoned permanently: every subsequent transaction started on that thread — even days later, long after the database has recovered — fails with:
org.jooq.exception.DataAccessException: Cannot set autoCommit
at org.jooq.impl.DefaultConnectionProvider.setAutoCommit(DefaultConnectionProvider.java:207)
at org.jooq.impl.DefaultTransactionProvider.brace(DefaultTransactionProvider.java:226)
at org.jooq.impl.DefaultTransactionProvider.begin(DefaultTransactionProvider.java:145)
at org.jooq.impl.ThreadLocalTransactionProvider.begin(ThreadLocalTransactionProvider.java:93)
...
Caused by: java.sql.SQLException: Connection is closedThe thread never calls ConnectionProvider.acquire() again; only a JVM restart recovers it. In our production incident (Spring Boot + HikariCP + PostgreSQL behind a Tomcat thread pool), one worker thread per instance stayed poisoned for 3 days, failing thousands of requests that happened to land on it. The pool had correctly evicted and closed the connection; jOOQ retained the closed proxy.
Cause, from reading the 3.20.6 sources (the relevant code is unchanged on current main):
- When the inner (savepoint) rollback throws (rollback-to-savepoint on a dead connection), DefaultTransactionProvider.rollback performs no connection cleanup for non-top-level scopes — brace(ctx, false) only runs when the savepoint stack is empty — so the inner transaction's Configuration still holds the dead connection wrapper under DATA_DEFAULT_TRANSACTION_PROVIDER_CONNECTION.
- In ThreadLocalTransactionProvider.rollback() (and commit()), configurations().pop() runs after the delegate call, not in a finally — so when the delegate throws, that poisoned Configuration is leaked permanently on the thread's localConfigurations deque.
- Every later ContextTransactional* call on the thread seeds from configuration(fallback) → peek() → the leaked Configuration. Its derived data map still contains the dead connection wrapper (and cached autoCommit = true), so begin() → brace(true) → setAutoCommit(false) fails on the closed connection. That failure's rollback also throws, pop() is skipped again — the state is self-sustaining.
Note: commit() intentionally skips pop() on failure because rollback() is expected to do it (per the #17517 comment) — but rollback() also skips it when the delegate throws, which is the gap.
Steps to reproduce the problem
No code generation or DDL involved; reproduces with plain SQL on any table-less queries.
On a DSLContext configured with ThreadLocalTransactionProvider over a pooled PostgreSQL DataSource (HikariCP), using a single-thread executor to pin the thread:
- Submit to the executor:
ctx.transaction(() -> { // outer
ctx.transaction(() -> { // inner → savepoint
// read pg_backend_pid() and hand it to the main thread, then block
// until the main thread has killed the backend
...
ctx.fetch("select 1"); // fails: connection is dead
});
});- From the main thread (different connection):
select pg_terminate_backend(<pid from step 1>) - The inner statement fails; the nested and outer rollbacks fail too (dead connection).
- Submit any new transaction to the same executor thread.
Expected: step 4 succeeds on a fresh connection. Actual: step 4 — and every retry on that thread, forever — fails with "Cannot set autoCommit" / "Connection is closed".
Control: killing the connection in a top-level-only transaction does NOT reproduce the issue (the thread recovers), which isolates the problem to the nested/savepoint case.
jOOQ Version
jOOQ Open Source Edition 3.20.6 (relevant code verified unchanged on current main)
Database product and version
PostgreSQL 17.9 on x86_64-pc-linux-gnu, compiled by Debian clang version 12.0.1, 64-bit
Java Version
openjdk version "21" (Eclipse Temurin)
JDBC / R2DBC driver name and version (include name if unofficial driver)
org.postgresql:postgresql:42.7.7
Source: jOOQ/jOOQ