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 c116656 Add and reuse CPDSConnectionFactory.setMaxConnLifetime(Duration), deprecate setMaxConnLifetimeMillis(long). c116656 is described below commit c11665673c39adbd0b836034d9c22809f4b5704c Author: Gary Gregory <gardgreg...@gmail.com> AuthorDate: Sun Jun 6 10:06:07 2021 -0400 Add and reuse CPDSConnectionFactory.setMaxConnLifetime(Duration), deprecate setMaxConnLifetimeMillis(long). Add and reuse KeyedCPDSConnectionFactory.setMaxConnLifetime(Duration), deprecate setMaxConnLifetimeMillis(long). --- src/changes/changes.xml | 10 ++++- .../dbcp2/datasources/CPDSConnectionFactory.java | 33 ++++++++++++----- .../dbcp2/datasources/InstanceKeyDataSource.java | 43 ++++++++++++++++++++-- .../datasources/KeyedCPDSConnectionFactory.java | 31 ++++++++++++---- .../dbcp2/datasources/PerUserPoolDataSource.java | 6 +-- .../dbcp2/datasources/SharedPoolDataSource.java | 6 +-- 6 files changed, 100 insertions(+), 29 deletions(-) diff --git a/src/changes/changes.xml b/src/changes/changes.xml index e89518e..9285023 100644 --- a/src/changes/changes.xml +++ b/src/changes/changes.xml @@ -69,10 +69,16 @@ The <action> type attribute can be add,update,fix,remove. Add and reuse DataSourceMXBean. </action> <action dev="ggregory" type="add" due-to="Gary Gregory"> - Add and reuse DriverAdapterCPDS.{get|set}DurationBetweenEvictionRuns(), deprecate {get|set}TimeBetweenEvictionRunsMillis(). + Add and reuse DriverAdapterCPDS.{get|set}DurationBetweenEvictionRuns(), deprecate {get|set}TimeBetweenEvictionRunsMillis(long). </action> <action dev="ggregory" type="add" due-to="Gary Gregory"> - Add and reuse DriverAdapterCPDS.{get|set}MinEvictableIdleDuration(), deprecate {get|set}MinEvictableIdleTimeMillis(). + Add and reuse DriverAdapterCPDS.{get|set}MinEvictableIdleDuration(), deprecate {get|set}MinEvictableIdleTimeMillis(int). + </action> + <action dev="ggregory" type="add" due-to="Gary Gregory"> + Add and reuse CPDSConnectionFactory.setMaxConnLifetime(Duration), deprecate setMaxConnLifetimeMillis(long). + </action> + <action dev="ggregory" type="add" due-to="Gary Gregory"> + Add and reuse KeyedCPDSConnectionFactory.setMaxConnLifetime(Duration), deprecate setMaxConnLifetimeMillis(long). </action> <!-- FIXES --> <action dev="ggregory" type="fix" issue="DBCP-569" due-to="Florent Guillaume"> 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 29df4a0..e2fc3e6 100644 --- a/src/main/java/org/apache/commons/dbcp2/datasources/CPDSConnectionFactory.java +++ b/src/main/java/org/apache/commons/dbcp2/datasources/CPDSConnectionFactory.java @@ -20,6 +20,7 @@ import java.sql.Connection; import java.sql.ResultSet; import java.sql.SQLException; import java.sql.Statement; +import java.time.Duration; import java.util.Collections; import java.util.Map; import java.util.Set; @@ -53,7 +54,7 @@ class CPDSConnectionFactory private ObjectPool<PooledConnectionAndInfo> pool; private final String userName; private char[] userPassword; - private long maxConnLifetimeMillis = -1; + private Duration maxConnLifetime = Duration.ofMillis(-1); /** * Map of PooledConnections for which close events are ignored. Connections are muted when they are being validated. @@ -357,11 +358,25 @@ class CPDSConnectionFactory * Sets the maximum lifetime in milliseconds of a connection after which the connection will always fail activation, * passivation and validation. * + * @param maxConnLifetime + * A value of zero or less indicates an infinite lifetime. The default value is -1 milliseconds. + * @since 2.9.0 + */ + public void setMaxConnLifetime(final Duration maxConnLifetime) { + this.maxConnLifetime = maxConnLifetime; + } + + /** + * Sets the maximum lifetime in milliseconds of a connection after which the connection will always fail activation, + * passivation and validation. + * * @param maxConnLifetimeMillis * A value of zero or less indicates an infinite lifetime. The default value is -1. + * @deprecated Use {@link #setMaxConnLifetime(Duration)}. */ + @Deprecated public void setMaxConnLifetimeMillis(final long maxConnLifetimeMillis) { - this.maxConnLifetimeMillis = maxConnLifetimeMillis; + setMaxConnLifetime(Duration.ofMillis(maxConnLifetimeMillis)); } /** @@ -382,12 +397,12 @@ class CPDSConnectionFactory } } - private void validateLifetime(final PooledObject<PooledConnectionAndInfo> p) throws Exception { - if (maxConnLifetimeMillis > 0) { - final long lifetimeMillis = System.currentTimeMillis() - p.getCreateTime(); - if (lifetimeMillis > maxConnLifetimeMillis) { - throw new Exception(Utils.getMessage("connectionFactory.lifetimeExceeded", lifetimeMillis, - maxConnLifetimeMillis)); + private void validateLifetime(final PooledObject<PooledConnectionAndInfo> pooledObject) throws Exception { + if (maxConnLifetime.compareTo(Duration.ZERO) > 0) { + final long lifetimeMillis = System.currentTimeMillis() - pooledObject.getCreateTime(); + if (lifetimeMillis > maxConnLifetime.toMillis()) { + throw new Exception( + Utils.getMessage("connectionFactory.lifetimeExceeded", lifetimeMillis, maxConnLifetime)); } } } @@ -409,7 +424,7 @@ class CPDSConnectionFactory builder.append(", pool="); builder.append(pool); builder.append(", maxConnLifetimeMillis="); - builder.append(maxConnLifetimeMillis); + builder.append(maxConnLifetime); builder.append(", validatingSet="); builder.append(validatingSet); builder.append(", pcMap="); 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 f20b955..1a64425 100644 --- a/src/main/java/org/apache/commons/dbcp2/datasources/InstanceKeyDataSource.java +++ b/src/main/java/org/apache/commons/dbcp2/datasources/InstanceKeyDataSource.java @@ -24,6 +24,7 @@ import java.nio.charset.StandardCharsets; import java.sql.Connection; import java.sql.SQLException; import java.sql.SQLFeatureNotSupportedException; +import java.time.Duration; import java.util.Properties; import java.util.logging.Logger; @@ -130,7 +131,7 @@ public abstract class InstanceKeyDataSource implements DataSource, Referenceable private String validationQuery; private int validationQueryTimeoutSeconds = -1; private boolean rollbackAfterValidation; - private long maxConnLifetimeMillis = -1; + private Duration maxConnLifetimeMillis = Duration.ofMillis(-1); // Connection properties private Boolean defaultAutoCommit; @@ -848,14 +849,46 @@ public abstract class InstanceKeyDataSource implements DataSource, Referenceable } /** - * Returns the maximum permitted lifetime of a connection in milliseconds. A value of zero or less indicates an + * Gets the maximum permitted lifetime of a connection. A value of zero or less indicates an + * infinite lifetime. + * + * @return The maximum permitted lifetime of a connection. A value of zero or less indicates an + * infinite lifetime. + */ + public Duration getMaxConnLifetime() { + return maxConnLifetimeMillis; + } + + /** + * Gets the maximum permitted lifetime of a connection in milliseconds. A value of zero or less indicates an * infinite lifetime. * * @return The maximum permitted lifetime of a connection in milliseconds. A value of zero or less indicates an * infinite lifetime. + * @deprecated Use {@link #getMaxConnLifetime()}. */ public long getMaxConnLifetimeMillis() { - return maxConnLifetimeMillis; + return maxConnLifetimeMillis.toMillis(); + } + + /** + * <p> + * Sets the maximum permitted lifetime of a connection. A value of zero or less indicates an + * infinite lifetime. + * </p> + * <p> + * Note: this method currently has no effect once the pool has been initialized. The pool is initialized the first + * time one of the following methods is invoked: <code>getConnection, setLogwriter, + * setLoginTimeout, getLoginTimeout, getLogWriter.</code> + * </p> + * + * @param maxConnLifetimeMillis + * The maximum permitted lifetime of a connection. A value of zero or less indicates an + * infinite lifetime. + * @since 2.9.0 + */ + public void setMaxConnLifetime(final Duration maxConnLifetimeMillis) { + this.maxConnLifetimeMillis = maxConnLifetimeMillis; } /** @@ -872,9 +905,11 @@ public abstract class InstanceKeyDataSource implements DataSource, Referenceable * @param maxConnLifetimeMillis * The maximum permitted lifetime of a connection in milliseconds. A value of zero or less indicates an * infinite lifetime. + * @deprecated Use {@link #setMaxConnLifetime(Duration)}. */ + @Deprecated public void setMaxConnLifetimeMillis(final long maxConnLifetimeMillis) { - this.maxConnLifetimeMillis = maxConnLifetimeMillis; + setMaxConnLifetime(Duration.ofMillis(maxConnLifetimeMillis)); } /** 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 e289d2e..f57b82b 100644 --- a/src/main/java/org/apache/commons/dbcp2/datasources/KeyedCPDSConnectionFactory.java +++ b/src/main/java/org/apache/commons/dbcp2/datasources/KeyedCPDSConnectionFactory.java @@ -21,6 +21,7 @@ import java.sql.Connection; import java.sql.ResultSet; import java.sql.SQLException; import java.sql.Statement; +import java.time.Duration; import java.util.Collections; import java.util.Map; import java.util.Set; @@ -54,7 +55,7 @@ class KeyedCPDSConnectionFactory implements KeyedPooledObjectFactory<UserPassKey private final int validationQueryTimeoutSeconds; private final boolean rollbackAfterValidation; private KeyedObjectPool<UserPassKey, PooledConnectionAndInfo> pool; - private long maxConnLifetimeMillis = -1; + private Duration maxConnLifetime = Duration.ofMillis(-1); /** * Map of PooledConnections for which close events are ignored. Connections are muted when they are being validated. @@ -102,10 +103,10 @@ class KeyedCPDSConnectionFactory implements KeyedPooledObjectFactory<UserPassKey } /** - * Creates a new {@link PooledConnectionAndInfo} from the given {@link UserPassKey}. + * Creates a new {@code PooledConnectionAndInfo} from the given {@code UserPassKey}. * * @param upkey - * {@link UserPassKey} containing user credentials + * {@code UserPassKey} containing user credentials * @throws SQLException * if the connection could not be created. * @see org.apache.commons.pool2.KeyedPooledObjectFactory#makeObject(java.lang.Object) @@ -309,14 +310,28 @@ class KeyedCPDSConnectionFactory implements KeyedPooledObjectFactory<UserPassKey } /** + * Sets the maximum lifetime of a connection after which the connection will always fail activation, + * passivation and validation. + * + * @param maxConnLifetimeMillis + * A value of zero or less indicates an infinite lifetime. The default value is -1 milliseconds. + * @since 2.9.0 + */ + public void setMaxConnLifetime(final Duration maxConnLifetimeMillis) { + this.maxConnLifetime = maxConnLifetimeMillis; + } + + /** * Sets the maximum lifetime in milliseconds of a connection after which the connection will always fail activation, * passivation and validation. * * @param maxConnLifetimeMillis * A value of zero or less indicates an infinite lifetime. The default value is -1. + * @deprecated Use {@link #setMaxConnLifetime(Duration)}. */ + @Deprecated public void setMaxConnLifetimeMillis(final long maxConnLifetimeMillis) { - this.maxConnLifetimeMillis = maxConnLifetimeMillis; + setMaxConnLifetime(Duration.ofMillis(maxConnLifetimeMillis)); } /** @@ -333,11 +348,11 @@ class KeyedCPDSConnectionFactory implements KeyedPooledObjectFactory<UserPassKey } private void validateLifetime(final PooledObject<PooledConnectionAndInfo> p) throws Exception { - if (maxConnLifetimeMillis > 0) { + if (maxConnLifetime.compareTo(Duration.ZERO) > 0) { final long lifetimeMillis = System.currentTimeMillis() - p.getCreateTime(); - if (lifetimeMillis > maxConnLifetimeMillis) { - throw new Exception(Utils.getMessage("connectionFactory.lifetimeExceeded", lifetimeMillis, - maxConnLifetimeMillis)); + if (lifetimeMillis > maxConnLifetime.toMillis()) { + throw new Exception( + Utils.getMessage("connectionFactory.lifetimeExceeded", lifetimeMillis, maxConnLifetime)); } } } 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 40d1b94..f2779fb 100644 --- a/src/main/java/org/apache/commons/dbcp2/datasources/PerUserPoolDataSource.java +++ b/src/main/java/org/apache/commons/dbcp2/datasources/PerUserPoolDataSource.java @@ -623,7 +623,7 @@ public class PerUserPoolDataSource extends InstanceKeyDataSource { } private synchronized void registerPool(final String userName, final String password) - throws NamingException, SQLException { + throws NamingException, SQLException { final ConnectionPoolDataSource cpds = testCPDS(userName, password); @@ -631,8 +631,8 @@ public class PerUserPoolDataSource extends InstanceKeyDataSource { // the factory with the pool, so we do not have to do so // explicitly) final CPDSConnectionFactory factory = new CPDSConnectionFactory(cpds, getValidationQuery(), - getValidationQueryTimeout(), isRollbackAfterValidation(), userName, password); - factory.setMaxConnLifetimeMillis(getMaxConnLifetimeMillis()); + getValidationQueryTimeout(), isRollbackAfterValidation(), userName, password); + factory.setMaxConnLifetime(getMaxConnLifetime()); // Create an object pool to contain our PooledConnections final GenericObjectPool<PooledConnectionAndInfo> pool = new GenericObjectPool<>(factory); 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 95b8384..96f831c 100644 --- a/src/main/java/org/apache/commons/dbcp2/datasources/SharedPoolDataSource.java +++ b/src/main/java/org/apache/commons/dbcp2/datasources/SharedPoolDataSource.java @@ -164,8 +164,8 @@ public class SharedPoolDataSource extends InstanceKeyDataSource { // Create an object pool to contain our PooledConnections factory = new KeyedCPDSConnectionFactory(cpds, getValidationQuery(), getValidationQueryTimeout(), - isRollbackAfterValidation()); - factory.setMaxConnLifetimeMillis(getMaxConnLifetimeMillis()); + isRollbackAfterValidation()); + factory.setMaxConnLifetime(getMaxConnLifetime()); final GenericKeyedObjectPoolConfig<PooledConnectionAndInfo> config = new GenericKeyedObjectPoolConfig<>(); config.setBlockWhenExhausted(getDefaultBlockWhenExhausted()); @@ -186,7 +186,7 @@ public class SharedPoolDataSource extends InstanceKeyDataSource { config.setTimeBetweenEvictionRunsMillis(getDefaultTimeBetweenEvictionRunsMillis()); final KeyedObjectPool<UserPassKey, PooledConnectionAndInfo> tmpPool = new GenericKeyedObjectPool<>(factory, - config); + config); factory.setPool(tmpPool); pool = tmpPool; }