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 982e9ca  Use UserPassKey internally.
982e9ca is described below

commit 982e9caf8b691993b78d32f0495742d021d95a47
Author: Gary Gregory <gardgreg...@gmail.com>
AuthorDate: Sun Jun 6 13:37:59 2021 -0400

    Use UserPassKey internally.
---
 .../dbcp2/datasources/CPDSConnectionFactory.java   | 22 ++++++++++------------
 .../dbcp2/datasources/SharedPoolDataSource.java    | 11 +++--------
 2 files changed, 13 insertions(+), 20 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 ddb1eb4..2323348 100644
--- 
a/src/main/java/org/apache/commons/dbcp2/datasources/CPDSConnectionFactory.java
+++ 
b/src/main/java/org/apache/commons/dbcp2/datasources/CPDSConnectionFactory.java
@@ -52,8 +52,7 @@ class CPDSConnectionFactory
     private final int validationQueryTimeoutSeconds;
     private final boolean rollbackAfterValidation;
     private ObjectPool<PooledConnectionAndInfo> pool;
-    private char[] userName;
-    private char[] userPassword;
+    private UserPassKey userPassKey;
     private Duration maxConnLifetime = Duration.ofMillis(-1);
 
     /**
@@ -87,12 +86,11 @@ class CPDSConnectionFactory
      */
     public CPDSConnectionFactory(final ConnectionPoolDataSource cpds, final 
String validationQuery,
             final int validationQueryTimeoutSeconds, final boolean 
rollbackAfterValidation, final String userName,
-            final char[] userPassword) {
+        final char[] userPassword) {
         this.cpds = cpds;
         this.validationQuery = validationQuery;
         this.validationQueryTimeoutSeconds = validationQueryTimeoutSeconds;
-        this.userName = Utils.toCharArray(userName);
-        this.userPassword = userPassword;
+        this.userPassKey = new UserPassKey(userName, userPassword);
         this.rollbackAfterValidation = rollbackAfterValidation;
     }
 
@@ -127,7 +125,7 @@ class CPDSConnectionFactory
      * @return value of password.
      */
     char[] getPasswordCharArray() {
-        return userPassword;
+        return userPassKey.getPasswordCharArray();
     }
 
     /**
@@ -153,10 +151,10 @@ class CPDSConnectionFactory
         final PooledConnectionAndInfo pci;
         try {
             PooledConnection pc = null;
-            if (userName == null) {
+            if (userPassKey.getUserName() == null) {
                 pc = cpds.getPooledConnection();
             } else {
-                pc = cpds.getPooledConnection(Utils.toString(userName), 
Utils.toString(userPassword));
+                pc = cpds.getPooledConnection(userPassKey.getUserName(), 
userPassKey.getPassword());
             }
 
             if (pc == null) {
@@ -166,7 +164,7 @@ class CPDSConnectionFactory
             // should we add this object as a listener or the pool.
             // consider the validateObject method in decision
             pc.addConnectionEventListener(this);
-            pci = new PooledConnectionAndInfo(pc, userName, userPassword);
+            pci = new PooledConnectionAndInfo(pc, userPassKey);
             pcMap.put(pc, pci);
         } catch (final SQLException e) {
             throw new RuntimeException(e.getMessage());
@@ -340,7 +338,7 @@ class CPDSConnectionFactory
      *            new password
      */
     public synchronized void setPassword(final char[] userPassword) {
-        this.userPassword =  Utils.clone(userPassword);
+        this.userPassKey = new UserPassKey(userPassKey.getUserName(), 
userPassword);
     }
 
     /**
@@ -351,7 +349,7 @@ class CPDSConnectionFactory
      */
     @Override
     public synchronized void setPassword(final String userPassword) {
-        this.userPassword = Utils.toCharArray(userPassword);
+        this.userPassKey = new UserPassKey(userPassKey.getUserName(), 
userPassword);
     }
 
     /**
@@ -386,7 +384,7 @@ class CPDSConnectionFactory
     @Override
     public void closePool(final String userName) throws SQLException {
         synchronized (this) {
-            if (userName == null || !userName.equals(this.userName)) {
+            if (userName == null || 
!userName.equals(this.userPassKey.getUserName())) {
                 return;
             }
         }
diff --git 
a/src/main/java/org/apache/commons/dbcp2/datasources/SharedPoolDataSource.java 
b/src/main/java/org/apache/commons/dbcp2/datasources/SharedPoolDataSource.java
index 96f831c..e735c98 100644
--- 
a/src/main/java/org/apache/commons/dbcp2/datasources/SharedPoolDataSource.java
+++ 
b/src/main/java/org/apache/commons/dbcp2/datasources/SharedPoolDataSource.java
@@ -126,25 +126,20 @@ public class SharedPoolDataSource extends 
InstanceKeyDataSource {
                 try {
                     registerPool(userName, userPassword);
                 } catch (final NamingException e) {
-                    throw new SQLException("RegisterPool failed", e);
+                    throw new SQLException("registerPool failed", e);
                 }
             }
         }
 
-        PooledConnectionAndInfo info = null;
-
-        final UserPassKey key = new UserPassKey(userName, userPassword);
-
         try {
-            info = pool.borrowObject(key);
+            return pool.borrowObject(new UserPassKey(userName, userPassword));
         } catch (final Exception e) {
             throw new SQLException("Could not retrieve connection info from 
pool", e);
         }
-        return info;
     }
 
     @Override
-    protected PooledConnectionManager getConnectionManager(final UserPassKey 
upkey) {
+    protected PooledConnectionManager getConnectionManager(final UserPassKey 
userPassKey) {
         return factory;
     }
 

Reply via email to