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 f4ead48bc2a9a7d7eb7cf7143964d1c202a11928 Author: Gary Gregory <[email protected]> AuthorDate: Mon Nov 23 10:01:49 2026 -0500 Use Objects.equals to match pool user names Allow closePool(null) to close a pool using default credentials when its configured user name is also null. Preserve matching behavior for non-null user names. Document null matching and add regression coverage for matching, mismatching, and null user names, including idle connection cleanup. --- .../dbcp2/datasources/CPDSConnectionFactory.java | 5 +++-- .../datasources/TestCPDSConnectionFactory.java | 26 ++++++++++++++++++++++ 2 files changed, 29 insertions(+), 2 deletions(-) diff --git a/src/main/java/org/apache/commons/dbcp2/datasources/CPDSConnectionFactory.java b/src/main/java/org/apache/commons/dbcp2/datasources/CPDSConnectionFactory.java index 49c7de32..287cebdd 100644 --- a/src/main/java/org/apache/commons/dbcp2/datasources/CPDSConnectionFactory.java +++ b/src/main/java/org/apache/commons/dbcp2/datasources/CPDSConnectionFactory.java @@ -19,6 +19,7 @@ package org.apache.commons.dbcp2.datasources; import java.sql.Connection; import java.sql.SQLException; import java.time.Duration; +import java.util.Objects; import java.util.function.Supplier; import javax.sql.ConnectionEvent; @@ -71,12 +72,12 @@ final class CPDSConnectionFactory extends AbstractConnectionFactory /** * Verifies that the user name matches the user whose connections are being managed by this factory and closes the - * pool if this is the case; otherwise does nothing. + * pool if this is the case; otherwise does nothing. Two null user names match. */ @Override public void closePool(final String userName) throws SQLException { synchronized (this) { - if (userName == null || !userName.equals(this.userPassKey.getUserName())) { + if (!Objects.equals(userName, userPassKey.getUserName())) { return; } } diff --git a/src/test/java/org/apache/commons/dbcp2/datasources/TestCPDSConnectionFactory.java b/src/test/java/org/apache/commons/dbcp2/datasources/TestCPDSConnectionFactory.java index 31f2798e..a7ce3ef6 100644 --- a/src/test/java/org/apache/commons/dbcp2/datasources/TestCPDSConnectionFactory.java +++ b/src/test/java/org/apache/commons/dbcp2/datasources/TestCPDSConnectionFactory.java @@ -33,6 +33,8 @@ import org.apache.commons.dbcp2.cpdsadapter.DriverAdapterCPDS; import org.apache.commons.pool2.impl.GenericObjectPool; import org.junit.jupiter.api.BeforeEach; import org.junit.jupiter.api.Test; +import org.junit.jupiter.params.ParameterizedTest; +import org.junit.jupiter.params.provider.CsvSource; /** */ @@ -56,6 +58,30 @@ public class TestCPDSConnectionFactory { delegate.setPassword("password"); } + /** + * Tests that closing a pool requires matching user names, including two null names for default credentials. + * + * @param poolUserName the user name managed by the pool, or null for default credentials. + * @param userName the user name passed to closePool. + * @param expectedClosed whether the pool should close and release its idle connection. + * @throws Exception if creating, populating, or closing the pool fails. + */ + @ParameterizedTest + @CsvSource(value = {"userName, userName, true", "NULL, NULL, true", "userName, other, false", + "userName, NULL, false", "NULL, userName, false"}, nullValues = "NULL") + void testClosePool(final String poolUserName, final String userName, final boolean expectedClosed) throws Exception { + final CPDSConnectionFactory factory = new CPDSConnectionFactory(cpds, null, Duration.ofMillis(-1), false, poolUserName, + "password".toCharArray(), Duration.ofMillis(-1), () -> null); + try (GenericObjectPool<PooledConnectionAndInfo> pool = new GenericObjectPool<>(factory)) { + factory.setPool(pool); + pool.addObject(); + assertEquals(1, pool.getNumIdle()); + factory.closePool(userName); + assertEquals(expectedClosed, pool.isClosed()); + assertEquals(expectedClosed ? 0 : 1, pool.getNumIdle()); + } + } + /** * JIRA DBCP-216 *
