You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@commons.apache.org by gg...@apache.org on 2022/07/03 17:05:47 UTC

[commons-dbcp] branch master updated (09677c3e -> b954fc93)

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

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


    from 09677c3e LifetimeExceededException should extend SQLException.
     new 278f169c Javadoc
     new 74f6a019 Fix message now that values are Durations instead of milliseconds.
     new b954fc93 Refactor duplicate logic and throw SQLException instead of Exception in internal validateLifetime() methods.

The 3 revisions listed above as "new" are entirely new to this
repository and will be described in separate emails.  The revisions
listed as "add" were already present in the repository and have only
been added to this reference.


Summary of changes:
 .../commons/dbcp2/LifetimeExceededException.java     | 11 +++++------
 .../commons/dbcp2/PoolableConnectionFactory.java     |  9 +--------
 src/main/java/org/apache/commons/dbcp2/Utils.java    | 13 +++++++++++++
 .../dbcp2/datasources/CPDSConnectionFactory.java     | 20 +++++++-------------
 .../datasources/KeyedCPDSConnectionFactory.java      | 16 +++-------------
 .../org/apache/commons/dbcp2/LocalStrings.properties |  2 +-
 6 files changed, 30 insertions(+), 41 deletions(-)


[commons-dbcp] 02/03: Fix message now that values are Durations instead of milliseconds.

Posted by gg...@apache.org.
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

commit 74f6a0193579b8c2a078983dfd7343d57fc42e9e
Author: Gary Gregory <ga...@gmail.com>
AuthorDate: Sun Jul 3 12:54:45 2022 -0400

    Fix message now that values are Durations instead of milliseconds.
---
 src/main/resources/org/apache/commons/dbcp2/LocalStrings.properties | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)

diff --git a/src/main/resources/org/apache/commons/dbcp2/LocalStrings.properties b/src/main/resources/org/apache/commons/dbcp2/LocalStrings.properties
index 2e0ada2b..a7f3e904 100644
--- a/src/main/resources/org/apache/commons/dbcp2/LocalStrings.properties
+++ b/src/main/resources/org/apache/commons/dbcp2/LocalStrings.properties
@@ -13,7 +13,7 @@
 # See the License for the specific language governing permissions and
 # limitations under the License.
 
-connectionFactory.lifetimeExceeded=The lifetime of the connection [{0}] milliseconds exceeds the maximum permitted value of [{1}] milliseconds
+connectionFactory.lifetimeExceeded=The lifetime of the connection [{0}] exceeds the maximum permitted value of [{1}].
 
 poolableConnectionFactory.validateObject.fail=Failed to validate a poolable connection.
 


[commons-dbcp] 03/03: Refactor duplicate logic and throw SQLException instead of Exception in internal validateLifetime() methods.

Posted by gg...@apache.org.
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

commit b954fc9321030e8fe2fad54839961c18218f86e9
Author: Gary Gregory <ga...@gmail.com>
AuthorDate: Sun Jul 3 13:05:41 2022 -0400

    Refactor duplicate logic and throw SQLException instead of Exception in
    internal validateLifetime() methods.
---
 .../commons/dbcp2/PoolableConnectionFactory.java     |  9 +--------
 src/main/java/org/apache/commons/dbcp2/Utils.java    | 13 +++++++++++++
 .../dbcp2/datasources/CPDSConnectionFactory.java     | 20 +++++++-------------
 .../datasources/KeyedCPDSConnectionFactory.java      | 16 +++-------------
 4 files changed, 24 insertions(+), 34 deletions(-)

diff --git a/src/main/java/org/apache/commons/dbcp2/PoolableConnectionFactory.java b/src/main/java/org/apache/commons/dbcp2/PoolableConnectionFactory.java
index 80aa601d..71aebbb4 100644
--- a/src/main/java/org/apache/commons/dbcp2/PoolableConnectionFactory.java
+++ b/src/main/java/org/apache/commons/dbcp2/PoolableConnectionFactory.java
@@ -20,7 +20,6 @@ import java.sql.Connection;
 import java.sql.SQLException;
 import java.sql.Statement;
 import java.time.Duration;
-import java.time.Instant;
 import java.util.Collection;
 import java.util.Objects;
 import java.util.concurrent.atomic.AtomicLong;
