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 b524b7f001f37bc21e3204a7f616f62f0b125883
Author: hutiefang76 <[email protected]>
AuthorDate: Fri Oct 23 13:01:30 2026 +0800

    [DBCP-522] Invalidate auto-commit cache before driver calls
    
    A driver can change auto-commit before a setter or follow-up getter
    throws an Error. Clear the previous cached state before delegation so
    a later read does not return a stale value. Preserve the SQLException
    handler and the successful-path read of the actual delegate state.
    
    Add controlled-error regressions over real H2 memory connections and
    normal, SQLException and cache-disabled controls.
    
    Generated-by: DeepSeek
    Generated-by: Codex
---
 src/changes/changes.xml                            |   1 +
 .../apache/commons/dbcp2/DelegatingConnection.java |   1 +
 .../commons/dbcp2/TestDelegatingConnection.java    | 106 +++++++++++++++++++++
 3 files changed, 108 insertions(+)

diff --git a/src/changes/changes.xml b/src/changes/changes.xml
index fbf12dca..80020bb8 100644
--- a/src/changes/changes.xml
+++ b/src/changes/changes.xml
@@ -64,6 +64,7 @@ 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>
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..3c491d3b 100644
--- a/src/test/java/org/apache/commons/dbcp2/TestDelegatingConnection.java
+++ b/src/test/java/org/apache/commons/dbcp2/TestDelegatingConnection.java
@@ -374,6 +374,112 @@ 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");
+                DelegatingConnection<Connection> outer = new 
DelegatingConnection<>(physical)) {
+            assertTrue(outer.getAutoCommit());
+            outer.setAutoCommit(false);
+            assertFalse(physical.getAutoCommit());
+            assertFalse(outer.getAutoCommit());
+        }
+    }
+
     @Test
     void testSetClientInfo() throws Exception {
         // TODO

Reply via email to