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

ggregory pushed a commit to branch master
in repository https://gitbox.apache.org/repos/asf/commons-dbcp.git


The following commit(s) were added to refs/heads/master by this push:
     new 9ab70dba PerUserPoolDataSource.registerPool() incorrectly replacing a 
CPDSConnectionFactory into managers map before throwing an IllegalStateException
9ab70dba is described below

commit 9ab70dba21d796d162f246e6d5fd4dc641ac08c9
Author: Gary Gregory <garydgreg...@gmail.com>
AuthorDate: Sat Nov 23 16:38:34 2024 -0500

    PerUserPoolDataSource.registerPool() incorrectly replacing a
    CPDSConnectionFactory into managers map before throwing an
    IllegalStateException
---
 src/changes/changes.xml                                 |  1 +
 .../dbcp2/datasources/PerUserPoolDataSource.java        | 17 +++++++----------
 2 files changed, 8 insertions(+), 10 deletions(-)

diff --git a/src/changes/changes.xml b/src/changes/changes.xml
index b562293c..18d83a0c 100644
--- a/src/changes/changes.xml
+++ b/src/changes/changes.xml
@@ -68,6 +68,7 @@ The <action> type attribute can be add,update,fix,remove.
       <action type="fix" issue="DBCP-599" dev="ggregory" due-to="denixx 
baykin, Phil Steitz, Gary Gregory">PoolableConnectionFactory.destroyObject() 
method behaves incorrectly on ABANDONED connection, issue with unhandled 
AbstractMethodError. DelegatingConnection.abort(Executor) should delegate to 
Jdbc41Bridge</action>
       <action type="fix" dev="ggregory" due-to="Gary 
Gregory">DelegatingConnection.setSchema(String) should delegate to 
Jdbc41Bridge.</action>
       <action type="fix" dev="ggregory" due-to="Gary Gregory">Fix possible 
NullPointerException in PoolingConnection.close().</action>
+      <action type="fix" dev="ggregory" due-to="Gary 
Gregory">PerUserPoolDataSource.registerPool() incorrectly replacing a 
CPDSConnectionFactory into managers map before throwing an 
IllegalStateException.</action>
       <action type="fix" dev="ggregory" due-to="Gary Gregory">Fix PMD 
UnnecessaryFullyQualifiedName in AbandonedTrace.</action>
       <action type="fix" dev="ggregory" due-to="Gary Gregory">Fix PMD 
UnnecessaryFullyQualifiedName in PoolableCallableStatement.</action>
       <action type="fix" dev="ggregory" due-to="Gary Gregory">Fix PMD 
UnnecessaryFullyQualifiedName in PoolablePreparedStatement.</action>
diff --git 
a/src/main/java/org/apache/commons/dbcp2/datasources/PerUserPoolDataSource.java 
b/src/main/java/org/apache/commons/dbcp2/datasources/PerUserPoolDataSource.java
index 95194c2a..7d5344ab 100644
--- 
a/src/main/java/org/apache/commons/dbcp2/datasources/PerUserPoolDataSource.java
+++ 
b/src/main/java/org/apache/commons/dbcp2/datasources/PerUserPoolDataSource.java
@@ -683,19 +683,16 @@ public class PerUserPoolDataSource extends 
InstanceKeyDataSource {
         }
     }
 
-    private synchronized void registerPool(final String userName, final String 
password)
-        throws NamingException, SQLException {
-
+    private synchronized void registerPool(final String userName, final String 
password) throws NamingException, SQLException {
         final ConnectionPoolDataSource cpds = testCPDS(userName, password);
-
         // Set up the factory we will use (passing the pool associates
         // the factory with the pool, so we do not have to do so
         // explicitly)
         final CPDSConnectionFactory factory = new CPDSConnectionFactory(cpds, 
getValidationQuery(), getValidationQueryTimeoutDuration(),
-            isRollbackAfterValidation(), userName, password);
+                isRollbackAfterValidation(), userName, password);
         factory.setMaxConn(getMaxConnDuration());
-
         // Create an object pool to contain our PooledConnections
+        @SuppressWarnings("resource")
         final GenericObjectPool<PooledConnectionAndInfo> pool = new 
GenericObjectPool<>(factory);
         factory.setPool(pool);
         pool.setBlockWhenExhausted(getPerUserBlockWhenExhausted(userName));
@@ -713,13 +710,13 @@ public class PerUserPoolDataSource extends 
InstanceKeyDataSource {
         pool.setTestOnReturn(getPerUserTestOnReturn(userName));
         pool.setTestWhileIdle(getPerUserTestWhileIdle(userName));
         
pool.setDurationBetweenEvictionRuns(getPerUserDurationBetweenEvictionRuns(userName));
-
         pool.setSwallowedExceptionListener(new SwallowedExceptionLogger(log));
-
-        final PooledConnectionManager old = managers.put(getPoolKey(userName), 
factory);
-        if (old != null) {
+        final PoolKey poolKey = getPoolKey(userName);
+        if (managers.containsKey(poolKey)) {
+            pool.close();
             throw new IllegalStateException("Pool already contains an entry 
for this user/password: " + userName);
         }
+        managers.put(poolKey, factory);
     }
 
     private <K, V> Map<K, V> replaceAll(final Map<K, V> currentMap, final 
Map<K, V> newMap) {

Reply via email to