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/06/05 13:30:23 UTC

[commons-dbcp] branch master updated: Add and reuse DriverAdapterCPDS.{get|set}DurationBetweenEvictionRuns(), deprecate {get|set}TimeBetweenEvictionRunsMillis().

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 2153203  Add and reuse DriverAdapterCPDS.{get|set}DurationBetweenEvictionRuns(), deprecate {get|set}TimeBetweenEvictionRunsMillis().
2153203 is described below

commit 21532035d5a3e9443226523a3da0f4fb62dafacd
Author: Gary Gregory <ga...@gmail.com>
AuthorDate: Sat Jun 5 09:30:18 2021 -0400

    Add and reuse DriverAdapterCPDS.{get|set}DurationBetweenEvictionRuns(),
    deprecate {get|set}TimeBetweenEvictionRunsMillis().
---
 src/changes/changes.xml                            | 11 +++--
 .../dbcp2/cpdsadapter/DriverAdapterCPDS.java       | 52 ++++++++++++++++++----
 .../dbcp2/datasources/InstanceKeyDataSource.java   | 10 ++---
 .../dbcp2/cpdsadapter/TestDriverAdapterCPDS.java   |  1 +
 4 files changed, 56 insertions(+), 18 deletions(-)

diff --git a/src/changes/changes.xml b/src/changes/changes.xml
index bb61802..499bbd7 100644
--- a/src/changes/changes.xml
+++ b/src/changes/changes.xml
@@ -67,13 +67,16 @@ The <action> type attribute can be add,update,fix,remove.
       </action>
       <action dev="ggregory" type="add" due-to="Frank Gasdorf, Gary Gregory">
          Add and reuse DataSourceMXBean.
-      </action>      
+      </action>
+      <action dev="ggregory" type="add" due-to="Frank Gasdorf, Gary Gregory">
+         Add and reuse DriverAdapterCPDS.{get|set}DurationBetweenEvictionRuns(), deprecate {get|set}TimeBetweenEvictionRunsMillis().
+      </action>
       <!--  FIXES -->
       <action dev="ggregory" type="fix" issue="DBCP-569" due-to="Florent Guillaume">
-         Fix test random failure on TestSynchronizationOrder.testInterposedSynchronization, #84.
+        Fix test random failure on TestSynchronizationOrder.testInterposedSynchronization, #84.
       </action>
       <action dev="ggregory" type="fix" issue="DBCP-568" due-to="Florent Guillaume">
-         ManagedConnection must clear its cached state after transaction completes, #75.
+        ManagedConnection must clear its cached state after transaction completes, #75.
       </action>
       <action dev="ggregory" type="fix" due-to="Arturo Bernal">
         Minor Improvements #78.
@@ -121,7 +124,7 @@ The <action> type attribute can be add,update,fix,remove.
         org.apache.commons.dbcp2.PStmtKey.getColumnNames() may expose internal representation by returning PStmtKey.columnNames.
       </action>
       <action issue="DBCP-578" dev="ggregory" type="fix" due-to="Arturo Bernal">
-        Use Collections.synchronizedList Instead Of Vector #101.
+        Use Collections.synchronizedList() Instead Of Vector #101.
       </action>
       <action issue="DBCP-576" dev="ggregory" type="fix" due-to="Arturo Bernal">
         Simplify and inline variables #99.
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 e11dc40..ce1a9a9 100644
--- a/src/main/java/org/apache/commons/dbcp2/cpdsadapter/DriverAdapterCPDS.java
+++ b/src/main/java/org/apache/commons/dbcp2/cpdsadapter/DriverAdapterCPDS.java
@@ -22,6 +22,7 @@ import java.io.Serializable;
 import java.sql.DriverManager;
 import java.sql.SQLException;
 import java.sql.SQLFeatureNotSupportedException;
+import java.time.Duration;
 import java.util.Hashtable;
 import java.util.Properties;
 import java.util.logging.Logger;
@@ -109,10 +110,11 @@ public class DriverAdapterCPDS implements ConnectionPoolDataSource, Referenceabl
 
     /** Log stream. NOT USED */
     private transient PrintWriter logWriter;
+
     // PreparedStatement pool properties
     private boolean poolPreparedStatements;
     private int maxIdle = 10;
