This is an automated email from the ASF dual-hosted git repository. garydgregory pushed a commit to branch master in repository https://gitbox.apache.org/repos/asf/commons-dbcp.git
commit c072890b200a9ffbd665397a34d9113a96b6a591 Author: Gary Gregory <[email protected]> AuthorDate: Sun Nov 22 10:25:31 2026 -0500 [DBCP-522] Invalidate auto-commit cache before driver calls Clear cached auto-commit before delegation so unchecked failures from the setter or follow-up getter cannot leave stale state. Preserve SQLException handling and cache the actual driver state on success. Add regression tests for failure paths, successful cache reuse, and rollback and auto-commit restoration when returning pooled connections. --- src/changes/changes.xml | 2 +- .../apache/commons/dbcp2/DelegatingConnection.java | 1 + .../commons/dbcp2/TestDelegatingConnection.java | 126 +++++++++++++++++++++ .../commons/dbcp2/TestPoolableConnection.java | 58 ++++++++++ 4 files changed, 186 insertions(+), 1 deletion(-) diff --git a/src/changes/changes.xml b/src/changes/changes.xml index 7d9425bc..357ef2c8 100644 --- a/src/changes/changes.xml +++ b/src/changes/changes.xml @@ -64,7 +64,6 @@ The <action> type attribute can be add,update,fix,remove. <body> <release version="2.15.0" date="YYYY-MM-DD" description="This is a minor release, including bug fixes and enhancements."> <!-- FIX --> - <action type="fix" issue="DBCP-522">Invalidate the cached auto-commit state before delegating setAutoCommit to the underlying connection, so a failed state update does not leave a stale cached value.</action> <action type="fix" dev="ggregory" due-to="Gary Gregory, Mark Thomas">Handle SQLException on connection initialization with SQL statements (#540).</action> <action type="fix" dev="ggregory" due-to="Gary Gregory, Stamatis Zampetakis">Check configured JDBC driver classes implement java.sql.Driver before initializing or instantiating them.</action> <action type="fix" dev="ggregory" due-to="Gary Gregory">Report SQLState 08001 for driver loading and creation failures, preserving driver-supplied SQLStates and vendor codes.</action> @@ -76,6 +75,7 @@ The <action> type attribute can be add,update,fix,remove. <action type="fix" dev="ggregory" due-to="Gary Gregory">Expose only DataSourceMXBean through DynamicMBean when BasicDataSource is registered directly with JMX. Retain the Java password accessor and MXBean reference mapping for compatibility. Direct registrations resolve empty domains and perform cleanup using their owning MBeanServer. Only one direct registration may be active at a time; use separate StandardMBean wrappers explicitly selecting DataSourceMXBean for multiple registra [...] <action type="fix" dev="ggregory" due-to="Gary Gregory">Extend Utils.cloneWithoutCredentials(Properties) to omit java.naming.security.principal and java.naming.security.credentials in addition to user and password. Use the shared filter for SharedPoolDataSource, PerUserPoolDataSource, DriverAdapterCPDS, and DriverConnectionFactory diagnostic strings without changing configured properties (f006).</action> <action type="fix" dev="ggregory" due-to="Gary Gregory">Omit unknown and obsolete JNDI property values from BasicDataSourceFactory diagnostics to avoid logging credentials supplied under incorrect property names (f008).</action> + <action type="fix" dev="ggregory" due-to="Bill Mitchell, Tiefang Hu, Gary Gregory" issue="DBCP-522">Prevent stale cached auto-commit state after unchecked failures in DelegatingConnection.setAutoCommit(), preserving rollback and auto-commit restoration when pooled connections are returned #549.</action> <!-- ADD --> <action type="add" dev="ggregory" due-to="Gary Gregory">Add DriverAdapterCPDS.exportPasswordAsRefAddr to optionally omit the password address from JNDI References. Password export remains enabled by default for compatibility; the setting survives JNDI lookup and Java serialization.</action> <action type="add" dev="ggregory" due-to="Gary Gregory">Add PoolingDriver.Builder and PoolingDriver.builder() to configure delegate access when creating drivers, preserving the existing permissive default.</action> diff --git a/src/main/java/org/apache/commons/dbcp2/DelegatingConnection.java b/src/main/java/org/apache/commons/dbcp2/DelegatingConnection.java index 5aee5ace..2567f3f1 100644 --- a/src/main/java/org/apache/commons/dbcp2/DelegatingConnection.java +++ b/src/main/java/org/apache/commons/dbcp2/DelegatingConnection.java @@ -856,6 +856,7 @@ public class DelegatingConnection<C extends Connection> extends AbandonedTrace i @Override public void setAutoCommit(final boolean autoCommit) throws SQLException { checkOpen(); + cachedAutoCommit = null; try { connection.setAutoCommit(autoCommit); if (cacheState) { diff --git a/src/test/java/org/apache/commons/dbcp2/TestDelegatingConnection.java b/src/test/java/org/apache/commons/dbcp2/TestDelegatingConnection.java index c2eea4ee..9a989e9f 100644 --- a/src/test/java/org/apache/commons/dbcp2/TestDelegatingConnection.java +++ b/src/test/java/org/apache/commons/dbcp2/TestDelegatingConnection.java @@ -30,6 +30,7 @@ import java.sql.Connection; import java.sql.DriverManager; import java.sql.SQLException; import java.sql.Savepoint; +import java.util.concurrent.atomic.AtomicInteger; import org.apache.commons.pool2.impl.GenericKeyedObjectPool; import org.junit.jupiter.api.AfterEach; @@ -374,6 +375,131 @@ public class TestDelegatingConnection { } } + @Test + void testSetAutoCommitErrorAfterPhysicalChange() throws SQLException { + try (Connection physical = DriverManager.getConnection("jdbc:h2:mem:dcp522_autocommit_error")) { + physical.setAutoCommit(true); + final AssertionError failure = new AssertionError("setAutoCommit failed"); + final Connection faulty = new DelegatingConnection<Connection>(physical) { + @Override + public void setAutoCommit(final boolean autoCommit) throws SQLException { + super.setAutoCommit(autoCommit); + throw failure; + } + }; + try (DelegatingConnection<Connection> outer = new DelegatingConnection<>(faulty)) { + assertTrue(outer.getAutoCommit()); + final AssertionError thrown = assertThrows(AssertionError.class, () -> outer.setAutoCommit(false)); + assertSame(failure, thrown); + assertFalse(physical.getAutoCommit()); + assertFalse(outer.getAutoCommit()); + } + } + } + + @Test + void testSetAutoCommitErrorFromDelegateGetter() throws SQLException { + try (Connection physical = DriverManager.getConnection("jdbc:h2:mem:dcp522_autocommit_getter_error")) { + physical.setAutoCommit(true); + final AssertionError failure = new AssertionError("getAutoCommit failed"); + final Connection faulty = new DelegatingConnection<Connection>(physical) { + private boolean failedOnce; + + @Override + public boolean getAutoCommit() throws SQLException { + if (!failedOnce && !physical.getAutoCommit()) { + failedOnce = true; + throw failure; + } + return physical.getAutoCommit(); + } + }; + try (DelegatingConnection<Connection> outer = new DelegatingConnection<>(faulty)) { + assertTrue(outer.getAutoCommit()); + final AssertionError thrown = assertThrows(AssertionError.class, () -> outer.setAutoCommit(false)); + assertSame(failure, thrown); + assertFalse(physical.getAutoCommit()); + assertFalse(outer.getAutoCommit()); + } + } + } + + @Test + void testSetAutoCommitErrorWithCacheStateDisabled() throws SQLException { + try (Connection physical = DriverManager.getConnection("jdbc:h2:mem:dcp522_autocommit_no_cache")) { + physical.setAutoCommit(true); + final AssertionError failure = new AssertionError("setAutoCommit failed"); + final DelegatingConnection<Connection> faulty = new DelegatingConnection<Connection>(physical) { + @Override + public void setAutoCommit(final boolean autoCommit) throws SQLException { + super.setAutoCommit(autoCommit); + throw failure; + } + }; + faulty.setCacheState(false); + try (DelegatingConnection<DelegatingConnection<Connection>> outer = new DelegatingConnection<>(faulty)) { + outer.setCacheState(false); + assertTrue(outer.getAutoCommit()); + final AssertionError thrown = assertThrows(AssertionError.class, () -> outer.setAutoCommit(false)); + assertSame(failure, thrown); + assertFalse(physical.getAutoCommit()); + assertFalse(outer.getAutoCommit()); + } + } + } + + @Test + void testSetAutoCommitSqlExceptionAfterPhysicalChange() throws SQLException { + try (Connection physical = DriverManager.getConnection("jdbc:h2:mem:dcp522_autocommit_sql_exception")) { + physical.setAutoCommit(true); + final SQLException failure = new SQLException("setAutoCommit failed"); + final Connection faulty = new DelegatingConnection<Connection>(physical) { + @Override + public void setAutoCommit(final boolean autoCommit) throws SQLException { + super.setAutoCommit(autoCommit); + throw failure; + } + }; + try (DelegatingConnection<Connection> outer = new DelegatingConnection<>(faulty)) { + assertTrue(outer.getAutoCommit()); + final SQLException thrown = assertThrows(SQLException.class, () -> outer.setAutoCommit(false)); + assertSame(failure, thrown); + assertFalse(physical.getAutoCommit()); + assertFalse(outer.getAutoCommit()); + } + } + } + + @Test + void testSetAutoCommitSuccessUpdatesCache() throws SQLException { + try (Connection physical = DriverManager.getConnection("jdbc:h2:mem:dcp522_autocommit_success")) { + final AtomicInteger getAutoCommitCalls = new AtomicInteger(); + final Connection counting = new DelegatingConnection<Connection>(physical) { + @Override + public boolean getAutoCommit() throws SQLException { + getAutoCommitCalls.incrementAndGet(); + return physical.getAutoCommit(); + } + }; + try (DelegatingConnection<Connection> outer = new DelegatingConnection<>(counting)) { + assertTrue(outer.getAutoCommit()); + assertEquals(1, getAutoCommitCalls.get()); + + outer.setAutoCommit(false); + assertEquals(2, getAutoCommitCalls.get()); + assertFalse(physical.getAutoCommit()); + assertFalse(outer.getAutoCommit()); + assertEquals(2, getAutoCommitCalls.get(), "The getter should use the state cached by the setter"); + + outer.setAutoCommit(true); + assertEquals(3, getAutoCommitCalls.get()); + assertTrue(physical.getAutoCommit()); + assertTrue(outer.getAutoCommit()); + assertEquals(3, getAutoCommitCalls.get(), "The getter should use the updated cached state"); + } + } + } + @Test void testSetClientInfo() throws Exception { // TODO diff --git a/src/test/java/org/apache/commons/dbcp2/TestPoolableConnection.java b/src/test/java/org/apache/commons/dbcp2/TestPoolableConnection.java index 183d2cdc..a30dc95d 100644 --- a/src/test/java/org/apache/commons/dbcp2/TestPoolableConnection.java +++ b/src/test/java/org/apache/commons/dbcp2/TestPoolableConnection.java @@ -19,11 +19,15 @@ package org.apache.commons.dbcp2; import static org.junit.jupiter.api.Assertions.assertEquals; import static org.junit.jupiter.api.Assertions.assertFalse; import static org.junit.jupiter.api.Assertions.assertNotNull; +import static org.junit.jupiter.api.Assertions.assertSame; import static org.junit.jupiter.api.Assertions.assertThrows; import static org.junit.jupiter.api.Assertions.assertTrue; import java.sql.Connection; +import java.sql.DriverManager; +import java.sql.ResultSet; import java.sql.SQLException; +import java.sql.Statement; import java.util.ArrayList; import java.util.Arrays; @@ -213,4 +217,58 @@ public class TestPoolableConnection { assertEquals(0, pool.getNumActive(), "The pool should have no active connections"); } + + @Test + void testReturnAfterAutoCommitErrorRollsBackAndRestoresState() throws Exception { + try (Connection physical = DriverManager.getConnection("jdbc:h2:mem:dbcp522_pool_return")) { + try (Statement statement = physical.createStatement()) { + statement.execute("CREATE TABLE items (id INT)"); + } + final AssertionError failure = new AssertionError("setAutoCommit failed"); + final Connection faulty = new DelegatingConnection<Connection>(physical) { + private boolean failedOnce; + + @Override + public void setAutoCommit(final boolean autoCommit) throws SQLException { + super.setAutoCommit(autoCommit); + if (!autoCommit && !failedOnce) { + failedOnce = true; + throw failure; + } + } + }; + final PoolableConnectionFactory factory = new PoolableConnectionFactory(() -> faulty, null); + factory.setCacheState(true); + factory.setDefaultAutoCommit(Boolean.TRUE); + factory.setRollbackOnReturn(true); + factory.setAutoCommitOnReturn(true); + try (GenericObjectPool<PoolableConnection> connectionPool = new GenericObjectPool<>(factory)) { + factory.setPool(connectionPool); + connectionPool.setMaxTotal(1); + final PoolableConnection first = connectionPool.borrowObject(); + try (Connection connection = first) { + assertTrue(connection.getAutoCommit()); + assertSame(failure, assertThrows(AssertionError.class, () -> connection.setAutoCommit(false))); + assertFalse(physical.getAutoCommit()); + // DBCP-522: An application may catch the Error and continue using the connection. + try (Statement statement = connection.createStatement()) { + statement.executeUpdate("INSERT INTO items VALUES (1)"); + } + } + + assertEquals(0, connectionPool.getNumActive()); + assertEquals(1, connectionPool.getNumIdle()); + assertTrue(physical.getAutoCommit(), "Returning the connection should restore auto-commit"); + try (PoolableConnection second = connectionPool.borrowObject()) { + assertSame(first, second, "The test must reuse the same pooled connection"); + assertTrue(second.getAutoCommit()); + try (Statement statement = second.createStatement(); ResultSet resultSet = statement.executeQuery("SELECT COUNT(*) FROM items")) { + assertTrue(resultSet.next()); + assertEquals(0, resultSet.getInt(1), "Returning the connection should roll back the previous borrower's transaction"); + } + } + } + } + } + }
