This is an automated email from the ASF dual-hosted git repository. rmaucher pushed a commit to branch main in repository https://gitbox.apache.org/repos/asf/tomcat.git
commit 63bfeb3d867e333da6b64717e571428ec6efc16b Author: opencode <[email protected]> AuthorDate: Tue Dec 1 21:03:44 2026 +0100 Bound chain definitions by distinct configuration The live configuration view deep-copies interceptor definitions for every chain, and ConnectionPool recorded each copy in an identity-based set. A long-lived pool therefore retained one definition and its cached interceptor class for every pooled connection ever built. Record an immutable configuration key holding the class name and property values in a deduplicated set instead, and reconstruct a definition only when close delivers poolClosed. Add regression tests asserting the record does not grow with the number of connections and that a removed definition receives one poolClosed callback across several connections. mvn test passes: 1180 tests, 0 failures. --- .../apache/tomcat/jdbc/pool/ConnectionPool.java | 76 ++++++++++++++++++---- .../InterceptorRemovalBeforePoolCloseBase.java | 67 +++++++++++++++++++ 2 files changed, 132 insertions(+), 11 deletions(-) diff --git a/modules/jdbc-pool/src/main/java/org/apache/tomcat/jdbc/pool/ConnectionPool.java b/modules/jdbc-pool/src/main/java/org/apache/tomcat/jdbc/pool/ConnectionPool.java index 15b0291da6..ec171cbdd5 100644 --- a/modules/jdbc-pool/src/main/java/org/apache/tomcat/jdbc/pool/ConnectionPool.java +++ b/modules/jdbc-pool/src/main/java/org/apache/tomcat/jdbc/pool/ConnectionPool.java @@ -26,6 +26,7 @@ import java.util.ArrayList; import java.util.Arrays; import java.util.Collections; import java.util.ConcurrentModificationException; +import java.util.HashMap; import java.util.HashSet; import java.util.Iterator; import java.util.List; @@ -173,17 +174,21 @@ public class ConnectionPool { private volatile PoolProperties.InterceptorDefinition[] startedInterceptors; /** - * The interceptor definitions a connection chain of this pool was actually built from. The close pairs with + * The configurations a connection chain of this pool was actually built from. The close pairs with * {@link #startedInterceptors} and with the definitions the {@code jdbcInterceptors} property lists at close * time, but neither of those covers a definition the property gained after the start and lost again before the * close: it never saw the start to be recorded there, and by the close it is gone from the list the close reads * - yet a chain was built from it, and what a borrow through that chain registered under the pool's name (the * statistics and, for the reporting interceptors, the published MBean) is only ever taken down by a close. - * Recording the definitions at chain construction is what lets the close find that third set. Entries are the - * small configuration objects the property parses, one per definition per reconfiguration of a live pool; a - * restarted pool is a new instance pairing over its own generation. + * Recording the configurations at chain construction is what lets the close find that third set. + * <p> + * An entry is a configuration key, not one definition object per chain: the live configuration view deep-copies + * the parsed definitions for every chain, so storing the objects themselves would retain one copy - and its cached + * interceptor class - for every pooled connection ever built. The key holds only the class name and the property + * values, deduplicates equal configurations, and is turned back into a definition when the close needs to deliver + * the notification. A restarted pool is a new instance pairing over its own generation. */ - private final Set<PoolProperties.InterceptorDefinition> chainDefinitions = + private final Set<ChainDefinitionKey> chainDefinitions = ConcurrentHashMap.newKeySet(); /** @@ -601,10 +606,10 @@ public class ConnectionPool { throw sx; } for (int i = proxies.length - 1; i >= 0; i--) { - // Record what this chain was built from: the definitions the + // Record what this chain was built from: the configurations the // close has to pair with are the ones a chain ran, not only - // the ones the property lists when the pool comes down - chainDefinitions.add(proxies[i]); + // the ones the property lists when the pool comes down. + chainDefinitions.add(new ChainDefinitionKey(proxies[i])); try { JdbcInterceptor interceptor = proxies[i].getInterceptorClass().getConstructor().newInstance(); interceptor.setProperties(proxies[i].getProperties()); @@ -906,9 +911,10 @@ public class ConnectionPool { // the start list (a chain can be built while initialization is // still running), so a close during initialization must release // them too. - for (PoolProperties.InterceptorDefinition chainDefinition : chainDefinitions) { - if (!isToldAlready(chainDefinition, toldStarted) && !isToldAlready(chainDefinition, proxies)) { - informInterceptorOfPoolClosure(chainDefinition); + for (ChainDefinitionKey chainDefinition : chainDefinitions) { + PoolProperties.InterceptorDefinition definition = chainDefinition.toDefinition(); + if (!isToldAlready(definition, toldStarted) && !isToldAlready(definition, proxies)) { + informInterceptorOfPoolClosure(definition); } } } @@ -1048,6 +1054,54 @@ public class ConnectionPool { } + /** + * The immutable configuration identity of one interceptor definition in a chain: its class name and its property + * names and values. The close reconstructs a definition from this key when it delivers the notification, so the + * set of chain configurations does not retain the definition objects - or their cached classes - for every pooled + * connection that was ever built. + */ + private static final class ChainDefinitionKey { + + private final String className; + private final Map<String, String> properties; + + ChainDefinitionKey(PoolProperties.InterceptorDefinition definition) { + className = definition.getClassName(); + Map<String, String> copy = new HashMap<>(); + for (Map.Entry<String, PoolProperties.InterceptorProperty> entry : + definition.getProperties().entrySet()) { + copy.put(entry.getKey(), entry.getValue().getValue()); + } + properties = Collections.unmodifiableMap(copy); + } + + PoolProperties.InterceptorDefinition toDefinition() { + PoolProperties.InterceptorDefinition definition = new PoolProperties.InterceptorDefinition(className); + for (Map.Entry<String, String> entry : properties.entrySet()) { + definition.addProperty(entry.getKey(), entry.getValue()); + } + return definition; + } + + @Override + public int hashCode() { + return Objects.hash(className, properties); + } + + @Override + public boolean equals(Object other) { + if (other == this) { + return true; + } + if (!(other instanceof ChainDefinitionKey)) { + return false; + } + ChainDefinitionKey key = (ChainDefinitionKey) other; + return Objects.equals(className, key.className) && properties.equals(key.properties); + } + } + + /** * Initialize the connection pool - called from the constructor * diff --git a/modules/jdbc-pool/src/test/java/org/apache/tomcat/jdbc/pool/interceptor/InterceptorRemovalBeforePoolCloseBase.java b/modules/jdbc-pool/src/test/java/org/apache/tomcat/jdbc/pool/interceptor/InterceptorRemovalBeforePoolCloseBase.java index 36bd5e031b..85d8df381d 100644 --- a/modules/jdbc-pool/src/test/java/org/apache/tomcat/jdbc/pool/interceptor/InterceptorRemovalBeforePoolCloseBase.java +++ b/modules/jdbc-pool/src/test/java/org/apache/tomcat/jdbc/pool/interceptor/InterceptorRemovalBeforePoolCloseBase.java @@ -17,9 +17,11 @@ package org.apache.tomcat.jdbc.pool.interceptor; import java.lang.management.ManagementFactory; +import java.lang.reflect.Field; import java.sql.Connection; import java.sql.Statement; import java.util.Map; +import java.util.Set; import java.util.concurrent.ConcurrentHashMap; import java.util.concurrent.ConcurrentMap; import java.util.concurrent.atomic.AtomicInteger; @@ -267,4 +269,69 @@ abstract class InterceptorRemovalBeforePoolCloseBase extends JdbcPoolBaseTest { ds.close(); } } + + @Test + void chainDefinitionsDoNotGrowWithTheNumberOfConnections() throws Exception { + LifecycleRecordingInterceptor.resetCounts(); + DataSource ds = poolNamed("ChainDefinitionGrowth-" + System.nanoTime(), + RECORDING + "(marker=stable)"); + ds.getPoolProperties().setMaxActive(4); + try { + ds.createPool(); + Connection[] connections = new Connection[4]; + connections[0] = ds.getConnection(); + int afterFirstConnection = chainDefinitionCount(ds.getPool()); + for (int i = 1; i < connections.length; i++) { + connections[i] = ds.getConnection(); + } + int afterAllConnections = chainDefinitionCount(ds.getPool()); + + Assertions.assertEquals(afterFirstConnection, afterAllConnections, + "the chain-definition record must hold one entry per distinct configuration," + + " not one per pooled connection ever built"); + for (Connection connection : connections) { + connection.close(); + } + } finally { + ds.close(); + } + } + + @Test + void oneRemovedDefinitionIsClosedOnceForAllItsConnections() throws Exception { + LifecycleRecordingInterceptor.resetCounts(); + DataSource ds = poolNamed("AddedThenRemovedMany-" + System.nanoTime(), ""); + ds.getPoolProperties().setMaxActive(4); + try { + ds.createPool(); + ds.setJdbcInterceptors(RECORDING + "(marker=shared)"); + Connection[] connections = new Connection[4]; + for (int i = 0; i < connections.length; i++) { + connections[i] = ds.getConnection(); + } + for (Connection connection : connections) { + connection.close(); + } + ds.setJdbcInterceptors(""); + ds.close(); + + Assertions.assertEquals(0, LifecycleRecordingInterceptor.starts("shared"), + "the definition was added after the start and must not receive a start"); + Assertions.assertEquals(1, LifecycleRecordingInterceptor.closes("shared"), + "four connections built from the same removed definition must receive one" + + " poolClosed notification for that configuration, not one per chain"); + } finally { + ds.close(); + } + } + + private static int chainDefinitionCount(ConnectionPool pool) throws Exception { + Field field = ConnectionPool.class.getDeclaredField("chainDefinitions"); + field.setAccessible(true); + Object value = field.get(pool); + if (value instanceof Map) { + return ((Map<?, ?>) value).size(); + } + return ((Set<?>) value).size(); + } } --------------------------------------------------------------------- To unsubscribe, e-mail: [email protected] For additional commands, e-mail: [email protected]