-    private long timeBetweenEvictionRunsMillis = BaseObjectPoolConfig.DEFAULT_TIME_BETWEEN_EVICTION_RUNS_MILLIS;
+    private Duration durationBetweenEvictionRuns = BaseObjectPoolConfig.DEFAULT_TIME_BETWEEN_EVICTION_RUNS;
     private int numTestsPerEvictionRun = -1;
     private int minEvictableIdleTimeMillis = -1;
 
@@ -130,7 +132,7 @@ public class DriverAdapterCPDS implements ConnectionPoolDataSource, Referenceabl
     private boolean accessToUnderlyingConnectionAllowed;
 
     /**
-     * Default no-arg constructor for Serialization
+     * Default no-argument constructor for Serialization
      */
     public DriverAdapterCPDS() {
     }
@@ -158,8 +160,8 @@ public class DriverAdapterCPDS implements ConnectionPoolDataSource, Referenceabl
     }
 
     /**
-     * Gets the value of description. This property is here for use by the code which will deploy this datasource. It is
-     * not used internally.
+     * Gets the value of description. This property is here for use by the code which will deploy this data source. It
+     * is not used internally.
      *
      * @return value of description, may be null.
      * @see #setDescription(String)
@@ -205,7 +207,7 @@ public class DriverAdapterCPDS implements ConnectionPoolDataSource, Referenceabl
      * @return the value of maxIdle
      */
     public int getMaxIdle() {
-        return this.maxIdle;
+        return maxIdle;
     }
 
     /**
@@ -388,7 +390,7 @@ public class DriverAdapterCPDS implements ConnectionPoolDataSource, Referenceabl
             if (getMaxPreparedStatements() <= 0) {
                 // Since there is no limit, create a prepared statement pool with an eviction thread;
                 // evictor settings are the same as the connection pool settings.
-                config.setTimeBetweenEvictionRunsMillis(getTimeBetweenEvictionRunsMillis());
+                config.setTimeBetweenEvictionRuns(getDurationBetweenEvictionRuns());
                 config.setNumTestsPerEvictionRun(getNumTestsPerEvictionRun());
                 config.setMinEvictableIdleTimeMillis(getMinEvictableIdleTimeMillis());
             } else {
@@ -425,6 +427,7 @@ public class DriverAdapterCPDS implements ConnectionPoolDataSource, Referenceabl
 
         ref.add(new StringRefAddr("poolPreparedStatements", String.valueOf(isPoolPreparedStatements())));
         ref.add(new StringRefAddr("maxIdle", String.valueOf(getMaxIdle())));
+        ref.add(new StringRefAddr("durationBetweenEvictionRuns", String.valueOf(getDurationBetweenEvictionRuns())));
         ref.add(new StringRefAddr("timeBetweenEvictionRunsMillis", String.valueOf(getTimeBetweenEvictionRunsMillis())));
         ref.add(new StringRefAddr("numTestsPerEvictionRun", String.valueOf(getNumTestsPerEvictionRun())));
         ref.add(new StringRefAddr("minEvictableIdleTimeMillis", String.valueOf(getMinEvictableIdleTimeMillis())));
@@ -438,14 +441,28 @@ public class DriverAdapterCPDS implements ConnectionPoolDataSource, Referenceabl
     }
 
     /**
+     * Gets the duration to sleep between runs of the idle object evictor thread. When non-positive, no
+     * idle object evictor thread will be run.
+     *
+     * @return the value of the evictor thread timer
+     * @see #setDurationBetweenEvictionRuns(Duration)
+     * @since 2.9.0
+     */
+    public Duration getDurationBetweenEvictionRuns() {
+        return durationBetweenEvictionRuns;
+    }
+
+    /**
      * Gets the number of milliseconds to sleep between runs of the idle object evictor thread. When non-positive, no
      * idle object evictor thread will be run.
      *
      * @return the value of the evictor thread timer
      * @see #setTimeBetweenEvictionRunsMillis(long)
+     * @deprecated Use {@link #getDurationBetweenEvictionRuns()}.
      */
