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 bfd20dd868b3d322be5a0b38115582359b70ea4f
Author: Gary Gregory <[email protected]>
AuthorDate: Mon Nov 23 10:38:54 2026 -0500

    Handle null passwords consistently when borrowing connections
    
    Use Objects.equals for both password comparisons in
    InstanceKeyDataSource.getConnection. Accept matching null passwords
    during retries after a password change.
    
    Add regression coverage for both pool types, including changes to
    and from null and rejection of incorrect passwords. Document
    null-password equality.
---
 .../dbcp2/datasources/InstanceKeyDataSource.java   |  6 +--
 .../datasources/TestInstanceKeyDataSource.java     | 43 ++++++++++++++++++++++
 2 files changed, 46 insertions(+), 3 deletions(-)

diff --git 
a/src/main/java/org/apache/commons/dbcp2/datasources/InstanceKeyDataSource.java 
b/src/main/java/org/apache/commons/dbcp2/datasources/InstanceKeyDataSource.java
index 200ddea0..19dfb5c4 100644
--- 
a/src/main/java/org/apache/commons/dbcp2/datasources/InstanceKeyDataSource.java
+++ 
b/src/main/java/org/apache/commons/dbcp2/datasources/InstanceKeyDataSource.java
@@ -230,7 +230,7 @@ public abstract class InstanceKeyDataSource implements 
DataSource, Referenceable
     /**
      * Attempts to retrieve a database connection using {@link 
#getPooledConnectionAndInfo(String, String)} with the
      * provided user name and password. The password on the {@code 
PooledConnectionAndInfo} instance returned by
-     * {@code getPooledConnectionAndInfo} is compared to the {@code password} 
parameter. If the comparison
+     * {@code getPooledConnectionAndInfo} is compared to the {@code 
userPassword} parameter. Two null passwords are considered equal. If the 
comparison
      * fails, a database connection using the supplied user name and password 
is attempted. If the connection attempt
      * fails, an SQLException is thrown, indicating that the given password 
did not match the password used to create
      * the pooled connection. If the connection attempt succeeds, this means 
that the database password has been
@@ -257,7 +257,7 @@ public abstract class InstanceKeyDataSource implements 
DataSource, Referenceable
         }
 
         // Password on PooledConnectionAndInfo does not match
-        if (!(userPassword == null ? info.getPassword() == null : 
userPassword.equals(info.getPassword()))) {
+        if (!Objects.equals(userPassword, info.getPassword())) {
             try { // See if password has changed by attempting connection
                 testCPDS(userName, userPassword);
             } catch (final SQLException ex) {
@@ -289,7 +289,7 @@ public abstract class InstanceKeyDataSource implements 
DataSource, Referenceable
                     closeDueToException(info);
                     throw new SQLException("Cannot borrow connection from 
pool", e);
                 }
-                if (info != null && userPassword != null && 
userPassword.equals(info.getPassword())) {
+                if (info != null && Objects.equals(userPassword, 
info.getPassword())) {
                     break;
                 }
                 if (info != null) {
diff --git 
a/src/test/java/org/apache/commons/dbcp2/datasources/TestInstanceKeyDataSource.java
 
b/src/test/java/org/apache/commons/dbcp2/datasources/TestInstanceKeyDataSource.java
index 9836ff00..b58986a4 100644
--- 
a/src/test/java/org/apache/commons/dbcp2/datasources/TestInstanceKeyDataSource.java
+++ 
b/src/test/java/org/apache/commons/dbcp2/datasources/TestInstanceKeyDataSource.java
@@ -24,6 +24,11 @@ import static org.junit.jupiter.api.Assertions.assertNull;
 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 static org.mockito.ArgumentMatchers.eq;
+import static org.mockito.ArgumentMatchers.nullable;
+import static org.mockito.Mockito.doAnswer;
+import static org.mockito.Mockito.doThrow;
+import static org.mockito.Mockito.mock;
 
 import java.io.PrintWriter;
 import java.sql.Connection;
@@ -31,10 +36,14 @@ import java.sql.SQLException;
 import java.time.Duration;
 import java.util.Properties;
 
+import javax.sql.ConnectionPoolDataSource;
+
 import org.apache.commons.dbcp2.cpdsadapter.DriverAdapterCPDS;
 import org.junit.jupiter.api.AfterEach;
 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;
 
 /**
  */
@@ -92,6 +101,40 @@ public class TestInstanceKeyDataSource {
         }
     }
 
+    /**
+     * Tests password changes, including changes to and from null, using real 
pools and a provider that validates the current password.
+     *
+     * @param shared whether to use a shared pool instead of a per-user pool.
+     * @param oldPassword the password used to create the initial pooled 
connection.
+     * @param newPassword the replacement password accepted by the provider.
+     * @throws SQLException if obtaining or closing a connection fails.
+     */
+    @ParameterizedTest
+    @CsvSource(value = {"true, bar, baz", "true, bar, NULL", "true, NULL, baz",
+            "false, bar, baz", "false, bar, NULL", "false, NULL, baz"}, 
nullValues = "NULL")
+    void testConnectionPasswordChange(final boolean shared, final String 
oldPassword, final String newPassword) throws SQLException {
+        final ConnectionPoolDataSource provider = 
mock(ConnectionPoolDataSource.class);
+        doThrow(new SQLException("Invalid 
password")).when(provider).getPooledConnection(eq(USER), 
nullable(String.class));
+        doAnswer(invocation -> 
pcds.getPooledConnection()).when(provider).getPooledConnection(USER, 
oldPassword);
+        try (final InstanceKeyDataSource dataSource = shared ? new 
SharedPoolDataSource() : new PerUserPoolDataSource()) {
+            dataSource.setConnectionPoolDataSource(provider);
+            try (final Connection connection = dataSource.getConnection(USER, 
oldPassword)) {
+                assertFalse(connection.isClosed());
+            }
+            assertThrows(SQLException.class, () -> 
dataSource.getConnection(USER, oldPassword == null ? PASS : null));
+
+            doThrow(new SQLException("Invalid 
password")).when(provider).getPooledConnection(USER, oldPassword);
+            doAnswer(invocation -> 
pcds.getPooledConnection()).when(provider).getPooledConnection(USER, 
newPassword);
+            try (final Connection connection = dataSource.getConnection(USER, 
newPassword)) {
+                assertFalse(connection.isClosed());
+            }
+            assertThrows(SQLException.class, () -> 
dataSource.getConnection(USER, oldPassword));
+            try (final Connection connection = dataSource.getConnection(USER, 
newPassword)) {
+                assertFalse(connection.isClosed());
+            }
+        }
+    }
+
     @Test
     void testConnectionPoolDataSource() {
         assertEquals(pcds, spds.getConnectionPoolDataSource());

Reply via email to