commons-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From ggreg...@apache.org
Subject commons-dbcp git commit: [DBCP-486] DriverAdapterCPDS.setUser(), setPassword(), and getPooledConnection() with null arguments throw NullPointerExceptions when connection properties are set
Date Wed, 18 Apr 2018 22:33:18 GMT
Repository: commons-dbcp
Updated Branches:
  refs/heads/master 323d3fbae -> a6feb941e


[DBCP-486] DriverAdapterCPDS.setUser(), setPassword(), and
getPooledConnection() with null arguments throw NullPointerExceptions
when connection properties are set

Project: http://git-wip-us.apache.org/repos/asf/commons-dbcp/repo
Commit: http://git-wip-us.apache.org/repos/asf/commons-dbcp/commit/a6feb941
Tree: http://git-wip-us.apache.org/repos/asf/commons-dbcp/tree/a6feb941
Diff: http://git-wip-us.apache.org/repos/asf/commons-dbcp/diff/a6feb941

Branch: refs/heads/master
Commit: a6feb941e5ff8fde22865f981d912c6d5f48546e
Parents: 323d3fb
Author: Gary Gregory <garydgregory@gmail.com>
Authored: Wed Apr 18 16:33:16 2018 -0600
Committer: Gary Gregory <garydgregory@gmail.com>
Committed: Wed Apr 18 16:33:16 2018 -0600

----------------------------------------------------------------------
 src/changes/changes.xml                         |  2 +-
 .../dbcp2/cpdsadapter/DriverAdapterCPDS.java    | 30 +++++++++-----------
 .../dbcp2/managed/ManagedConnection.java        | 20 +++++++------
 3 files changed, 27 insertions(+), 25 deletions(-)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/commons-dbcp/blob/a6feb941/src/changes/changes.xml
----------------------------------------------------------------------
diff --git a/src/changes/changes.xml b/src/changes/changes.xml
index 8f69ffb..7abec82 100644
--- a/src/changes/changes.xml
+++ b/src/changes/changes.xml
@@ -68,7 +68,7 @@ The <action> type attribute can be add,update,fix,remove.
         Make constant public: org.apache.commons.dbcp2.PoolingDriver.URL_PREFIX.
       </action>
       <action dev="ggregory" type="update" issue="DBCP-486" due-to="Gary Gregory">
-        DriverAdapterCPDS.setUser(null) and setPassword(null) throw NullPointerExceptions
when connection properties are set.
+        DriverAdapterCPDS.setUser(), setPassword(), and getPooledConnection() with null arguments
throw NullPointerExceptions when connection properties are set.
       </action>
     </release>
     <release version="2.2.0" date="2017-12-DD" description="This is a minor release, including
bug fixes and enhancements.">

http://git-wip-us.apache.org/repos/asf/commons-dbcp/blob/a6feb941/src/main/java/org/apache/commons/dbcp2/cpdsadapter/DriverAdapterCPDS.java
----------------------------------------------------------------------
diff --git a/src/main/java/org/apache/commons/dbcp2/cpdsadapter/DriverAdapterCPDS.java b/src/main/java/org/apache/commons/dbcp2/cpdsadapter/DriverAdapterCPDS.java
index bf7d960..748dec7 100644
--- a/src/main/java/org/apache/commons/dbcp2/cpdsadapter/DriverAdapterCPDS.java
+++ b/src/main/java/org/apache/commons/dbcp2/cpdsadapter/DriverAdapterCPDS.java
@@ -177,8 +177,8 @@ public class DriverAdapterCPDS
         // exception upon first invocation.
         try {
             if (connectionProperties != null) {
-                connectionProperties.put(KEY_USER, username);
-                connectionProperties.put(KEY_PASSWORD, pass);
+                update(connectionProperties, KEY_USER, username);
+                update(connectionProperties, KEY_PASSWORD, pass);
                 pci = new PooledConnectionImpl(DriverManager.getConnection(
                         getUrl(), connectionProperties));
             } else {
@@ -442,13 +442,7 @@ public class DriverAdapterCPDS
     public void setPassword(final String v) {
         assertInitializationAllowed();
         this.password = v;
-        if (connectionProperties != null) {
-            if (v == null) {
-                connectionProperties.remove(KEY_PASSWORD);
-            } else {
-                connectionProperties.setProperty(KEY_PASSWORD, v);
-            }
-        }
+        update(connectionProperties, KEY_PASSWORD, v);
     }
 
     /**
@@ -485,13 +479,7 @@ public class DriverAdapterCPDS
     public void setUser(final String v) {
         assertInitializationAllowed();
         this.user = v;
-        if (connectionProperties != null) {
-            if (v == null) {
-                connectionProperties.remove(KEY_USER);
-            } else {
-                connectionProperties.setProperty(KEY_USER, v);
-            }
-        }
+        update(connectionProperties, KEY_USER, v);
     }
 
     /**
@@ -716,4 +704,14 @@ public class DriverAdapterCPDS
     {
         _maxPreparedStatements = maxPreparedStatements;
     }
+    
+    private void update(final Properties properties, final String key, final String value)
{
+        if (properties != null) {
+            if (value == null) {
+                properties.remove(key);
+            } else {
+                properties.setProperty(key, value);
+            }
+        }        
+    }
 }

http://git-wip-us.apache.org/repos/asf/commons-dbcp/blob/a6feb941/src/main/java/org/apache/commons/dbcp2/managed/ManagedConnection.java
----------------------------------------------------------------------
diff --git a/src/main/java/org/apache/commons/dbcp2/managed/ManagedConnection.java b/src/main/java/org/apache/commons/dbcp2/managed/ManagedConnection.java
index a66a7a8..d05144b 100644
--- a/src/main/java/org/apache/commons/dbcp2/managed/ManagedConnection.java
+++ b/src/main/java/org/apache/commons/dbcp2/managed/ManagedConnection.java
@@ -159,14 +159,16 @@ public class ManagedConnection<C extends Connection> extends DelegatingConnectio
     @Override
     public void close() throws SQLException {
         if (!isClosedInternal()) {
-            try {
-                // Don't actually close the connection if in a transaction. The
-                // connection will be closed by the transactionComplete method.
-                if (transactionContext == null) {
-                    super.close();
+            synchronized (this) {
+                try {
+                    // Don't actually close the connection if in a transaction. The
+                    // connection will be closed by the transactionComplete method.
+                    if (transactionContext == null) {
+                        super.close();
+                    }
+                } finally {
+                    setClosedInternal(true);
                 }
-            } finally {
-                setClosedInternal(true);
             }
         }
     }
@@ -186,7 +188,9 @@ public class ManagedConnection<C extends Connection> extends DelegatingConnectio
     }
 
     protected void transactionComplete() {
-        transactionContext = null;
+        synchronized (this) {
+            transactionContext = null;
+        }
 
         // If we were using a shared connection, clear the reference now that
         // the transaction has completed


Mime
View raw message