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 2021/08/19 13:11:15 UTC

[commons-dbcp] branch master updated: PerUserPoolDataSourceFactory.getNewInstance(Reference) parsed defaultMaxWaitMillis as an int instead of a long.

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 f609b04  PerUserPoolDataSourceFactory.getNewInstance(Reference) parsed defaultMaxWaitMillis as an int instead of a long.
f609b04 is described below

commit f609b04f916dfe96c13671ed98f990e497975fcf
Author: Gary Gregory <ga...@gmail.com>
AuthorDate: Thu Aug 19 09:11:13 2021 -0400

    PerUserPoolDataSourceFactory.getNewInstance(Reference) parsed
    defaultMaxWaitMillis as an int instead of a long.
    
    - Migrate away from deprecated APIs in Apache Commons Pool.
    - Reuse internal methods.
---
 src/changes/changes.xml                            | 10 +++-
 .../datasources/InstanceKeyDataSourceFactory.java  | 13 ++---
 .../dbcp2/datasources/PerUserPoolDataSource.java   |  9 ++--
 .../datasources/PerUserPoolDataSourceFactory.java  | 55 +++++++++++-----------
 .../dbcp2/datasources/SharedPoolDataSource.java    |  7 +--
 5 files changed, 52 insertions(+), 42 deletions(-)

diff --git a/src/changes/changes.xml b/src/changes/changes.xml
index efae885..21aa807 100644
--- a/src/changes/changes.xml
+++ b/src/changes/changes.xml
@@ -62,11 +62,17 @@ The <action> type attribute can be add,update,fix,remove.
   <body>
     <release version="2.10.0" date="2021-MM-DD" description="This is a minor release, including bug fixes and enhancements.">
       <!-- FIX -->
+      <action dev="ggregory" type="fix" due-to="newnewcoder, Gary Gregory">
+        Fix StackOverflowError in PoolableConnection.isDisconnectionSqlException #123.
+      </action>
+      <action dev="ggregory" type="fix" due-to="Gary Gregory">
+        PerUserPoolDataSourceFactory.getNewInstance(Reference) parsed defaultMaxWaitMillis as an int instead of a long.
+      </action>
       <action dev="ggregory" type="fix" due-to="Gary Gregory">
         Reimplement time tracking in AbandonedTrace with an Instant instead of a long.
       </action>
-      <action dev="ggregory" type="fix" due-to="newnewcoder, Gary Gregory">
-        Fix StackOverflowError in PoolableConnection.isDisconnectionSqlException #123.
+      <action dev="ggregory" type="fix" due-to="Gary Gregory">
+        Migrate away from deprecated APIs in Apache Commons Pool.
       </action>
       <!-- ADDS -->
       <action dev="ggregory" type="add" due-to="Gary Gregory">
diff --git a/src/main/java/org/apache/commons/dbcp2/datasources/InstanceKeyDataSourceFactory.java b/src/main/java/org/apache/commons/dbcp2/datasources/InstanceKeyDataSourceFactory.java
index bcb3b31..222e32b 100644
--- a/src/main/java/org/apache/commons/dbcp2/datasources/InstanceKeyDataSourceFactory.java
+++ b/src/main/java/org/apache/commons/dbcp2/datasources/InstanceKeyDataSourceFactory.java
@@ -19,6 +19,7 @@ package org.apache.commons.dbcp2.datasources;
 import java.io.ByteArrayInputStream;
 import java.io.IOException;
 import java.io.ObjectInputStream;
+import java.time.Duration;
 import java.util.ArrayList;
 import java.util.Hashtable;
 import java.util.List;
