From 04b03c91adb1f097c4013c7e8d07b25276b3675d Mon Sep 17 00:00:00 2001 From: Leon van Zantvoort Date: Tue, 12 May 2026 23:25:01 +0200 Subject: [PATCH] Return connection to pool when commit fails. (#125) --- .../template/impl/JdbcTransactionContext.kt | 28 ++++++++++++++++--- 1 file changed, 24 insertions(+), 4 deletions(-) diff --git a/storm-kotlin/src/main/kotlin/st/orm/template/impl/JdbcTransactionContext.kt b/storm-kotlin/src/main/kotlin/st/orm/template/impl/JdbcTransactionContext.kt index 2a08ab257..1c2eb8b16 100644 --- a/storm-kotlin/src/main/kotlin/st/orm/template/impl/JdbcTransactionContext.kt +++ b/storm-kotlin/src/main/kotlin/st/orm/template/impl/JdbcTransactionContext.kt @@ -438,8 +438,18 @@ internal class JdbcTransactionContext : TransactionContext { } state.ownsConnection -> { logger.debug("Committing transaction on {} ({}).", connection, state.transactionId) - if (!connection.autoCommit) connection.commit() - close(connection, state) + try { + if (!connection.autoCommit) connection.commit() + } finally { + // Always return the connection to the pool — even if commit() threw — so the pool + // does not retain a connection with autoCommit=false (which would fail the auto-commit + // precondition on the next openNewTransaction()). + try { + close(connection, state) + } catch (closeException: SQLException) { + logger.warn("Failed to close connection after commit ({}).", state.transactionId, closeException) + } + } } else -> { // joined REQUIRED: nothing to do yet. @@ -473,8 +483,18 @@ internal class JdbcTransactionContext : TransactionContext { } state.ownsConnection && connection != null -> { logger.debug("Rolling back transaction on {} ({}).", connection, state.transactionId) - if (!connection.autoCommit) connection.rollback() - close(connection, state) + try { + if (!connection.autoCommit) connection.rollback() + } finally { + // Always return the connection to the pool — even if rollback() threw — so the pool + // does not retain a connection with autoCommit=false (which would fail the auto-commit + // precondition on the next openNewTransaction()). + try { + close(connection, state) + } catch (closeException: SQLException) { + logger.warn("Failed to close connection after rollback ({}).", state.transactionId, closeException) + } + } } else -> { // Joined REQUIRED or non-transactional scope (no connection):