+    @Deprecated
     public long getTimeBetweenEvictionRunsMillis() {
-        return timeBetweenEvictionRunsMillis;
+        return durationBetweenEvictionRuns.toMillis();
     }
 
     /**
@@ -656,6 +673,21 @@ public class DriverAdapterCPDS implements ConnectionPoolDataSource, Referenceabl
     }
 
     /**
+     * Sets the duration to sleep between runs of the idle object evictor thread. When non-positive, no
+     * idle object evictor thread will be run.
+     *
+     * @param durationBetweenEvictionRuns The duration to sleep between runs of the idle object evictor
+     *        thread. When non-positive, no idle object evictor thread will be run.
+     * @see #getDurationBetweenEvictionRuns()
+     * @throws IllegalStateException if {@link #getPooledConnection()} has been called
+     * @since 2.9.0
+     */
+    public void setDurationBetweenEvictionRuns(final Duration durationBetweenEvictionRuns) {
+        assertInitializationAllowed();
+        this.durationBetweenEvictionRuns = durationBetweenEvictionRuns;
+    }
+
+    /**
      * Sets the number of milliseconds to sleep between runs of the idle object evictor thread. When non-positive, no
      * idle object evictor thread will be run.
      *
@@ -663,10 +695,12 @@ public class DriverAdapterCPDS implements ConnectionPoolDataSource, Referenceabl
      *        thread. When non-positive, no idle object evictor thread will be run.
      * @see #getTimeBetweenEvictionRunsMillis()
      * @throws IllegalStateException if {@link #getPooledConnection()} has been called
+     * @deprecated Use {@link #setDurationBetweenEvictionRuns(Duration)}.
      */
+    @Deprecated
     public void setTimeBetweenEvictionRunsMillis(final long timeBetweenEvictionRunsMillis) {
         assertInitializationAllowed();
-        this.timeBetweenEvictionRunsMillis = timeBetweenEvictionRunsMillis;
+        this.durationBetweenEvictionRuns = Duration.ofMillis(timeBetweenEvictionRunsMillis);
     }
 
     /**
@@ -715,7 +749,7 @@ public class DriverAdapterCPDS implements ConnectionPoolDataSource, Referenceabl
         builder.append(", maxIdle=");
         builder.append(maxIdle);
         builder.append(", timeBetweenEvictionRunsMillis=");
-        builder.append(timeBetweenEvictionRunsMillis);
+        builder.append(durationBetweenEvictionRuns);
         builder.append(", numTestsPerEvictionRun=");
         builder.append(numTestsPerEvictionRun);
         builder.append(", minEvictableIdleTimeMillis=");
diff --git a/src/main/java/org/apache/commons/dbcp2/datasources/InstanceKeyDataSource.java b/src/main/java/org/apache/commons/dbcp2/datasources/InstanceKeyDataSource.java
index b97be75..f20b955 100644
--- a/src/main/java/org/apache/commons/dbcp2/datasources/InstanceKeyDataSource.java
+++ b/src/main/java/org/apache/commons/dbcp2/datasources/InstanceKeyDataSource.java
@@ -960,14 +960,14 @@ public abstract class InstanceKeyDataSource implements DataSource, Referenceable
             }
         }
 
-        final Connection con = info.getPooledConnection().getConnection();
+        final Connection connection = info.getPooledConnection().getConnection();
         try {
-            setupDefaults(con, userName);
-            con.clearWarnings();
-            return con;
+            setupDefaults(connection, userName);
+            connection.clearWarnings();
+            return connection;
         } catch (final SQLException ex) {
             try {
-                con.close();
+                connection.close();
             } catch (final Exception exc) {
                 getLogWriter().println("ignoring exception during close: " + exc);
             }
diff --git a/src/test/java/org/apache/commons/dbcp2/cpdsadapter/TestDriverAdapterCPDS.java b/src/test/java/org/apache/commons/dbcp2/cpdsadapter/TestDriverAdapterCPDS.java
index 865b038..c5fd358 100644
--- a/src/test/java/org/apache/commons/dbcp2/cpdsadapter/TestDriverAdapterCPDS.java
+++ b/src/test/java/org/apache/commons/dbcp2/cpdsadapter/TestDriverAdapterCPDS.java
@@ -333,6 +333,7 @@ public class TestDriverAdapterCPDS {
         pcds.setMaxIdle(100);
         assertEquals(100, pcds.getMaxIdle());
         pcds.setTimeBetweenEvictionRunsMillis(100);
+        assertEquals(100, pcds.getDurationBetweenEvictionRuns().toMillis());
         assertEquals(100, pcds.getTimeBetweenEvictionRunsMillis());
         pcds.setNumTestsPerEvictionRun(1);
         assertEquals(1, pcds.getNumTestsPerEvictionRun());