This is an automated email from the ASF dual-hosted git repository. rmaucher pushed a commit to branch 9.0.x in repository https://gitbox.apache.org/repos/asf/tomcat.git
commit 8636ccc86c3d693d58d5b682af8b664be50db369 Author: opencode <[email protected]> AuthorDate: Tue Dec 1 20:48:58 2026 +0100 Fix init SQL transaction when ConnectionState owns auto-commit When ConnectionState is configured, connect() leaves the configured default auto-commit state to the borrow-time reset. The init SQL runs before the first borrow, so with defaultAutoCommit=false every statement executed in auto-commit mode and a failure in a later statement left the earlier ones committed. Apply the configured auto-commit state before executing initSQLs when a ConnectionState interceptor owns the defaults, so the statements share one transaction and the commit/rollback at the end of validation handles the whole batch. Add a regression test with ConnectionState, auto-commit off, and a failing second init statement. mvn test passes: 1178 tests, 0 failures. --- .../org/apache/tomcat/jdbc/pool/PooledConnection.java | 14 ++++++++++++++ .../org/apache/tomcat/jdbc/pool/MultipleInitSqlBase.java | 15 +++++++++++++++ 2 files changed, 29 insertions(+) diff --git a/modules/jdbc-pool/src/main/java/org/apache/tomcat/jdbc/pool/PooledConnection.java b/modules/jdbc-pool/src/main/java/org/apache/tomcat/jdbc/pool/PooledConnection.java index 612eeb9851..f11a101b31 100644 --- a/modules/jdbc-pool/src/main/java/org/apache/tomcat/jdbc/pool/PooledConnection.java +++ b/modules/jdbc-pool/src/main/java/org/apache/tomcat/jdbc/pool/PooledConnection.java @@ -1510,6 +1510,20 @@ public class PooledConnection implements PooledConnectionMBean { boolean transactionTerminated = false; Statement stmt = null; try { + if (executingInitSql && hasConnectionStateInterceptor()) { + // A ConnectionState interceptor deliberately defers the + // configured defaults to the borrow-time reset, but the init + // SQL runs before the first borrow. Apply the configured + // auto-commit state here as well, so the statements share one + // transaction and the commit/rollback below has a transaction + // to terminate even when the interceptor owns the defaults. + // Without the interceptor the connect path has already applied + // the value, and an extra driver round trip is not wanted. + Boolean defaultAutoCommit = configuration.getDefaultAutoCommit(); + if (defaultAutoCommit != null) { + connection.setAutoCommit(defaultAutoCommit.booleanValue()); + } + } stmt = connection.createStatement(); int validationQueryTimeout = configuration.getValidationQueryTimeout(); diff --git a/modules/jdbc-pool/src/test/java/org/apache/tomcat/jdbc/pool/MultipleInitSqlBase.java b/modules/jdbc-pool/src/test/java/org/apache/tomcat/jdbc/pool/MultipleInitSqlBase.java index 8db39826f9..1c673e8aaf 100644 --- a/modules/jdbc-pool/src/test/java/org/apache/tomcat/jdbc/pool/MultipleInitSqlBase.java +++ b/modules/jdbc-pool/src/test/java/org/apache/tomcat/jdbc/pool/MultipleInitSqlBase.java @@ -159,6 +159,21 @@ abstract class MultipleInitSqlBase extends JdbcPoolBaseTest { awaitSessions(0); } + @Test + void connectionStateDoesNotBreakTheInitSqlTransaction() throws Exception { + String table = createTable(); + DataSource ds = poolWithStatements(Arrays.asList( + "INSERT INTO " + table + " VALUES (1)", + "INSERT INTO NO_SUCH_TABLE VALUES (2)")); + ds.getPoolProperties().setDefaultAutoCommit(Boolean.FALSE); + ds.getPoolProperties().setJdbcInterceptors("ConnectionState"); + Assertions.assertThrows(SQLException.class, ds::createPool, + "a failed initialisation statement must fail the pool start"); + Assertions.assertEquals(0, adminRowCount(table), + "the earlier statement must be rolled back with the failed one"); + awaitSessions(0); + } + @Test void initSQLsReplaceTheValidationQueryAtConnect() throws Exception { DataSource ds = poolWithStatements(Collections.singletonList("SET @MULTI = 9")); --------------------------------------------------------------------- To unsubscribe, e-mail: [email protected] For additional commands, e-mail: [email protected]
