This is an automated email from the ASF dual-hosted git repository.

rmaucher pushed a commit to branch 11.0.x
in repository https://gitbox.apache.org/repos/asf/tomcat.git

commit e718a9b83721ad4320a358bdb42dffb32907933d
Author: opencode <[email protected]>
AuthorDate: Tue Dec 1 23:38:47 2026 +0100

    Use one configuration generation for reconnect and connect decisions
    
    connect() captured a snapshot for URL, driver properties and default state, 
but the first-connect ConnectionState decision read the live configuration. 
reconnectWith() also captured its credential defaults from one generation and 
then called connect(), which re-read the snapshot.
    
    Make connect() delegate to a connect(PoolConfiguration) overload and pass 
the captured generation into the ConnectionState decision. reconnectWith() now 
passes its captured generation to the same overload, so one attempt cannot mix 
two live reconfigurations.
    
    mvn test passes: 1187 tests, 0 failures.
---
 .../apache/tomcat/jdbc/pool/PooledConnection.java  | 49 ++++++++++++++++------
 1 file changed, 36 insertions(+), 13 deletions(-)

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 7f0eb10626..e52b1d1eac 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
@@ -268,13 +268,27 @@ public class PooledConnection implements 
PooledConnectionMBean {
      *                          {@link 
java.sql.Connection#setReadOnly(boolean)} fails.
      */
     public void connect() throws SQLException {
+        connect(connectConfiguration());
+    }
+
+
+    /**
+     * Connects using one configuration generation. The generation decides the 
data source or driver route, the URL and
+     * driver properties, the default connection state, and the interceptor 
configuration used when no chain exists yet.
+     * Callers that must pair credentials or other decisions with the connect 
use this overload so a concurrent live
+     * reconfiguration cannot mix two generations in one attempt.
+     *
+     * @param configuration the configuration generation this connect reads
+     *
+     * @throws SQLException if the connection cannot be established
+     */
+    private void connect(PoolConfiguration configuration) throws SQLException {
         if (released.get()) {
             throw new 
SQLException(MessageFactory.get("pooledConnection.cannotReestablish"));
         }
         if (parent != null && parent.isClosed()) {
             throw new 
SQLException(MessageFactory.get("connectionPool.poolClosed"));
         }
-        PoolConfiguration configuration = connectConfiguration();
         if (connection != null || xaConnection != null || cpdsConnection != 
null) {
             try {
                 this.disconnect(false);
@@ -304,7 +318,7 @@ public class PooledConnection implements 
PooledConnectionMBean {
             }
 
             // Set up the default state, unless we expect the interceptor to 
do it
-            if (!hasConnectionStateInterceptor()) {
+            if (!hasConnectionStateInterceptor(configuration)) {
                 if (configuration.getDefaultTransactionIsolation() != 
DataSourceFactory.UNKNOWN_TRANSACTIONISOLATION) {
                     
connection.setTransactionIsolation(configuration.getDefaultTransactionIsolation());
                 }
@@ -411,13 +425,22 @@ public class PooledConnection implements 
PooledConnectionMBean {
      * @return <code>true</code> if a {@link ConnectionState} interceptor is 
configured
      */
     private boolean hasConnectionStateInterceptor() {
-        // Once a chain has been built, the connection keeps it for its whole
-        // life; a live jdbcInterceptors change only affects chains built
-        // later. The decision must therefore be made on the chain this
-        // connection actually runs, not on the current configuration, or a
-        // connection with a cached chain that lacks ConnectionState would
-        // skip the defaults at reconnect because the configuration now lists
-        // the interceptor that the cached chain does not contain.
+        return hasConnectionStateInterceptor(configuration());
+    }
+
+
+    /**
+     * Whether the given configuration generation supplies a {@link 
ConnectionState} interceptor for a connection whose
+     * chain has not been built yet, or the cached chain itself contains one. 
The cached chain is authoritative once it
+     * exists: a live interceptor change only affects chains built later, so a 
connection with a cached chain that lacks
+     * ConnectionState must keep applying the defaults at reconnect even when 
the current configuration now lists the
+     * interceptor.
+     *
+     * @param configuration the configuration generation this decision reads
+     *
+     * @return {@code true} when a {@link ConnectionState} interceptor is 
configured
+     */
+    private boolean hasConnectionStateInterceptor(PoolConfiguration 
configuration) {
         JdbcInterceptor interceptor = handler;
         if (interceptor != null) {
             while (interceptor != null) {
@@ -428,9 +451,9 @@ public class PooledConnection implements 
PooledConnectionMBean {
             }
             return false;
         }
-        // A connection with no chain yet will build one from the current
-        // configuration during setup.
-        for (PoolProperties.InterceptorDefinition definition : 
configuration().getJdbcInterceptorsAsArray()) {
+        // A connection with no chain yet will build one from this
+        // configuration generation during setup.
+        for (PoolProperties.InterceptorDefinition definition : 
configuration.getJdbcInterceptorsAsArray()) {
             try {
                 if 
(ConnectionState.class.isAssignableFrom(definition.getInterceptorClass())) {
                     return true;
@@ -932,7 +955,7 @@ public class PooledConnection implements 
PooledConnectionMBean {
         }
 
         try {
-            connect();
+            connect(configuration);
         } catch (Throwable x) {
             // A connect can fail after it established the session - the
             // default state it applies to a new session can be refused, or a


---------------------------------------------------------------------
To unsubscribe, e-mail: [email protected]
For additional commands, e-mail: [email protected]

Reply via email to