@@ -188,15 +189,15 @@ abstract class InstanceKeyDataSourceFactory implements ObjectFactory {
      */
     protected abstract boolean isCorrectClass(String className);
 
-    private boolean parseBoolean(final RefAddr refAddr) {
+    boolean parseBoolean(final RefAddr refAddr) {
         return Boolean.parseBoolean(toString(refAddr));
     }
 
-    private int parseInt(final RefAddr refAddr) {
+    int parseInt(final RefAddr refAddr) {
         return Integer.parseInt(toString(refAddr));
     }
 
-    private long parseLong(final RefAddr refAddr) {
+    long parseLong(final RefAddr refAddr) {
         return Long.parseLong(toString(refAddr));
     }
 
@@ -253,7 +254,7 @@ abstract class InstanceKeyDataSourceFactory implements ObjectFactory {
 
         refAddr = ref.get("maxWaitMillis");
         if (refAddr != null && refAddr.getContent() != null) {
-            ikds.setDefaultMaxWaitMillis(parseLong(refAddr));
+            ikds.setDefaultMaxWait(Duration.ofMillis(parseLong(refAddr)));
         }
 
         refAddr = ref.get("minEvictableIdleTimeMillis");
@@ -320,7 +321,7 @@ abstract class InstanceKeyDataSourceFactory implements ObjectFactory {
 
         refAddr = ref.get("maxConnLifetimeMillis");
         if (refAddr != null && refAddr.getContent() != null) {
-            ikds.setMaxConnLifetimeMillis(parseLong(refAddr));
+            ikds.setMaxConnLifetime(Duration.ofMillis(parseLong(refAddr)));
         }
 
         // Connection properties
@@ -341,7 +342,7 @@ abstract class InstanceKeyDataSourceFactory implements ObjectFactory {
         }
     }
 
-    private String toString(final RefAddr refAddr) {
+    String toString(final RefAddr refAddr) {
         return refAddr.getContent().toString();
     }
 }
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 8e584a7..c69a40a 100644
--- a/src/main/java/org/apache/commons/dbcp2/datasources/PerUserPoolDataSource.java
+++ b/src/main/java/org/apache/commons/dbcp2/datasources/PerUserPoolDataSource.java
@@ -20,6 +20,7 @@ import java.io.IOException;
 import java.io.ObjectInputStream;
 import java.sql.Connection;
 import java.sql.SQLException;
+import java.time.Duration;
 import java.util.HashMap;
 import java.util.Map;
 import java.util.NoSuchElementException;
@@ -640,16 +641,16 @@ public class PerUserPoolDataSource extends InstanceKeyDataSource {
         pool.setLifo(getPerUserLifo(userName));
         pool.setMaxIdle(getPerUserMaxIdle(userName));
         pool.setMaxTotal(getPerUserMaxTotal(userName));
-        pool.setMaxWaitMillis(getPerUserMaxWaitMillis(userName));
-        pool.setMinEvictableIdleTimeMillis(getPerUserMinEvictableIdleTimeMillis(userName));
+        pool.setMaxWait(Duration.ofMillis(getPerUserMaxWaitMillis(userName)));
+        pool.setMinEvictableIdle(Duration.ofMillis(getPerUserMinEvictableIdleTimeMillis(userName)));
         pool.setMinIdle(getPerUserMinIdle(userName));
         pool.setNumTestsPerEvictionRun(getPerUserNumTestsPerEvictionRun(userName));
-        pool.setSoftMinEvictableIdleTimeMillis(getPerUserSoftMinEvictableIdleTimeMillis(userName));
+        pool.setSoftMinEvictableIdle(Duration.ofMillis(getPerUserSoftMinEvictableIdleTimeMillis(userName)));
         pool.setTestOnCreate(getPerUserTestOnCreate(userName));
         pool.setTestOnBorrow(getPerUserTestOnBorrow(userName));
         pool.setTestOnReturn(getPerUserTestOnReturn(userName));
         pool.setTestWhileIdle(getPerUserTestWhileIdle(userName));
-        pool.setTimeBetweenEvictionRunsMillis(getPerUserTimeBetweenEvictionRunsMillis(userName));
+        pool.setTimeBetweenEvictionRuns(Duration.ofMillis(getPerUserTimeBetweenEvictionRunsMillis(userName)));
 
         pool.setSwallowedExceptionListener(new SwallowedExceptionLogger(log));
 
diff --git a/src/main/java/org/apache/commons/dbcp2/datasources/PerUserPoolDataSourceFactory.java b/src/main/java/org/apache/commons/dbcp2/datasources/PerUserPoolDataSourceFactory.java
index 9960851..6e5d480 100644
--- a/src/main/java/org/apache/commons/dbcp2/datasources/PerUserPoolDataSourceFactory.java
+++ b/src/main/java/org/apache/commons/dbcp2/datasources/PerUserPoolDataSourceFactory.java
@@ -18,6 +18,7 @@
 package org.apache.commons.dbcp2.datasources;
 
 import java.io.IOException;
+import java.time.Duration;
 import java.util.Map;
 
 import javax.naming.RefAddr;
@@ -35,54 +36,54 @@ public class PerUserPoolDataSourceFactory extends InstanceKeyDataSourceFactory {
     @Override
     protected InstanceKeyDataSource getNewInstance(final Reference ref) throws IOException, ClassNotFoundException {
         final PerUserPoolDataSource pupds = new PerUserPoolDataSource();
-        RefAddr ra = ref.get("defaultMaxTotal");
-        if (ra != null && ra.getContent() != null) {
-            pupds.setDefaultMaxTotal(Integer.parseInt(ra.getContent().toString()));
+        RefAddr refAddr = ref.get("defaultMaxTotal");
+        if (refAddr != null && refAddr.getContent() != null) {
+            pupds.setDefaultMaxTotal(parseInt(refAddr));
         }
 
-        ra = ref.get("defaultMaxIdle");
-        if (ra != null && ra.getContent() != null) {
-            pupds.setDefaultMaxIdle(Integer.parseInt(ra.getContent().toString()));
+        refAddr = ref.get("defaultMaxIdle");
+        if (refAddr != null && refAddr.getContent() != null) {
+            pupds.setDefaultMaxIdle(parseInt(refAddr));
         }
 
-        ra = ref.get("defaultMaxWaitMillis");
-        if (ra != null && ra.getContent() != null) {
-            pupds.setDefaultMaxWaitMillis(Integer.parseInt(ra.getContent().toString()));
+        refAddr = ref.get("defaultMaxWaitMillis");
+        if (refAddr != null && refAddr.getContent() != null) {
+            pupds.setDefaultMaxWait(Duration.ofMillis(parseInt(refAddr)));
         }
 
-        ra = ref.get("perUserDefaultAutoCommit");
-        if (ra != null && ra.getContent() != null) {
-            final byte[] serialized = (byte[]) ra.getContent();
+        refAddr = ref.get("perUserDefaultAutoCommit");
+        if (refAddr != null && refAddr.getContent() != null) {
+            final byte[] serialized = (byte[]) refAddr.getContent();
             pupds.setPerUserDefaultAutoCommit((Map<String, Boolean>) deserialize(serialized));
         }
 
-        ra = ref.get("perUserDefaultTransactionIsolation");
-        if (ra != null && ra.getContent() != null) {
-            final byte[] serialized = (byte[]) ra.getContent();
+        refAddr = ref.get("perUserDefaultTransactionIsolation");
+        if (refAddr != null && refAddr.getContent() != null) {
+            final byte[] serialized = (byte[]) refAddr.getContent();
             pupds.setPerUserDefaultTransactionIsolation((Map<String, Integer>) deserialize(serialized));
         }
 
-        ra = ref.get("perUserMaxTotal");
-        if (ra != null && ra.getContent() != null) {
-            final byte[] serialized = (byte[]) ra.getContent();
+        refAddr = ref.get("perUserMaxTotal");
+        if (refAddr != null && refAddr.getContent() != null) {
+            final byte[] serialized = (byte[]) refAddr.getContent();
             pupds.setPerUserMaxTotal((Map<String, Integer>) deserialize(serialized));
         }
 
-        ra = ref.get("perUserMaxIdle");
-        if (ra != null && ra.getContent() != null) {
-            final byte[] serialized = (byte[]) ra.getContent();
+        refAddr = ref.get("perUserMaxIdle");
+        if (refAddr != null && refAddr.getContent() != null) {
+            final byte[] serialized = (byte[]) refAddr.getContent();
             pupds.setPerUserMaxIdle((Map<String, Integer>) deserialize(serialized));
         }
 
-        ra = ref.get("perUserMaxWaitMillis");
-        if (ra != null && ra.getContent() != null) {
-            final byte[] serialized = (byte[]) ra.getContent();
+        refAddr = ref.get("perUserMaxWaitMillis");
+        if (refAddr != null && refAddr.getContent() != null) {
+            final byte[] serialized = (byte[]) refAddr.getContent();
             pupds.setPerUserMaxWaitMillis((Map<String, Long>) deserialize(serialized));
         }
 
-        ra = ref.get("perUserDefaultReadOnly");
-        if (ra != null && ra.getContent() != null) {
-            final byte[] serialized = (byte[]) ra.getContent();
+        refAddr = ref.get("perUserDefaultReadOnly");
+        if (refAddr != null && refAddr.getContent() != null) {
+            final byte[] serialized = (byte[]) refAddr.getContent();
             pupds.setPerUserDefaultReadOnly((Map<String, Boolean>) deserialize(serialized));
         }
         return pupds;
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 a59f829..b863c99 100644
--- a/src/main/java/org/apache/commons/dbcp2/datasources/SharedPoolDataSource.java
+++ b/src/main/java/org/apache/commons/dbcp2/datasources/SharedPoolDataSource.java
@@ -21,6 +21,7 @@ import java.io.IOException;
 import java.io.ObjectInputStream;
 import java.sql.Connection;
 import java.sql.SQLException;
+import java.time.Duration;
 
 import javax.naming.NamingException;
 import javax.naming.Reference;
@@ -180,15 +181,15 @@ public class SharedPoolDataSource extends InstanceKeyDataSource {
         config.setMaxTotal(getMaxTotal());
         config.setMaxTotalPerKey(getDefaultMaxTotal());
         config.setMaxWait(getDefaultMaxWait());
-        config.setMinEvictableIdleTimeMillis(getDefaultMinEvictableIdleTimeMillis());
+        config.setMinEvictableIdleTime(Duration.ofMillis(getDefaultMinEvictableIdleTimeMillis()));
         config.setMinIdlePerKey(getDefaultMinIdle());
         config.setNumTestsPerEvictionRun(getDefaultNumTestsPerEvictionRun());
-        config.setSoftMinEvictableIdleTimeMillis(getDefaultSoftMinEvictableIdleTimeMillis());
+        config.setSoftMinEvictableIdleTime(Duration.ofMillis(getDefaultSoftMinEvictableIdleTimeMillis()));
         config.setTestOnCreate(getDefaultTestOnCreate());
         config.setTestOnBorrow(getDefaultTestOnBorrow());
         config.setTestOnReturn(getDefaultTestOnReturn());
         config.setTestWhileIdle(getDefaultTestWhileIdle());
-        config.setTimeBetweenEvictionRunsMillis(getDefaultTimeBetweenEvictionRunsMillis());
+        config.setTimeBetweenEvictionRuns(Duration.ofMillis(getDefaultTimeBetweenEvictionRunsMillis()));
 
         final KeyedObjectPool<UserPassKey, PooledConnectionAndInfo> tmpPool = new GenericKeyedObjectPool<>(factory,
             config);