@@ -745,19 +744,13 @@ public class PoolableConnectionFactory implements PooledObjectFactory<PoolableCo
     }
 
     private void validateLifetime(final PooledObject<PoolableConnection> p) throws LifetimeExceededException {
-        if (maxConnDuration.compareTo(Duration.ZERO) > 0) {
-            final Duration lifetimeDuration = Duration.between(p.getCreateInstant(), Instant.now());
-            if (lifetimeDuration.compareTo(maxConnDuration) > 0) {
-                throw new LifetimeExceededException(Utils.getMessage("connectionFactory.lifetimeExceeded", lifetimeDuration, maxConnDuration));
-            }
-        }
+        Utils.validateLifetime(p, maxConnDuration);
     }
 
     @Override
     public boolean validateObject(final PooledObject<PoolableConnection> p) {
         try {
             validateLifetime(p);
-
             validateConnection(p.getObject());
             return true;
         } catch (final Exception e) {
diff --git a/src/main/java/org/apache/commons/dbcp2/Utils.java b/src/main/java/org/apache/commons/dbcp2/Utils.java
index 8caf10d1..afe64f51 100644
--- a/src/main/java/org/apache/commons/dbcp2/Utils.java
+++ b/src/main/java/org/apache/commons/dbcp2/Utils.java
@@ -21,11 +21,15 @@ import java.sql.Connection;
 import java.sql.ResultSet;
 import java.sql.Statement;
 import java.text.MessageFormat;
+import java.time.Duration;
+import java.time.Instant;
 import java.util.HashSet;
 import java.util.Properties;
 import java.util.ResourceBundle;
 import java.util.Set;
 
+import org.apache.commons.pool2.PooledObject;
+
 /**
  * Utility methods.
  *
@@ -219,6 +223,15 @@ public final class Utils {
         return value == null ? null : String.valueOf(value);
     }
 
+    public static void validateLifetime(final PooledObject<?> p, final Duration maxDuration) throws LifetimeExceededException {
+        if (maxDuration.compareTo(Duration.ZERO) > 0) {
+            final Duration lifetimeDuration = Duration.between(p.getCreateInstant(), Instant.now());
+            if (lifetimeDuration.compareTo(maxDuration) > 0) {
+                throw new LifetimeExceededException(Utils.getMessage("connectionFactory.lifetimeExceeded", lifetimeDuration, maxDuration));
+            }
+        }
+    }
+
     private Utils() {
         // not instantiable
     }
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 9962fe98..7907ef20 100644
--- a/src/main/java/org/apache/commons/dbcp2/datasources/CPDSConnectionFactory.java
+++ b/src/main/java/org/apache/commons/dbcp2/datasources/CPDSConnectionFactory.java
@@ -21,7 +21,6 @@ import java.sql.ResultSet;
 import java.sql.SQLException;
 import java.sql.Statement;
 import java.time.Duration;
-import java.time.Instant;
 import java.util.Collections;
 import java.util.Map;
 import java.util.Set;
@@ -176,7 +175,7 @@ class CPDSConnectionFactory
     }
 
     @Override
-    public void activateObject(final PooledObject<PooledConnectionAndInfo> p) throws Exception {
+    public void activateObject(final PooledObject<PooledConnectionAndInfo> p) throws SQLException {
         validateLifetime(p);
     }
 
@@ -304,10 +303,6 @@ class CPDSConnectionFactory
         }
     }
 
-    // ***********************************************************************
-    // java.sql.ConnectionEventListener implementation
-    // ***********************************************************************
-
     @Override
     public synchronized PooledObject<PooledConnectionAndInfo> makeObject() {
         final PooledConnectionAndInfo pci;
@@ -334,6 +329,10 @@ class CPDSConnectionFactory
         return new DefaultPooledObject<>(pci);
     }
 
+    // ***********************************************************************
+    // java.sql.ConnectionEventListener implementation
+    // ***********************************************************************
+
     @Override
     public void passivateObject(final PooledObject<PooledConnectionAndInfo> p) throws Exception {
         validateLifetime(p);
@@ -434,13 +433,8 @@ class CPDSConnectionFactory
         return builder.toString();
     }
 
-    private void validateLifetime(final PooledObject<PooledConnectionAndInfo> pooledObject) throws Exception {
-        if (maxConnDuration.compareTo(Duration.ZERO) > 0) {
-            final Duration lifetimeDuration = Duration.between(pooledObject.getCreateInstant(), Instant.now());
-            if (lifetimeDuration.compareTo(maxConnDuration) > 0) {
-                throw new Exception(Utils.getMessage("connectionFactory.lifetimeExceeded", lifetimeDuration, maxConnDuration));
-            }
-        }
+    private void validateLifetime(final PooledObject<PooledConnectionAndInfo> p) throws SQLException {
+        Utils.validateLifetime(p, maxConnDuration);
     }
 
     @Override
diff --git a/src/main/java/org/apache/commons/dbcp2/datasources/KeyedCPDSConnectionFactory.java b/src/main/java/org/apache/commons/dbcp2/datasources/KeyedCPDSConnectionFactory.java
index e2c3f991..3631a98d 100644
--- a/src/main/java/org/apache/commons/dbcp2/datasources/KeyedCPDSConnectionFactory.java
+++ b/src/main/java/org/apache/commons/dbcp2/datasources/KeyedCPDSConnectionFactory.java
@@ -21,7 +21,6 @@ import java.sql.ResultSet;
 import java.sql.SQLException;
 import java.sql.Statement;
 import java.time.Duration;
-import java.time.Instant;
 import java.util.Collections;
 import java.util.Map;
 import java.util.Set;
@@ -112,7 +111,7 @@ class KeyedCPDSConnectionFactory implements KeyedPooledObjectFactory<UserPassKey
     }
 
     @Override
-    public void activateObject(final UserPassKey key, final PooledObject<PooledConnectionAndInfo> p) throws Exception {
+    public void activateObject(final UserPassKey key, final PooledObject<PooledConnectionAndInfo> p) throws SQLException {
         validateLifetime(p);
     }
 
@@ -224,10 +223,6 @@ class KeyedCPDSConnectionFactory implements KeyedPooledObjectFactory<UserPassKey
         }
     }
 
-    // ***********************************************************************
-    // java.sql.ConnectionEventListener implementation
-    // ***********************************************************************
-
     /**
      * Creates a new {@code PooledConnectionAndInfo} from the given {@code UserPassKey}.
      *
@@ -317,13 +312,8 @@ class KeyedCPDSConnectionFactory implements KeyedPooledObjectFactory<UserPassKey
         this.pool = pool;
     }
 
-    private void validateLifetime(final PooledObject<PooledConnectionAndInfo> p) throws Exception {
-        if (maxConnLifetime.compareTo(Duration.ZERO) > 0) {
-            final Duration lifetimeDuration = Duration.between(p.getCreateInstant(), Instant.now());
-            if (lifetimeDuration.compareTo(maxConnLifetime) > 0) {
-                throw new Exception(Utils.getMessage("connectionFactory.lifetimeExceeded", lifetimeDuration, maxConnLifetime));
-            }
-        }
+    private void validateLifetime(final PooledObject<PooledConnectionAndInfo> pooledObject) throws SQLException {
+        Utils.validateLifetime(pooledObject, maxConnLifetime);
     }
 
     /**


[commons-dbcp] 01/03: Javadoc

Posted by gg...@apache.org.
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

commit 278f169c173c5e6d833fe5283a28e85bf0faf239
Author: Gary Gregory <ga...@gmail.com>
AuthorDate: Sun Jul 3 12:53:53 2022 -0400

    Javadoc
---
 .../org/apache/commons/dbcp2/LifetimeExceededException.java   | 11 +++++------
 1 file changed, 5 insertions(+), 6 deletions(-)

diff --git a/src/main/java/org/apache/commons/dbcp2/LifetimeExceededException.java b/src/main/java/org/apache/commons/dbcp2/LifetimeExceededException.java
index 4344b5d1..ff48941f 100644
--- a/src/main/java/org/apache/commons/dbcp2/LifetimeExceededException.java
+++ b/src/main/java/org/apache/commons/dbcp2/LifetimeExceededException.java
@@ -28,18 +28,17 @@ class LifetimeExceededException extends SQLException {
     private static final long serialVersionUID = -3783783104516492659L;
 
     /**
-     * Create a LifetimeExceededException.
+     * Constructs a new instance.
      */
     public LifetimeExceededException() {
     }
 
     /**
-     * Create a LifetimeExceededException with the given message.
+     * Constructs a new instance with the given message.
      *
-     * @param message
-     *            The message with which to create the exception
+     * @param reason a description of the exception
      */
-    public LifetimeExceededException(final String message) {
-        super(message);
+    public LifetimeExceededException(final String reason) {
+        super(reason);
     }
 }