You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@commons.apache.org by ma...@apache.org on 2013/07/24 22:06:43 UTC
svn commit: r1506687 [1/2] - in
/commons/proper/dbcp/trunk/src/java/org/apache/commons/dbcp2: ./
cpdsadapter/ datasources/ managed/
Author: markt
Date: Wed Jul 24 20:06:42 2013
New Revision: 1506687
URL: http://svn.apache.org/r1506687
Log:
Update for Pool-2 API changes.
Modified:
commons/proper/dbcp/trunk/src/java/org/apache/commons/dbcp2/BasicDataSource.java
commons/proper/dbcp/trunk/src/java/org/apache/commons/dbcp2/PoolableConnectionFactory.java
commons/proper/dbcp/trunk/src/java/org/apache/commons/dbcp2/PoolingConnection.java
commons/proper/dbcp/trunk/src/java/org/apache/commons/dbcp2/cpdsadapter/PooledConnectionImpl.java
commons/proper/dbcp/trunk/src/java/org/apache/commons/dbcp2/datasources/CPDSConnectionFactory.java
commons/proper/dbcp/trunk/src/java/org/apache/commons/dbcp2/datasources/KeyedCPDSConnectionFactory.java
commons/proper/dbcp/trunk/src/java/org/apache/commons/dbcp2/managed/PoolableManagedConnectionFactory.java
Modified: commons/proper/dbcp/trunk/src/java/org/apache/commons/dbcp2/BasicDataSource.java
URL: http://svn.apache.org/viewvc/commons/proper/dbcp/trunk/src/java/org/apache/commons/dbcp2/BasicDataSource.java?rev=1506687&r1=1506686&r2=1506687&view=diff
==============================================================================
--- commons/proper/dbcp/trunk/src/java/org/apache/commons/dbcp2/BasicDataSource.java (original)
+++ commons/proper/dbcp/trunk/src/java/org/apache/commons/dbcp2/BasicDataSource.java Wed Jul 24 20:06:42 2013
@@ -5,9 +5,9 @@
* The ASF licenses this file to You under the Apache License, Version 2.0
* (the "License"); you may not use this file except in compliance with
* the License. You may obtain a copy of the License at
- *
+ *
* http://www.apache.org/licenses/LICENSE-2.0
- *
+ *
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
@@ -33,6 +33,7 @@ import java.sql.SQLFeatureNotSupportedEx
import javax.sql.DataSource;
+import org.apache.commons.pool2.PooledObject;
import org.apache.commons.pool2.impl.AbandonedConfig;
import org.apache.commons.pool2.impl.GenericKeyedObjectPoolConfig;
import org.apache.commons.pool2.impl.GenericObjectPool;
@@ -44,7 +45,7 @@ import org.apache.commons.pool2.impl.Gen
* configured via JavaBeans properties. This is not the only way to
* combine the <em>commons-dbcp</em> and <em>commons-pool</em> packages,
* but provides a "one stop shopping" solution for basic requirements.</p>
- *
+ *
* <p>Users extending this class should take care to use appropriate accessors
* rather than accessing protected fields directly to ensure thread-safety.</p>
*
@@ -54,7 +55,7 @@ import org.apache.commons.pool2.impl.Gen
* @version $Revision$ $Date$
*/
public class BasicDataSource implements DataSource {
-
+
static {
// Attempt to prevent deadlocks - see DBCP - 272
DriverManager.getDrivers();
@@ -69,7 +70,7 @@ public class BasicDataSource implements
/**
* Returns the default auto-commit property.
- *
+ *
* @return true if default auto-commit is enabled
*/
public boolean getDefaultAutoCommit() {
@@ -84,7 +85,7 @@ public class BasicDataSource implements
* initialized. The pool is initialized the first time one of the
* following methods is invoked: <code>getConnection, setLogwriter,
* setLoginTimeout, getLoginTimeout, getLogWriter.</code></p>
- *
+ *
* @param defaultAutoCommit default auto-commit value
*/
public void setDefaultAutoCommit(boolean defaultAutoCommit) {
@@ -100,7 +101,7 @@ public class BasicDataSource implements
/**
* Returns the default readOnly property.
- *
+ *
* @return true if connections are readOnly by default
*/
public boolean getDefaultReadOnly() {
@@ -118,7 +119,7 @@ public class BasicDataSource implements
* initialized. The pool is initialized the first time one of the
* following methods is invoked: <code>getConnection, setLogwriter,
* setLoginTimeout, getLoginTimeout, getLogWriter.</code></p>
- *
+ *
* @param defaultReadOnly default read-only value
*/
public void setDefaultReadOnly(boolean defaultReadOnly) {
@@ -134,7 +135,7 @@ public class BasicDataSource implements
/**
* Returns the default transaction isolation state of returned connections.
- *
+ *
* @return the default value for transaction isolation state
* @see Connection#getTransactionIsolation
*/
@@ -150,7 +151,7 @@ public class BasicDataSource implements
* initialized. The pool is initialized the first time one of the
* following methods is invoked: <code>getConnection, setLogwriter,
* setLoginTimeout, getLoginTimeout, getLogWriter.</code></p>
- *
+ *
* @param defaultTransactionIsolation the default transaction isolation
* state
* @see Connection#getTransactionIsolation
@@ -168,7 +169,7 @@ public class BasicDataSource implements
/**
* Returns the default catalog.
- *
+ *
* @return the default catalog
*/
public String getDefaultCatalog() {
@@ -182,7 +183,7 @@ public class BasicDataSource implements
* initialized. The pool is initialized the first time one of the
* following methods is invoked: <code>getConnection, setLogwriter,
* setLoginTimeout, getLoginTimeout, getLogWriter.</code></p>
- *
+ *
* @param defaultCatalog the default catalog
*/
public void setDefaultCatalog(String defaultCatalog) {
@@ -203,7 +204,7 @@ public class BasicDataSource implements
/**
* Returns the state caching flag.
- *
+ *
* @return the state caching flag
*/
public boolean getCacheState() {
@@ -212,7 +213,7 @@ public class BasicDataSource implements
/**
* Sets the state caching flag.
- *
+ *
* @param cacheState The new value for the state caching flag
*/
public void setCacheState(boolean cacheState) {
@@ -226,7 +227,7 @@ public class BasicDataSource implements
/**
* Returns the jdbc driver class name.
- *
+ *
* @return the jdbc driver class name
*/
public synchronized String getDriverClassName() {
@@ -240,7 +241,7 @@ public class BasicDataSource implements
* initialized. The pool is initialized the first time one of the
* following methods is invoked: <code>getConnection, setLogwriter,
* setLoginTimeout, getLoginTimeout, getLogWriter.</code></p>
- *
+ *
* @param driverClassName the class name of the jdbc driver
*/
public synchronized void setDriverClassName(String driverClassName) {
@@ -260,7 +261,7 @@ public class BasicDataSource implements
* used.
*/
protected ClassLoader driverClassLoader = null;
-
+
/**
* Returns the class loader specified for loading the JDBC driver. Returns
* <code>null</code> if no class loader has been explicitly specified.
@@ -276,7 +277,7 @@ public class BasicDataSource implements
* initialized. The pool is initialized the first time one of the
* following methods is invoked: <code>getConnection, setLogwriter,
* setLoginTimeout, getLoginTimeout, getLogWriter.</code></p>
- *
+ *
* @param driverClassLoader the class loader with which to load the JDBC
* driver
*/
@@ -285,7 +286,7 @@ public class BasicDataSource implements
this.driverClassLoader = driverClassLoader;
this.restartNeeded = true;
}
-
+
/**
* True means that borrowObject returns the most recently used ("last in")
* connection in the pool (if there are idle connections available). False
@@ -295,12 +296,12 @@ public class BasicDataSource implements
private boolean lifo = GenericObjectPoolConfig.DEFAULT_LIFO;
/**
- * Returns the LIFO property.
- *
+ * Returns the LIFO property.
+ *
* @return true if connection pool behaves as a LIFO queue.
- *
- * @see #lifo
- */
+ *
+ * @see #lifo
+ */
public synchronized boolean getLifo() {
return this.lifo;
}
@@ -308,17 +309,17 @@ public class BasicDataSource implements
/**
* Sets the LIFO property. True means the pool behaves as a LIFO queue;
* false means FIFO.
- *
+ *
* @param lifo the new value for the LIFO property
- *
- */
+ *
+ */
public synchronized void setLifo(boolean lifo) {
- this.lifo = lifo;
+ this.lifo = lifo;
if (connectionPool != null) {
connectionPool.setLifo(lifo);
}
}
-
+
/**
* The maximum number of active connections that can be allocated from
* this pool at the same time, or negative for no limit.
@@ -330,7 +331,7 @@ public class BasicDataSource implements
* allocated at the same time.
* </p>
* <p>A negative number means that there is no limit.</p>
- *
+ *
* @return the maximum number of active connections
*/
public synchronized int getMaxTotal() {
@@ -340,7 +341,7 @@ public class BasicDataSource implements
/**
* Sets the maximum total number of idle and borrows connections that can be
* active at the same time. Use a negative value for no limit.
- *
+ *
* @param maxTotal the new value for maxTotal
* @see #getMaxTotal()
*/
@@ -368,7 +369,7 @@ public class BasicDataSource implements
* pool. Excess idle connections are destroyed on return to the pool.
* </p>
* <p>A negative value indicates that there is no limit</p>
- *
+ *
* @return the maximum number of idle connections
*/
public synchronized int getMaxIdle() {
@@ -378,7 +379,7 @@ public class BasicDataSource implements
/**
* Sets the maximum number of connections that can remain idle in the
* pool. Excess idle connections are destroyed on return to the pool.
- *
+ *
* @see #getMaxIdle()
* @param maxIdle the new value for maxIdle
*/
@@ -391,7 +392,7 @@ public class BasicDataSource implements
/**
* The minimum number of active connections that can remain idle in the
- * pool, without extra ones being created when the evictor runs, or 0 to create none.
+ * pool, without extra ones being created when the evictor runs, or 0 to create none.
* The pool attempts to ensure that minIdle connections are available when the idle object evictor
* runs. The value of this property has no effect unless {@link #timeBetweenEvictionRunsMillis}
* has a positive value.
@@ -403,7 +404,7 @@ public class BasicDataSource implements
* to ensure that minIdle connections are available when the idle object evictor
* runs. The value of this property has no effect unless {@link #timeBetweenEvictionRunsMillis}
* has a positive value.
- *
+ *
* @return the minimum number of idle connections
* @see GenericObjectPool#getMinIdle()
*/
@@ -416,7 +417,7 @@ public class BasicDataSource implements
* to ensure that minIdle connections are available when the idle object evictor
* runs. The value of this property has no effect unless {@link #timeBetweenEvictionRunsMillis}
* has a positive value.
- *
+ *
* @param minIdle the new value for minIdle
* @see GenericObjectPool#setMinIdle(int)
*/
@@ -430,20 +431,20 @@ public class BasicDataSource implements
/**
* The initial number of connections that are created when the pool
* is started.
- *
+ *
* @since 1.2
*/
protected int initialSize = 0;
-
+
/**
* Returns the initial size of the connection pool.
- *
+ *
* @return the number of connections created when the pool is initialized
*/
public synchronized int getInitialSize() {
return this.initialSize;
}
-
+
/**
* <p>Sets the initial size of the connection pool.</p>
* <p>
@@ -451,7 +452,7 @@ public class BasicDataSource implements
* initialized. The pool is initialized the first time one of the
* following methods is invoked: <code>getConnection, setLogwriter,
* setLoginTimeout, getLoginTimeout, getLogWriter.</code></p>
- *
+ *
* @param initialSize the number of connections created when the pool
* is initialized
*/
@@ -474,7 +475,7 @@ public class BasicDataSource implements
* </p>
* <p>A value less than or equal to zero means the pool is set to wait
* indefinitely.</p>
- *
+ *
* @return the maxWaitMillis property value
*/
public synchronized long getMaxWaitMillis() {
@@ -486,7 +487,7 @@ public class BasicDataSource implements
* </p>
* <p>Use -1 to make the pool wait indefinitely.
* </p>
- *
+ *
* @param maxWaitMillis the new value for MaxWaitMillis
* @see #getMaxWaitMillis()
*/
@@ -502,10 +503,10 @@ public class BasicDataSource implements
* both PreparedStatements and CallableStatements are pooled.
*/
protected boolean poolPreparedStatements = false;
-
+
/**
* Returns true if we are pooling statements.
- *
+ *
* @return true if prepared and callable statements are pooled
*/
public synchronized boolean isPoolPreparedStatements() {
@@ -519,7 +520,7 @@ public class BasicDataSource implements
* initialized. The pool is initialized the first time one of the
* following methods is invoked: <code>getConnection, setLogwriter,
* setLoginTimeout, getLoginTimeout, getLogWriter.</code></p>
- *
+ *
* @param poolingStatements pooling on or off
*/
public synchronized void setPoolPreparedStatements(boolean poolingStatements) {
@@ -529,10 +530,10 @@ public class BasicDataSource implements
/**
* <p>The maximum number of open statements that can be allocated from
- * the statement pool at the same time, or non-positive for no limit. Since
+ * the statement pool at the same time, or non-positive for no limit. Since
* a connection usually only uses one or two statements at a time, this is
* mostly used to help detect resource leaks.</p>
- *
+ *
* <p>Note: As of version 1.3, CallableStatements (those produced by {@link Connection#prepareCall})
* are pooled along with PreparedStatements (produced by {@link Connection#prepareStatement})
* and <code>maxOpenPreparedStatements</code> limits the total number of prepared or callable statements
@@ -543,7 +544,7 @@ public class BasicDataSource implements
/**
* Gets the value of the {@link #maxOpenPreparedStatements} property.
- *
+ *
* @return the maximum number of open statements
* @see #maxOpenPreparedStatements
*/
@@ -551,7 +552,7 @@ public class BasicDataSource implements
return this.maxOpenPreparedStatements;
}
- /**
+ /**
* <p>Sets the value of the {@link #maxOpenPreparedStatements}
* property.</p>
* <p>
@@ -559,7 +560,7 @@ public class BasicDataSource implements
* initialized. The pool is initialized the first time one of the
* following methods is invoked: <code>getConnection, setLogwriter,
* setLoginTimeout, getLoginTimeout, getLogWriter.</code></p>
- *
+ *
* @param maxOpenStatements the new maximum number of prepared statements
* @see #maxOpenPreparedStatements
*/
@@ -577,10 +578,10 @@ public class BasicDataSource implements
/**
* Returns the {@link #testOnBorrow} property.
- *
+ *
* @return true if objects are validated before being borrowed from the
* pool
- *
+ *
* @see #testOnBorrow
*/
public synchronized boolean getTestOnBorrow() {
@@ -590,9 +591,9 @@ public class BasicDataSource implements
/**
* Sets the {@link #testOnBorrow} property. This property determines
* whether or not the pool will validate objects before they are borrowed
- * from the pool. For a <code>true</code> value to have any effect, the
+ * from the pool. For a <code>true</code> value to have any effect, the
* <code>validationQuery</code> property must be set to a non-null string.
- *
+ *
* @param testOnBorrow new value for testOnBorrow property
*/
public synchronized void setTestOnBorrow(boolean testOnBorrow) {
@@ -610,7 +611,7 @@ public class BasicDataSource implements
/**
* Returns the value of the {@link #testOnReturn} property.
- *
+ *
* @return true if objects are validated before being returned to the
* pool
* @see #testOnReturn
@@ -622,9 +623,9 @@ public class BasicDataSource implements
/**
* Sets the <code>testOnReturn</code> property. This property determines
* whether or not the pool will validate objects before they are returned
- * to the pool. For a <code>true</code> value to have any effect, the
+ * to the pool. For a <code>true</code> value to have any effect, the
* <code>validationQuery</code> property must be set to a non-null string.
- *
+ *
* @param testOnReturn new value for testOnReturn property
*/
public synchronized void setTestOnReturn(boolean testOnReturn) {
@@ -641,11 +642,11 @@ public class BasicDataSource implements
*/
protected long timeBetweenEvictionRunsMillis =
GenericObjectPoolConfig.DEFAULT_TIME_BETWEEN_EVICTION_RUNS_MILLIS;
-
+
/**
* Returns the value of the {@link #timeBetweenEvictionRunsMillis}
* property.
- *
+ *
* @return the time (in milliseconds) between evictor runs
* @see #timeBetweenEvictionRunsMillis
*/
@@ -655,7 +656,7 @@ public class BasicDataSource implements
/**
* Sets the {@link #timeBetweenEvictionRunsMillis} property.
- *
+ *
* @param timeBetweenEvictionRunsMillis the new time between evictor runs
* @see #timeBetweenEvictionRunsMillis
*/
@@ -675,7 +676,7 @@ public class BasicDataSource implements
/**
* Returns the value of the {@link #numTestsPerEvictionRun} property.
- *
+ *
* @return the number of objects to examine during idle object evictor
* runs
* @see #numTestsPerEvictionRun
@@ -686,8 +687,8 @@ public class BasicDataSource implements
/**
* Sets the value of the {@link #numTestsPerEvictionRun} property.
- *
- * @param numTestsPerEvictionRun the new {@link #numTestsPerEvictionRun}
+ *
+ * @param numTestsPerEvictionRun the new {@link #numTestsPerEvictionRun}
* value
* @see #numTestsPerEvictionRun
*/
@@ -707,7 +708,7 @@ public class BasicDataSource implements
/**
* Returns the {@link #minEvictableIdleTimeMillis} property.
- *
+ *
* @return the value of the {@link #minEvictableIdleTimeMillis} property
* @see #minEvictableIdleTimeMillis
*/
@@ -717,9 +718,9 @@ public class BasicDataSource implements
/**
* Sets the {@link #minEvictableIdleTimeMillis} property.
- *
+ *
* @param minEvictableIdleTimeMillis the minimum amount of time an object
- * may sit idle in the pool
+ * may sit idle in the pool
* @see #minEvictableIdleTimeMillis
*/
public synchronized void setMinEvictableIdleTimeMillis(long minEvictableIdleTimeMillis) {
@@ -728,7 +729,7 @@ public class BasicDataSource implements
connectionPool.setMinEvictableIdleTimeMillis(minEvictableIdleTimeMillis);
}
}
-
+
/**
* The minimum amount of time a connection may sit idle in the pool before
* it is eligible for eviction by the idle object evictor, with the extra
@@ -738,7 +739,7 @@ public class BasicDataSource implements
*/
private long softMinEvictableIdleTimeMillis =
GenericObjectPoolConfig.DEFAULT_SOFT_MIN_EVICTABLE_IDLE_TIME_MILLIS;
-
+
/**
* Sets the minimum amount of time a connection may sit idle in the pool
* before it is eligible for eviction by the idle object evictor, with the
@@ -756,19 +757,19 @@ public class BasicDataSource implements
connectionPool.setSoftMinEvictableIdleTimeMillis(softMinEvictableIdleTimeMillis);
}
}
-
+
/**
* <p>Returns the minimum amount of time a connection may sit idle in the
* pool before it is eligible for eviction by the idle object evictor, with
* the extra condition that at least "minIdle" connections remain in the
* pool.</p>
- *
- * <p>When {@link #getMinEvictableIdleTimeMillis() miniEvictableIdleTimeMillis}
+ *
+ * <p>When {@link #getMinEvictableIdleTimeMillis() miniEvictableIdleTimeMillis}
* is set to a positive value, miniEvictableIdleTimeMillis is examined
* first by the idle connection evictor - i.e. when idle connections are
* visited by the evictor, idle time is first compared against
* {@code minEvictableIdleTimeMillis} (without considering the number of idle
- * connections in the pool) and then against
+ * connections in the pool) and then against
* {@code softMinEvictableIdleTimeMillis}, including the {@code minIdle},
* constraint.</p>
*
@@ -790,7 +791,7 @@ public class BasicDataSource implements
/**
* Returns the value of the {@link #testWhileIdle} property.
- *
+ *
* @return true if objects examined by the idle object evictor are
* validated
* @see #testWhileIdle
@@ -802,9 +803,9 @@ public class BasicDataSource implements
/**
* Sets the <code>testWhileIdle</code> property. This property determines
* whether or not the idle object evictor will validate connections. For a
- * <code>true</code> value to have any effect, the
+ * <code>true</code> value to have any effect, the
* <code>validationQuery</code> property must be set to a non-null string.
- *
+ *
* @param testWhileIdle new value for testWhileIdle property
*/
public synchronized void setTestWhileIdle(boolean testWhileIdle) {
@@ -817,7 +818,7 @@ public class BasicDataSource implements
/**
* [Read Only] The current number of active connections that have been
* allocated from this data source.
- *
+ *
* @return the current number of active connections
*/
public synchronized int getNumActive() {
@@ -832,7 +833,7 @@ public class BasicDataSource implements
/**
* [Read Only] The current number of idle connections that are waiting
* to be allocated from this data source.
- *
+ *
* @return the current number of idle connections
*/
public synchronized int getNumIdle() {
@@ -851,21 +852,21 @@ public class BasicDataSource implements
/**
* Returns the password passed to the JDBC driver to establish connections.
- *
+ *
* @return the connection password
*/
public String getPassword() {
return this.password;
}
- /**
+ /**
* <p>Sets the {@link #password}.</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 password new value for the password
*/
public void setPassword(String password) {
@@ -881,7 +882,7 @@ public class BasicDataSource implements
/**
* Returns the JDBC connection {@link #url} property.
- *
+ *
* @return the {@link #url} passed to the JDBC driver to establish
* connections
*/
@@ -889,14 +890,14 @@ public class BasicDataSource implements
return this.url;
}
- /**
+ /**
* <p>Sets the {@link #url}.</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 url the new value for the JDBC connection url
*/
public synchronized void setUrl(String url) {
@@ -912,7 +913,7 @@ public class BasicDataSource implements
/**
* Returns the JDBC connection {@link #username} property.
- *
+ *
* @return the {@link #username} passed to the JDBC driver to establish
* connections
*/
@@ -920,14 +921,14 @@ public class BasicDataSource implements
return this.username;
}
- /**
+ /**
* <p>Sets the {@link #username}.</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 username the new value for the JDBC connection username
*/
public void setUsername(String username) {
@@ -946,7 +947,7 @@ public class BasicDataSource implements
/**
* Returns the validation query used to validate connections before
* returning them.
- *
+ *
* @return the SQL validation query
* @see #validationQuery
*/
@@ -954,14 +955,14 @@ public class BasicDataSource implements
return this.validationQuery;
}
- /**
+ /**
* <p>Sets the {@link #validationQuery}.</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 validationQuery the new value for the validation query
*/
public void setValidationQuery(String validationQuery) {
@@ -972,24 +973,24 @@ public class BasicDataSource implements
}
this.restartNeeded = true;
}
-
+
/**
- * Timeout in seconds before connection validation queries fail.
- *
+ * Timeout in seconds before connection validation queries fail.
+ *
* @since 1.3
*/
protected volatile int validationQueryTimeout = -1;
-
+
/**
* Returns the validation query timeout.
- *
+ *
* @return the timeout in seconds before connection validation queries fail.
* @since 1.3
*/
public int getValidationQueryTimeout() {
return validationQueryTimeout;
}
-
+
/**
* Sets the validation query timeout, the amount of time, in seconds, that
* connection validation will wait for a response from the database when
@@ -1000,7 +1001,7 @@ public class BasicDataSource implements
* initialized. The pool is initialized the first time one of the
* following methods is invoked: <code>getConnection, setLogwriter,
* setLoginTimeout, getLoginTimeout, getLogWriter.</code></p>
- *
+ *
* @param timeout new validation query timeout value in seconds
* @since 1.3
*/
@@ -1008,7 +1009,7 @@ public class BasicDataSource implements
this.validationQueryTimeout = timeout;
restartNeeded = true;
}
-
+
/**
* These SQL statements run once after a Connection is created.
* <p>
@@ -1016,7 +1017,7 @@ public class BasicDataSource implements
* NLS_SORT=XCYECH in an Oracle Database only once after connection
* creation.
* </p>
- *
+ *
* @since 1.3
*/
protected volatile List<String> connectionInitSqls;
@@ -1025,12 +1026,12 @@ public class BasicDataSource implements
* Returns the list of SQL statements executed when a physical connection
* is first created. Returns an empty list if there are no initialization
* statements configured.
- *
+ *
* @return initialization SQL statements
* @since 1.3
*/
public Collection<String> getConnectionInitSqls() {
- Collection<String> result = connectionInitSqls;
+ Collection<String> result = connectionInitSqls;
if (result == null) {
return Collections.emptyList();
}
@@ -1045,7 +1046,7 @@ public class BasicDataSource implements
* initialized. The pool is initialized the first time one of the
* following methods is invoked: <code>getConnection, setLogwriter,
* setLoginTimeout, getLoginTimeout, getLogWriter.</code></p>
- *
+ *
* @param connectionInitSqls Collection of SQL statements to execute
* on connection creation
*/
@@ -1070,14 +1071,14 @@ public class BasicDataSource implements
}
- /**
+ /**
* Controls access to the underlying connection.
*/
- private boolean accessToUnderlyingConnectionAllowed = false;
+ private boolean accessToUnderlyingConnectionAllowed = false;
/**
* Returns the value of the accessToUnderlyingConnectionAllowed property.
- *
+ *
* @return true if access to the underlying connection is allowed, false
* otherwise.
*/
@@ -1094,7 +1095,7 @@ public class BasicDataSource implements
* initialized. The pool is initialized the first time one of the
* following methods is invoked: <code>getConnection, setLogwriter,
* setLoginTimeout, getLoginTimeout, getLogWriter.</code></p>
- *
+ *
* @param allow Access to the underlying connection is granted when true.
*/
public synchronized void setAccessToUnderlyingConnectionAllowed(boolean allow) {
@@ -1112,12 +1113,12 @@ public class BasicDataSource implements
* this property has no effect.
*/
private volatile boolean restartNeeded = false;
-
+
/**
- * Returns whether or not a restart is needed.
- *
+ * Returns whether or not a restart is needed.
+ *
* Note: restart is not currently triggered by property changes.
- *
+ *
* @return true if a restart is needed
*/
private boolean isRestartNeeded() {
@@ -1128,7 +1129,7 @@ public class BasicDataSource implements
* The object pool that internally manages our connections.
*/
protected volatile GenericObjectPool<Connection> connectionPool = null;
-
+
/**
* The connection properties that will be sent to our JDBC driver when
* establishing new connections. <strong>NOTE</strong> - The "user" and
@@ -1262,17 +1263,17 @@ public class BasicDataSource implements
private AbandonedConfig abandonedConfig;
- /**
+ /**
* <p>Flag to remove abandoned connections if they exceed the
* removeAbandonedTimeout when borrowObject is invoked.</p>
*
* <p>The default value is false.<p>
- *
+ *
* <p>If set to true a connection is considered abandoned and eligible
* for removal if it has not been used for more than
* {@link #getRemoveAbandonedTimeout() removeAbandonedTimeout} seconds.</p>
- *
- * <p>Abandoned connections are identified and removed when
+ *
+ * <p>Abandoned connections are identified and removed when
* {@link #getConnection()} is invoked and the following conditions hold
* <ul><li>{@link #getRemoveAbandonedOnBorrow()} or
* {@link #getRemoveAbandonedOnMaintenance()} = true</li>
@@ -1280,22 +1281,22 @@ public class BasicDataSource implements
* <li>{@link #getNumIdle()} < 2 </li></ul></p>
*
* @see #getRemoveAbandonedTimeout()
- */
- public boolean getRemoveAbandonedOnBorrow() {
+ */
+ public boolean getRemoveAbandonedOnBorrow() {
if (abandonedConfig != null) {
return abandonedConfig.getRemoveAbandonedOnBorrow();
}
return false;
- }
-
+ }
+
/**
* <p>Flag to remove abandoned connections if they exceed the
* removeAbandonedTimeout when borrowObject is invoked.</p>
*
- * <p>If set to true a connection is considered abandoned and eligible
+ * <p>If set to true a connection is considered abandoned and eligible
* for removal if it has been idle longer than the
* {@link #getRemoveAbandonedTimeout() removeAbandonedTimeout}.</p>
- *
+ *
* <p>Setting this to true can recover db connections from poorly written
* applications which fail to close a connection.</p>
*
@@ -1311,19 +1312,19 @@ public class BasicDataSource implements
abandonedConfig.setRemoveAbandonedOnMaintenance(
removeAbandonedOnMaintenance);
this.restartNeeded = true;
- }
-
- /**
+ }
+
+ /**
* <p>Flag to remove abandoned connections if they exceed the
* removeAbandonedTimeout during pool maintenance.</p>
*
* <p>The default value is false.<p>
- *
+ *
* <p>If set to true a connection is considered abandoned and eligible
* for removal if it has not been used for more than
* {@link #getRemoveAbandonedTimeout() removeAbandonedTimeout} seconds.</p>
- *
- * <p>Abandoned connections are identified and removed when
+ *
+ * <p>Abandoned connections are identified and removed when
* {@link #getConnection()} is invoked and the following conditions hold
* <ul><li>{@link #getRemoveAbandonedOnBorrow()} or
* {@link #getRemoveAbandonedOnMaintenance()} = true</li>
@@ -1331,22 +1332,22 @@ public class BasicDataSource implements
* <li>{@link #getNumIdle()} < 2 </li></ul></p>
*
* @see #getRemoveAbandonedTimeout()
- */
- public boolean getRemoveAbandonedOnMaintenance() {
+ */
+ public boolean getRemoveAbandonedOnMaintenance() {
if (abandonedConfig != null) {
return abandonedConfig.getRemoveAbandonedOnMaintenance();
}
return false;
- }
-
+ }
+
/**
* <p>Flag to remove abandoned connections if they exceed the
* removeAbandonedTimeout during pool maintenance.</p>
*
- * <p>If set to true a connection is considered abandoned and eligible
+ * <p>If set to true a connection is considered abandoned and eligible
* for removal if it has been idle longer than the
* {@link #getRemoveAbandonedTimeout() removeAbandonedTimeout}.</p>
- *
+ *
* <p>Setting this to true can recover db connections from poorly written
* applications which fail to close a connection.</p>
*
@@ -1359,15 +1360,15 @@ public class BasicDataSource implements
}
abandonedConfig.setRemoveAbandonedOnBorrow(removeAbandonedOnBorrow);
this.restartNeeded = true;
- }
-
- /**
+ }
+
+ /**
* <p>Timeout in seconds before an abandoned connection can be removed.</p>
- *
+ *
* <p>Creating a Statement, PreparedStatement or CallableStatement or using
* one of these to execute a query (using one of the execute methods)
* resets the lastUsed property of the parent connection.</p>
- *
+ *
* <p>Abandoned connection cleanup happens when
* <code><ul>
* <li>{@link #getRemoveAbandonedOnBorrow()} or
@@ -1375,21 +1376,21 @@ public class BasicDataSource implements
* <li>{@link #getNumIdle() numIdle} < 2</li>
* <li>{@link #getNumActive() numActive} > {@link #getMaxTotal() maxActive} - 3</li>
* </ul></code></p>
- *
+ *
* <p>The default value is 300 seconds.</p>
- */
- public int getRemoveAbandonedTimeout() {
+ */
+ public int getRemoveAbandonedTimeout() {
if (abandonedConfig != null) {
return abandonedConfig.getRemoveAbandonedTimeout();
}
return 300;
- }
+ }
/**
* <p>Sets the timeout in seconds before an abandoned connection can be
* removed.</p>
- *
- * <p>Setting this property has no effect if
+ *
+ * <p>Setting this property has no effect if
* {@link #getRemoveAbandonedOnBorrow()} and
* {@link #getRemoveAbandonedOnMaintenance()} are false.</p>
*
@@ -1397,31 +1398,31 @@ public class BasicDataSource implements
* @see #getRemoveAbandonedTimeout()
* @see #getRemoveAbandonedOnBorrow()
* @see #getRemoveAbandonedOnMaintenance()
- */
+ */
public void setRemoveAbandonedTimeout(int removeAbandonedTimeout) {
if (abandonedConfig == null) {
abandonedConfig = new AbandonedConfig();
}
abandonedConfig.setRemoveAbandonedTimeout(removeAbandonedTimeout);
this.restartNeeded = true;
- }
-
+ }
+
/**
* <p>Flag to log stack traces for application code which abandoned
* a Statement or Connection.
* </p>
- * <p>Defaults to false.
- * </p>
+ * <p>Defaults to false.
+ * </p>
* <p>Logging of abandoned Statements and Connections adds overhead
- * for every Connection open or new Statement because a stack
+ * for every Connection open or new Statement because a stack
* trace has to be generated. </p>
- */
- public boolean getLogAbandoned() {
+ */
+ public boolean getLogAbandoned() {
if (abandonedConfig != null) {
return abandonedConfig.getLogAbandoned();
}
return false;
- }
+ }
/**
* @param logAbandoned new logAbandoned property value
@@ -1453,7 +1454,7 @@ public class BasicDataSource implements
/**
* Remove a custom connection property.
- *
+ *
* @param name Name of the custom connection property to remove
* @see #addConnectionProperty(String, String)
*/
@@ -1502,16 +1503,16 @@ public class BasicDataSource implements
* <p>Closes and releases all idle connections that are currently stored in the connection pool
* associated with this data source.</p>
*
- * <p>Connections that are checked out to clients when this method is invoked are not affected.
+ * <p>Connections that are checked out to clients when this method is invoked are not affected.
* When client applications subsequently invoke {@link Connection#close()} to return
* these connections to the pool, the underlying JDBC connections are closed.</p>
- *
+ *
* <p>Attempts to acquire connections using {@link #getConnection()} after this method has been
* invoked result in SQLExceptions.<p>
- *
+ *
* <p>This method is idempotent - i.e., closing an already closed BasicDataSource has no effect
* and does not generate exceptions.</p>
- *
+ *
* @throws SQLException if an error occurs closing idle connections
*/
public synchronized void close() throws SQLException {
@@ -1554,7 +1555,7 @@ public class BasicDataSource implements
public Logger getParentLogger() throws SQLFeatureNotSupportedException {
throw new SQLFeatureNotSupportedException();
}
-
+
// ------------------------------------------------------ Protected Methods
@@ -1583,7 +1584,7 @@ public class BasicDataSource implements
// create factory which returns raw physical connections
ConnectionFactory driverConnectionFactory = createConnectionFactory();
-
+
// Set up the poolable connection factory
boolean success = false;
PoolableConnectionFactory poolableConnectionFactory;
@@ -1598,7 +1599,7 @@ public class BasicDataSource implements
} catch (SQLException se) {
throw se;
} catch (RuntimeException rte) {
- throw rte;
+ throw rte;
} catch (Exception ex) {
throw new SQLException("Error creating connection factory", ex);
}
@@ -1607,7 +1608,7 @@ public class BasicDataSource implements
// create a pool for our connections
createConnectionPool(poolableConnectionFactory);
}
-
+
// Create the pooling data source to manage connections
success = false;
try {
@@ -1622,9 +1623,9 @@ public class BasicDataSource implements
} finally {
if (!success) {
closeConnectionPool();
- }
+ }
}
-
+
// If initialSize > 0, preload the pool
try {
for (int i = 0 ; i < initialSize ; i++) {
@@ -1634,10 +1635,10 @@ public class BasicDataSource implements
closeConnectionPool();
throw new SQLException("Error preloading the connection pool", e);
}
-
+
// If timeBetweenEvictionRunsMillis > 0, start the pool's evictor task
startPoolMaintenance();
-
+
return dataSource;
}
}
@@ -1694,7 +1695,7 @@ public class BasicDataSource implements
// respect the ContextClassLoader
driver = (Driver) driverFromCCL.newInstance();
if (!driver.acceptsURL(url)) {
- throw new SQLException("No suitable driver", "08001");
+ throw new SQLException("No suitable driver", "08001");
}
}
} catch (Exception t) {
@@ -1735,8 +1736,8 @@ public class BasicDataSource implements
/**
* Creates a connection pool for this datasource. This method only exists
* so subclasses can replace the implementation class.
- *
- * This implementation configures all pool properties other than
+ *
+ * This implementation configures all pool properties other than
* timeBetweenEvictionRunsMillis. Setting that property is deferred to
* {@link #startPoolMaintenance()}, since setting timeBetweenEvictionRunsMillis
* to a positive value causes {@link GenericObjectPool}'s eviction timer
@@ -1767,7 +1768,7 @@ public class BasicDataSource implements
factory.setPool(gop);
connectionPool = gop;
}
-
+
/**
* Closes the connection pool, silently swallowing any exception that occurs.
*/
@@ -1782,9 +1783,9 @@ public class BasicDataSource implements
// Do not propagate
}
}
-
+
/**
- * Starts the connection pool maintenance task, if configured.
+ * Starts the connection pool maintenance task, if configured.
*/
protected void startPoolMaintenance() {
if (connectionPool != null && timeBetweenEvictionRunsMillis > 0) {
@@ -1796,7 +1797,7 @@ public class BasicDataSource implements
/**
* Creates the actual data source instance. This method only exists so
* that subclasses can replace the implementation class.
- *
+ *
* @throws SQLException if unable to create a datasource instance
*/
protected void createDataSourceInstance() throws SQLException {
@@ -1809,7 +1810,7 @@ public class BasicDataSource implements
/**
* Creates the PoolableConnectionFactory and attaches it to the connection pool. This method only exists
* so subclasses can replace the default implementation.
- *
+ *
* @param driverConnectionFactory JDBC connection factory
* @throws SQLException if an error occurs creating the PoolableConnectionFactory
*/
@@ -1841,17 +1842,20 @@ public class BasicDataSource implements
return connectionFactory;
}
- protected static void validateConnectionFactory(PoolableConnectionFactory connectionFactory) throws Exception {
+ protected static void validateConnectionFactory(
+ PoolableConnectionFactory connectionFactory) throws Exception {
PoolableConnection conn = null;
+ PooledObject<PoolableConnection> p = null;
try {
- conn = connectionFactory.makeObject();
- connectionFactory.activateObject(conn);
+ p = connectionFactory.makeObject();
+ conn = p.getObject();
+ connectionFactory.activateObject(p);
connectionFactory.validateConnection(conn);
- connectionFactory.passivateObject(conn);
+ connectionFactory.passivateObject(p);
}
finally {
- if (conn != null) {
- connectionFactory.destroyObject(conn);
+ if (p != null) {
+ connectionFactory.destroyObject(p);
}
}
}
Modified: commons/proper/dbcp/trunk/src/java/org/apache/commons/dbcp2/PoolableConnectionFactory.java
URL: http://svn.apache.org/viewvc/commons/proper/dbcp/trunk/src/java/org/apache/commons/dbcp2/PoolableConnectionFactory.java?rev=1506687&r1=1506686&r2=1506687&view=diff
==============================================================================
--- commons/proper/dbcp/trunk/src/java/org/apache/commons/dbcp2/PoolableConnectionFactory.java (original)
+++ commons/proper/dbcp/trunk/src/java/org/apache/commons/dbcp2/PoolableConnectionFactory.java Wed Jul 24 20:06:42 2013
@@ -26,13 +26,15 @@ import java.util.Collection;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.apache.commons.pool2.KeyedObjectPool;
-import org.apache.commons.pool2.PoolableObjectFactory;
import org.apache.commons.pool2.ObjectPool;
+import org.apache.commons.pool2.PooledObject;
+import org.apache.commons.pool2.PooledObjectFactory;
import org.apache.commons.pool2.impl.GenericKeyedObjectPool;
import org.apache.commons.pool2.impl.GenericKeyedObjectPoolConfig;
+import org.apache.commons.pool2.impl.PooledObjectImpl;
/**
- * A {@link PoolableObjectFactory} that creates
+ * A {@link PooledObjectFactory} that creates
* {@link PoolableConnection}s.
*
* @author Rodney Waldhoff
@@ -42,7 +44,7 @@ import org.apache.commons.pool2.impl.Gen
* @version $Revision$ $Date$
*/
public class PoolableConnectionFactory
- implements PoolableObjectFactory<PoolableConnection> {
+ implements PooledObjectFactory<PoolableConnection> {
private static final Log log =
LogFactory.getLog(PoolableConnectionFactory.class);
@@ -166,7 +168,7 @@ public class PoolableConnectionFactory
}
@Override
- public PoolableConnection makeObject() throws Exception {
+ public PooledObject<PoolableConnection> makeObject() throws Exception {
Connection conn = _connFactory.createConnection();
if (conn == null) {
throw new IllegalStateException("Connection factory returned null from createConnection");
@@ -196,7 +198,7 @@ public class PoolableConnectionFactory
((PoolingConnection)conn).setStatementPool(stmtPool);
((PoolingConnection) conn).setCacheState(_cacheState);
}
- return new PoolableConnection(conn,_pool);
+ return new PooledObjectImpl<>(new PoolableConnection(conn,_pool));
}
protected void initializeConnection(Connection conn) throws SQLException {
@@ -218,14 +220,15 @@ public class PoolableConnectionFactory
}
@Override
- public void destroyObject(PoolableConnection obj) throws Exception {
- obj.reallyClose();
+ public void destroyObject(PooledObject<PoolableConnection> p)
+ throws Exception {
+ p.getObject().reallyClose();
}
@Override
- public boolean validateObject(PoolableConnection conn) {
+ public boolean validateObject(PooledObject<PoolableConnection> p) {
try {
- validateConnection(conn);
+ validateConnection(p.getObject());
return true;
} catch(Exception e) {
if (log.isDebugEnabled()) {
@@ -273,7 +276,9 @@ public class PoolableConnectionFactory
}
@Override
- public void passivateObject(PoolableConnection conn) throws Exception {
+ public void passivateObject(PooledObject<PoolableConnection> p)
+ throws Exception {
+ PoolableConnection conn = p.getObject();
if(!conn.getAutoCommit() && !conn.isReadOnly()) {
conn.rollback();
}
@@ -286,7 +291,10 @@ public class PoolableConnectionFactory
}
@Override
- public void activateObject(PoolableConnection conn) throws Exception {
+ public void activateObject(PooledObject<PoolableConnection> p)
+ throws Exception {
+
+ PoolableConnection conn = p.getObject();
conn.activate();
if (conn.getAutoCommit() != _defaultAutoCommit) {
Modified: commons/proper/dbcp/trunk/src/java/org/apache/commons/dbcp2/PoolingConnection.java
URL: http://svn.apache.org/viewvc/commons/proper/dbcp/trunk/src/java/org/apache/commons/dbcp2/PoolingConnection.java?rev=1506687&r1=1506686&r2=1506687&view=diff
==============================================================================
--- commons/proper/dbcp/trunk/src/java/org/apache/commons/dbcp2/PoolingConnection.java (original)
+++ commons/proper/dbcp/trunk/src/java/org/apache/commons/dbcp2/PoolingConnection.java Wed Jul 24 20:06:42 2013
@@ -5,9 +5,9 @@
* The ASF licenses this file to You under the Apache License, Version 2.0
* (the "License"); you may not use this file except in compliance with
* the License. You may obtain a copy of the License at
- *
+ *
* http://www.apache.org/licenses/LICENSE-2.0
- *
+ *
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
@@ -26,6 +26,9 @@ import java.util.NoSuchElementException;
import org.apache.commons.pool2.KeyedObjectPool;
import org.apache.commons.pool2.KeyedPoolableObjectFactory;
+import org.apache.commons.pool2.KeyedPooledObjectFactory;
+import org.apache.commons.pool2.PooledObject;
+import org.apache.commons.pool2.impl.PooledObjectImpl;
/**
* A {@link DelegatingConnection} that pools {@link PreparedStatement}s.
@@ -33,9 +36,9 @@ import org.apache.commons.pool2.KeyedPoo
* The {@link #prepareStatement} and {@link #prepareCall} methods, rather than creating a new PreparedStatement
* each time, may actually pull the statement from a pool of unused statements.
* The {@link PreparedStatement#close} method of the returned statement doesn't
- * actually close the statement, but rather returns it to the pool.
+ * actually close the statement, but rather returns it to the pool.
* (See {@link PoolablePreparedStatement}, {@link PoolableCallableStatement}.)
- *
+ *
*
* @see PoolablePreparedStatement
* @author Rodney Waldhoff
@@ -43,18 +46,18 @@ import org.apache.commons.pool2.KeyedPoo
* @version $Revision$ $Date$
*/
public class PoolingConnection extends DelegatingConnection
- implements KeyedPoolableObjectFactory<PStmtKey,DelegatingPreparedStatement> {
+ implements KeyedPooledObjectFactory<PStmtKey,DelegatingPreparedStatement> {
/** Pool of {@link PreparedStatement}s. and {@link CallableStatement}s */
protected KeyedObjectPool<PStmtKey,DelegatingPreparedStatement> _pstmtPool = null;
/** Prepared Statement type */
protected static final byte STATEMENT_PREPAREDSTMT = 0;
-
+
/** Callable Statement type */
private static final byte STATEMENT_CALLABLESTMT = 1;
-
-
+
+
/**
* Constructor.
* @param c the underlying {@link Connection}.
@@ -77,7 +80,7 @@ public class PoolingConnection extends D
@Override
public synchronized void close() throws SQLException {
if(null != _pstmtPool) {
- KeyedObjectPool<PStmtKey,DelegatingPreparedStatement> oldpool = _pstmtPool;
+ KeyedObjectPool<PStmtKey,DelegatingPreparedStatement> oldpool = _pstmtPool;
_pstmtPool = null;
try {
oldpool.close();
@@ -104,7 +107,7 @@ public class PoolingConnection extends D
try {
return _pstmtPool.borrowObject(createKey(sql));
} catch(NoSuchElementException e) {
- throw (SQLException) new SQLException("MaxOpenPreparedStatements limit reached").initCause(e);
+ throw (SQLException) new SQLException("MaxOpenPreparedStatements limit reached").initCause(e);
} catch(RuntimeException e) {
throw e;
} catch(Exception e) {
@@ -128,14 +131,14 @@ public class PoolingConnection extends D
try {
return _pstmtPool.borrowObject(createKey(sql,resultSetType,resultSetConcurrency));
} catch(NoSuchElementException e) {
- throw (SQLException) new SQLException("MaxOpenPreparedStatements limit reached").initCause(e);
+ throw (SQLException) new SQLException("MaxOpenPreparedStatements limit reached").initCause(e);
} catch(RuntimeException e) {
throw e;
} catch(Exception e) {
throw (SQLException) new SQLException("Borrow prepareStatement from pool failed").initCause(e);
}
}
-
+
/**
* Create or obtain a {@link CallableStatement} from the pool.
* @param sql the sql string used to define the CallableStatement
@@ -155,7 +158,7 @@ public class PoolingConnection extends D
throw new SQLException("Borrow callableStatement from pool failed", e);
}
}
-
+
/**
* Create or obtain a {@link CallableStatement} from the pool.
* @param sql the sql string used to define the CallableStatement
@@ -178,7 +181,7 @@ public class PoolingConnection extends D
throw new SQLException("Borrow callableStatement from pool failed", e);
}
}
-
+
// TODO: possible enhancement, cache these preparedStatements as well
@@ -218,7 +221,7 @@ public class PoolingConnection extends D
} catch (SQLException e) {}
return new PStmtKey(normalizeSQL(sql), catalog, resultSetType, resultSetConcurrency);
}
-
+
/**
* Create a PStmtKey for the given arguments.
* @param sql the sql string used to define the statement
@@ -245,7 +248,7 @@ public class PoolingConnection extends D
} catch (SQLException e) {}
return new PStmtKey(normalizeSQL(sql), catalog);
}
-
+
/**
* Create a PStmtKey for the given arguments.
* @param sql the sql string used to define the statement
@@ -270,31 +273,35 @@ public class PoolingConnection extends D
/**
* {@link KeyedPoolableObjectFactory} method for creating
* {@link PoolablePreparedStatement}s or {@link PoolableCallableStatement}s.
- * The <code>stmtType</code> field in the key determines whether
+ * The <code>stmtType</code> field in the key determines whether
* a PoolablePreparedStatement or PoolableCallableStatement is created.
- *
+ *
* @param key the key for the {@link PreparedStatement} to be created
* @see #createKey(String, int, int, byte)
*/
@Override
- public DelegatingPreparedStatement makeObject(PStmtKey key)
+ public PooledObject<DelegatingPreparedStatement> makeObject(PStmtKey key)
throws Exception {
if(null == key) {
throw new IllegalArgumentException("Prepared statement key is null or invalid.");
} else {
if( null == key.getResultSetType() && null == key.getResultSetConcurrency()) {
if (key.getStmtType() == STATEMENT_PREPAREDSTMT ) {
- return new PoolablePreparedStatement(getDelegate().prepareStatement( key.getSql()), key, _pstmtPool, this);
+ return new PooledObjectImpl<DelegatingPreparedStatement>(
+ new PoolablePreparedStatement(getDelegate().prepareStatement( key.getSql()), key, _pstmtPool, this));
} else {
- return new PoolableCallableStatement(getDelegate().prepareCall( key.getSql()), key, _pstmtPool, this);
+ return new PooledObjectImpl<DelegatingPreparedStatement>(
+ new PoolableCallableStatement(getDelegate().prepareCall( key.getSql()), key, _pstmtPool, this));
}
} else { // Both _resultSetType and _resultSetConcurrency are non-null here (both or neither are set by constructors)
if(key.getStmtType() == STATEMENT_PREPAREDSTMT) {
- return new PoolablePreparedStatement(getDelegate().prepareStatement(
- key.getSql(), key.getResultSetType().intValue(),key.getResultSetConcurrency().intValue()), key, _pstmtPool, this);
+ return new PooledObjectImpl<DelegatingPreparedStatement>(
+ new PoolablePreparedStatement(getDelegate().prepareStatement(
+ key.getSql(), key.getResultSetType().intValue(),key.getResultSetConcurrency().intValue()), key, _pstmtPool, this));
} else {
- return new PoolableCallableStatement( getDelegate().prepareCall(
- key.getSql(),key.getResultSetType().intValue(), key.getResultSetConcurrency().intValue()), key, _pstmtPool, this);
+ return new PooledObjectImpl<DelegatingPreparedStatement>(
+ new PoolableCallableStatement( getDelegate().prepareCall(
+ key.getSql(),key.getResultSetType().intValue(), key.getResultSetConcurrency().intValue()), key, _pstmtPool, this));
}
}
}
@@ -304,54 +311,56 @@ public class PoolingConnection extends D
* {@link KeyedPoolableObjectFactory} method for destroying
* PoolablePreparedStatements and PoolableCallableStatements.
* Closes the underlying statement.
- *
+ *
* @param key ignored
* @param dps the pooled statement to be destroyed.
*/
@Override
- public void destroyObject(PStmtKey key, DelegatingPreparedStatement dps)
+ public void destroyObject(PStmtKey key,
+ PooledObject<DelegatingPreparedStatement> p)
throws Exception {
- dps.getInnermostDelegate().close();
+ p.getObject().getInnermostDelegate().close();
}
/**
* {@link KeyedPoolableObjectFactory} method for validating
* pooled statements. Currently always returns true.
- *
+ *
* @param key ignored
* @param obj ignored
* @return <tt>true</tt>
*/
@Override
public boolean validateObject(PStmtKey key,
- DelegatingPreparedStatement obj) {
+ PooledObject<DelegatingPreparedStatement> p) {
return true;
}
/**
* {@link KeyedPoolableObjectFactory} method for activating
* pooled statements.
- *
+ *
* @param key ignored
* @param dps pooled statement to be activated
*/
@Override
public void activateObject(PStmtKey key,
- DelegatingPreparedStatement dps) throws Exception {
- dps.activate();
+ PooledObject<DelegatingPreparedStatement> p) throws Exception {
+ p.getObject().activate();
}
/**
* {@link KeyedPoolableObjectFactory} method for passivating
* {@link PreparedStatement}s or {@link CallableStatement}s.
* Invokes {@link PreparedStatement#clearParameters}.
- *
+ *
* @param key ignored
* @param dps a {@link PreparedStatement}
*/
@Override
public void passivateObject(PStmtKey key,
- DelegatingPreparedStatement dps) throws Exception {
+ PooledObject<DelegatingPreparedStatement> p) throws Exception {
+ DelegatingPreparedStatement dps = p.getObject();
dps.clearParameters();
dps.passivate();
}
Modified: commons/proper/dbcp/trunk/src/java/org/apache/commons/dbcp2/cpdsadapter/PooledConnectionImpl.java
URL: http://svn.apache.org/viewvc/commons/proper/dbcp/trunk/src/java/org/apache/commons/dbcp2/cpdsadapter/PooledConnectionImpl.java?rev=1506687&r1=1506686&r2=1506687&view=diff
==============================================================================
--- commons/proper/dbcp/trunk/src/java/org/apache/commons/dbcp2/cpdsadapter/PooledConnectionImpl.java (original)
+++ commons/proper/dbcp/trunk/src/java/org/apache/commons/dbcp2/cpdsadapter/PooledConnectionImpl.java Wed Jul 24 20:06:42 2013
@@ -5,9 +5,9 @@
* The ASF licenses this file to You under the Apache License, Version 2.0
* (the "License"); you may not use this file except in compliance with
* the License. You may obtain a copy of the License at
- *
+ *
* http://www.apache.org/licenses/LICENSE-2.0
- *
+ *
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
@@ -32,7 +32,9 @@ import javax.sql.StatementEventListener;
import org.apache.commons.dbcp2.DelegatingConnection;
import org.apache.commons.dbcp2.PoolablePreparedStatement;
import org.apache.commons.pool2.KeyedObjectPool;
-import org.apache.commons.pool2.KeyedPoolableObjectFactory;
+import org.apache.commons.pool2.KeyedPooledObjectFactory;
+import org.apache.commons.pool2.PooledObject;
+import org.apache.commons.pool2.impl.PooledObjectImpl;
/**
* Implementation of PooledConnection that is returned by
@@ -42,16 +44,16 @@ import org.apache.commons.pool2.KeyedPoo
* @version $Revision$ $Date$
*/
class PooledConnectionImpl implements PooledConnection,
- KeyedPoolableObjectFactory<PStmtKeyCPDS,PoolablePreparedStatement<PStmtKeyCPDS,PoolablePreparedStatementStub>> {
-
- private static final String CLOSED
+ KeyedPooledObjectFactory<PStmtKeyCPDS,PoolablePreparedStatement<PStmtKeyCPDS,PoolablePreparedStatementStub>> {
+
+ private static final String CLOSED
= "Attempted to use PooledConnection after closed() was called.";
/**
* The JDBC database connection that represents the physical db connection.
*/
private Connection connection = null;
-
+
/**
* A DelegatingConnection used to create a PoolablePreparedStatementStub
*/
@@ -82,10 +84,10 @@ class PooledConnectionImpl implements Po
// TODO - make final?
protected KeyedObjectPool<PStmtKeyCPDS, PoolablePreparedStatement<PStmtKeyCPDS, PoolablePreparedStatementStub>> pstmtPool = null;
- /**
- * Controls access to the underlying connection
+ /**
+ * Controls access to the underlying connection
*/
- private boolean accessToUnderlyingConnectionAllowed = false;
+ private boolean accessToUnderlyingConnectionAllowed = false;
/**
* Wrap the real connection.
@@ -102,7 +104,7 @@ class PooledConnectionImpl implements Po
eventListeners = new Vector<ConnectionEventListener>();
isClosed = false;
}
-
+
public void setStatementPool(
KeyedObjectPool<PStmtKeyCPDS, PoolablePreparedStatement<PStmtKeyCPDS, PoolablePreparedStatementStub>> statementPool) {
pstmtPool = statementPool;
@@ -128,14 +130,14 @@ class PooledConnectionImpl implements Po
/* JDBC_4_ANT_KEY_END */
/**
- * Closes the physical connection and marks this
- * <code>PooledConnection</code> so that it may not be used
+ * Closes the physical connection and marks this
+ * <code>PooledConnection</code> so that it may not be used
* to generate any more logical <code>Connection</code>s.
*
* @exception SQLException if an error occurs or the connection is already closed
*/
@Override
- public void close() throws SQLException {
+ public void close() throws SQLException {
assertOpen();
isClosed = true;
try {
@@ -181,7 +183,7 @@ class PooledConnectionImpl implements Po
if (logicalConnection != null && !logicalConnection.isClosed()) {
// should notify pool of error so the pooled connection can
// be removed !FIXME!
- throw new SQLException("PooledConnection was reused, without"
+ throw new SQLException("PooledConnection was reused, without"
+ "its previous Connection being closed.");
}
@@ -222,9 +224,9 @@ class PooledConnectionImpl implements Po
// make sure the last connection is marked as closed
if (logicalConnection != null && !logicalConnection.isClosed()) {
- throw new SQLException("PooledConnection was gc'ed, without"
+ throw new SQLException("PooledConnection was gc'ed, without"
+ "its last Connection being closed.");
- }
+ }
}
/**
@@ -265,19 +267,19 @@ class PooledConnectionImpl implements Po
* @param sql a <code>String</code> object that is the SQL statement to
* be sent to the database; may contain one or more '?' IN
* parameters
- * @param resultSetType a result set type; one of
- * <code>ResultSet.TYPE_FORWARD_ONLY</code>,
+ * @param resultSetType a result set type; one of
+ * <code>ResultSet.TYPE_FORWARD_ONLY</code>,
* <code>ResultSet.TYPE_SCROLL_INSENSITIVE</code>, or
* <code>ResultSet.TYPE_SCROLL_SENSITIVE</code>
* @param resultSetConcurrency a concurrency type; one of
* <code>ResultSet.CONCUR_READ_ONLY</code> or
* <code>ResultSet.CONCUR_UPDATABLE</code>
- *
+ *
* @return a {@link PoolablePreparedStatement}
* @see Connection#prepareStatement(String, int, int)
*/
- PreparedStatement prepareStatement(String sql, int resultSetType,
- int resultSetConcurrency)
+ PreparedStatement prepareStatement(String sql, int resultSetType,
+ int resultSetConcurrency)
throws SQLException {
if (pstmtPool == null) {
return connection.prepareStatement(sql, resultSetType, resultSetConcurrency);
@@ -297,14 +299,14 @@ class PooledConnectionImpl implements Po
* Create or obtain a {@link PreparedStatement} from my pool.
* @param sql an SQL statement that may contain one or more '?' IN
* parameter placeholders
- * @param autoGeneratedKeys a flag indicating whether auto-generated keys
+ * @param autoGeneratedKeys a flag indicating whether auto-generated keys
* should be returned; one of
* <code>Statement.RETURN_GENERATED_KEYS</code> or
- * <code>Statement.NO_GENERATED_KEYS</code>
+ * <code>Statement.NO_GENERATED_KEYS</code>
* @return a {@link PoolablePreparedStatement}
* @see Connection#prepareStatement(String, int)
*/
- PreparedStatement prepareStatement(String sql, int autoGeneratedKeys)
+ PreparedStatement prepareStatement(String sql, int autoGeneratedKeys)
throws SQLException {
if (pstmtPool == null) {
return connection.prepareStatement(sql, autoGeneratedKeys);
@@ -400,12 +402,12 @@ class PooledConnectionImpl implements Po
/**
* Create a {*link PooledConnectionImpl.PStmtKey} for the given arguments.
*/
- protected PStmtKeyCPDS createKey(String sql, int resultSetType,
+ protected PStmtKeyCPDS createKey(String sql, int resultSetType,
int resultSetConcurrency) {
return new PStmtKeyCPDS(normalizeSQL(sql), resultSetType,
resultSetConcurrency);
}
-
+
/**
* Create a {*link PooledConnectionImpl.PStmtKey} for the given arguments.
*/
@@ -427,7 +429,7 @@ class PooledConnectionImpl implements Po
* @param key the key for the {*link PreparedStatement} to be created
*/
@Override
- public PoolablePreparedStatement<PStmtKeyCPDS,PoolablePreparedStatementStub> makeObject(PStmtKeyCPDS key) throws Exception {
+ public PooledObject<PoolablePreparedStatement<PStmtKeyCPDS,PoolablePreparedStatementStub>> makeObject(PStmtKeyCPDS key) throws Exception {
if (null == key) {
throw new IllegalArgumentException();
} else {
@@ -435,21 +437,24 @@ class PooledConnectionImpl implements Po
if (null == key.getResultSetType()
&& null == key.getResultSetConcurrency()) {
if (null == key.getAutoGeneratedKeys()) {
- return new PoolablePreparedStatementStub(
- connection.prepareStatement(key.getSql()),
- key, pstmtPool, delegatingConnection);
+ return new PooledObjectImpl<PoolablePreparedStatement<PStmtKeyCPDS,PoolablePreparedStatementStub>>(
+ new PoolablePreparedStatementStub(
+ connection.prepareStatement(key.getSql()),
+ key, pstmtPool, delegatingConnection));
} else {
- return new PoolablePreparedStatementStub(
- connection.prepareStatement(key.getSql(),
- key.getAutoGeneratedKeys().intValue()),
- key, pstmtPool, delegatingConnection);
+ return new PooledObjectImpl<PoolablePreparedStatement<PStmtKeyCPDS,PoolablePreparedStatementStub>>(
+ new PoolablePreparedStatementStub(
+ connection.prepareStatement(key.getSql(),
+ key.getAutoGeneratedKeys().intValue()),
+ key, pstmtPool, delegatingConnection));
}
} else {
- return new PoolablePreparedStatementStub(
- connection.prepareStatement(key.getSql(),
- key.getResultSetType().intValue(),
- key.getResultSetConcurrency().intValue()),
- key, pstmtPool, delegatingConnection);
+ return new PooledObjectImpl<PoolablePreparedStatement<PStmtKeyCPDS,PoolablePreparedStatementStub>>(
+ new PoolablePreparedStatementStub(
+ connection.prepareStatement(key.getSql(),
+ key.getResultSetType().intValue(),
+ key.getResultSetConcurrency().intValue()),
+ key, pstmtPool, delegatingConnection));
}
}
}
@@ -461,9 +466,10 @@ class PooledConnectionImpl implements Po
* @param ppss the {*link PreparedStatement} to be destroyed.
*/
@Override
- public void destroyObject(PStmtKeyCPDS key, PoolablePreparedStatement<PStmtKeyCPDS,PoolablePreparedStatementStub> ppss)
+ public void destroyObject(PStmtKeyCPDS key,
+ PooledObject<PoolablePreparedStatement<PStmtKeyCPDS,PoolablePreparedStatementStub>> p)
throws Exception {
- ppss.getInnermostDelegate().close();
+ p.getObject().getInnermostDelegate().close();
}
/**
@@ -474,7 +480,8 @@ class PooledConnectionImpl implements Po
* @return <tt>true</tt>
*/
@Override
- public boolean validateObject(PStmtKeyCPDS key, PoolablePreparedStatement<PStmtKeyCPDS,PoolablePreparedStatementStub> ppss) {
+ public boolean validateObject(PStmtKeyCPDS key,
+ PooledObject<PoolablePreparedStatement<PStmtKeyCPDS,PoolablePreparedStatementStub>> p) {
return true;
}
@@ -485,9 +492,10 @@ class PooledConnectionImpl implements Po
* @param ppss ignored
*/
@Override
- public void activateObject(PStmtKeyCPDS key, PoolablePreparedStatement<PStmtKeyCPDS,PoolablePreparedStatementStub> ppss)
+ public void activateObject(PStmtKeyCPDS key,
+ PooledObject<PoolablePreparedStatement<PStmtKeyCPDS,PoolablePreparedStatementStub>> p)
throws Exception {
- ppss.activate();
+ p.getObject().activate();
}
/**
@@ -497,15 +505,17 @@ class PooledConnectionImpl implements Po
* @param ppss a {*link PreparedStatement}
*/
@Override
- public void passivateObject(PStmtKeyCPDS key, PoolablePreparedStatement<PStmtKeyCPDS,PoolablePreparedStatementStub> ppss)
+ public void passivateObject(PStmtKeyCPDS key,
+ PooledObject<PoolablePreparedStatement<PStmtKeyCPDS,PoolablePreparedStatementStub>> p)
throws Exception {
+ PoolablePreparedStatement<PStmtKeyCPDS,PoolablePreparedStatementStub> ppss = p.getObject();
ppss.clearParameters();
ppss.passivate();
}
/**
* Returns the value of the accessToUnderlyingConnectionAllowed property.
- *
+ *
* @return true if access to the underlying is allowed, false otherwise.
*/
public synchronized boolean isAccessToUnderlyingConnectionAllowed() {
@@ -516,7 +526,7 @@ class PooledConnectionImpl implements Po
* Sets the value of the accessToUnderlyingConnectionAllowed property.
* It controls if the PoolGuard allows access to the underlying connection.
* (Default: false)
- *
+ *
* @param allow Access to the underlying connection is granted when true.
*/
public synchronized void setAccessToUnderlyingConnectionAllowed(boolean allow) {
Modified: commons/proper/dbcp/trunk/src/java/org/apache/commons/dbcp2/datasources/CPDSConnectionFactory.java
URL: http://svn.apache.org/viewvc/commons/proper/dbcp/trunk/src/java/org/apache/commons/dbcp2/datasources/CPDSConnectionFactory.java?rev=1506687&r1=1506686&r2=1506687&view=diff
==============================================================================
--- commons/proper/dbcp/trunk/src/java/org/apache/commons/dbcp2/datasources/CPDSConnectionFactory.java (original)
+++ commons/proper/dbcp/trunk/src/java/org/apache/commons/dbcp2/datasources/CPDSConnectionFactory.java Wed Jul 24 20:06:42 2013
@@ -33,6 +33,9 @@ import javax.sql.PooledConnection;
import org.apache.commons.dbcp2.Utils;
import org.apache.commons.pool2.ObjectPool;
import org.apache.commons.pool2.PoolableObjectFactory;
+import org.apache.commons.pool2.PooledObject;
+import org.apache.commons.pool2.PooledObjectFactory;
+import org.apache.commons.pool2.impl.PooledObjectImpl;
/**
* A {@link PoolableObjectFactory} that creates
@@ -42,7 +45,7 @@ import org.apache.commons.pool2.Poolable
* @version $Revision$ $Date$
*/
class CPDSConnectionFactory
- implements PoolableObjectFactory<PooledConnectionAndInfo>,
+ implements PooledObjectFactory<PooledConnectionAndInfo>,
ConnectionEventListener, PooledConnectionManager {
private static final String NO_KEY_MESSAGE
@@ -56,7 +59,7 @@ class CPDSConnectionFactory
private final String _username;
private String _password = null;
- /**
+ /**
* Map of PooledConnections for which close events are ignored.
* Connections are muted when they are being validated.
*/
@@ -71,11 +74,11 @@ class CPDSConnectionFactory
/**
* Create a new <tt>PoolableConnectionFactory</tt>.
- *
+ *
* @param cpds the ConnectionPoolDataSource from which to obtain
* PooledConnection's
* @param validationQuery a query to use to {@link #validateObject validate}
- * {@link Connection}s. Should return at least one row. May be
+ * {@link Connection}s. Should return at least one row. May be
* <tt>null</tt>
* @param username
* @param password
@@ -86,10 +89,10 @@ class CPDSConnectionFactory
String password) {
this(cpds, validationQuery, false, username, password);
}
-
+
/**
* Create a new <tt>PoolableConnectionFactory</tt>.
- *
+ *
* @param cpds the ConnectionPoolDataSource from which to obtain
* PooledConnection's
* @param validationQuery a query to use to {@link #validateObject
@@ -114,7 +117,7 @@ class CPDSConnectionFactory
/**
* Returns the object pool used to pool connections created by this factory.
- *
+ *
* @return ObjectPool managing pooled connections
*/
public ObjectPool<PooledConnectionAndInfo> getPool() {
@@ -122,7 +125,7 @@ class CPDSConnectionFactory
}
/**
- *
+ *
* @param pool the {@link ObjectPool} in which to pool those {@link
* Connection}s
*/
@@ -131,7 +134,7 @@ class CPDSConnectionFactory
}
@Override
- public synchronized PooledConnectionAndInfo makeObject() {
+ public synchronized PooledObject<PooledConnectionAndInfo> makeObject() {
PooledConnectionAndInfo pci;
try {
PooledConnection pc = null;
@@ -153,24 +156,28 @@ class CPDSConnectionFactory
} catch (SQLException e) {
throw new RuntimeException(e.getMessage());
}
- return pci;
+ return new PooledObjectImpl<>(pci);
}
/**
* Closes the PooledConnection and stops listening for events from it.
*/
@Override
- public void destroyObject(PooledConnectionAndInfo pci) throws Exception {
+ public void destroyObject(PooledObject<PooledConnectionAndInfo> p) throws Exception {
+ doDestroyObject(p.getObject());
+ }
+
+ private void doDestroyObject(PooledConnectionAndInfo pci) throws Exception{
PooledConnection pc = pci.getPooledConnection();
pc.removeConnectionEventListener(this);
pcMap.remove(pc);
- pc.close();
+ pc.close();
}
@Override
- public boolean validateObject(PooledConnectionAndInfo obj) {
+ public boolean validateObject(PooledObject<PooledConnectionAndInfo> p) {
boolean valid = false;
- PooledConnection pconn = obj.getPooledConnection();
+ PooledConnection pconn = p.getObject().getPooledConnection();
String query = _validationQuery;
if (null != query) {
Connection conn = null;
@@ -208,11 +215,11 @@ class CPDSConnectionFactory
}
@Override
- public void passivateObject(PooledConnectionAndInfo obj) {
+ public void passivateObject(PooledObject<PooledConnectionAndInfo> p) {
}
@Override
- public void activateObject(PooledConnectionAndInfo obj) {
+ public void activateObject(PooledObject<PooledConnectionAndInfo> p) {
}
// ***********************************************************************
@@ -243,7 +250,7 @@ class CPDSConnectionFactory
+ "NOT BE RETURNED TO THE POOL");
pc.removeConnectionEventListener(this);
try {
- destroyObject(pci);
+ doDestroyObject(pci);
} catch (Exception e2) {
System.err.println("EXCEPTION WHILE DESTROYING OBJECT "
+ pci);
@@ -278,11 +285,11 @@ class CPDSConnectionFactory
e.printStackTrace();
}
}
-
+
// ***********************************************************************
// PooledConnectionManager implementation
// ***********************************************************************
-
+
/**
* Invalidates the PooledConnection in the pool. The CPDSConnectionFactory
* closes the connection and pool counters are updated appropriately.
@@ -300,19 +307,19 @@ class CPDSConnectionFactory
_pool.close(); // Clear any other instances in this pool and kill others as they come back
} catch (Exception ex) {
throw (SQLException) new SQLException("Error invalidating connection").initCause(ex);
- }
+ }
}
-
+
/**
* Sets the database password used when creating new connections.
- *
+ *
* @param password new password
*/
@Override
public synchronized void setPassword(String password) {
_password = password;
}
-
+
/**
* Verifies that the username matches the user whose connections are being managed by this
* factory and closes the pool if this is the case; otherwise does nothing.
@@ -328,7 +335,7 @@ class CPDSConnectionFactory
_pool.close();
} catch (Exception ex) {
throw (SQLException) new SQLException("Error closing connection pool").initCause(ex);
- }
+ }
}
-
+
}
Modified: commons/proper/dbcp/trunk/src/java/org/apache/commons/dbcp2/datasources/KeyedCPDSConnectionFactory.java
URL: http://svn.apache.org/viewvc/commons/proper/dbcp/trunk/src/java/org/apache/commons/dbcp2/datasources/KeyedCPDSConnectionFactory.java?rev=1506687&r1=1506686&r2=1506687&view=diff
==============================================================================
--- commons/proper/dbcp/trunk/src/java/org/apache/commons/dbcp2/datasources/KeyedCPDSConnectionFactory.java (original)
+++ commons/proper/dbcp/trunk/src/java/org/apache/commons/dbcp2/datasources/KeyedCPDSConnectionFactory.java Wed Jul 24 20:06:42 2013
@@ -32,7 +32,9 @@ import javax.sql.PooledConnection;
import org.apache.commons.dbcp2.Utils;
import org.apache.commons.pool2.KeyedObjectPool;
-import org.apache.commons.pool2.KeyedPoolableObjectFactory;
+import org.apache.commons.pool2.KeyedPooledObjectFactory;
+import org.apache.commons.pool2.PooledObject;
+import org.apache.commons.pool2.impl.PooledObjectImpl;
/**
* A {*link PoolableObjectFactory} that creates
@@ -42,7 +44,7 @@ import org.apache.commons.pool2.KeyedPoo
* @version $Revision$ $Date$
*/
class KeyedCPDSConnectionFactory
- implements KeyedPoolableObjectFactory<UserPassKey,PooledConnectionAndInfo>,
+ implements KeyedPooledObjectFactory<UserPassKey,PooledConnectionAndInfo>,
ConnectionEventListener, PooledConnectionManager {
private static final String NO_KEY_MESSAGE
@@ -53,14 +55,14 @@ class KeyedCPDSConnectionFactory
private final String _validationQuery;
private final boolean _rollbackAfterValidation;
private KeyedObjectPool<UserPassKey,PooledConnectionAndInfo> _pool;
-
- /**
+
+ /**
* Map of PooledConnections for which close events are ignored.
* Connections are muted when they are being validated.
*/
private final Set<PooledConnection> validatingSet =
new HashSet<PooledConnection>();
-
+
/**
* Map of PooledConnectionAndInfo instances
*/
@@ -75,7 +77,7 @@ class KeyedCPDSConnectionFactory
*/
public KeyedCPDSConnectionFactory(ConnectionPoolDataSource cpds,
String validationQuery) {
- this(cpds , validationQuery, false);
+ this(cpds , validationQuery, false);
}
/**
@@ -87,7 +89,7 @@ class KeyedCPDSConnectionFactory
* @param rollbackAfterValidation whether a rollback should be issued after
* {@link #validateObject validating} {@link Connection}s.
*/
- public KeyedCPDSConnectionFactory(ConnectionPoolDataSource cpds,
+ public KeyedCPDSConnectionFactory(ConnectionPoolDataSource cpds,
String validationQuery,
boolean rollbackAfterValidation) {
_cpds = cpds;
@@ -101,7 +103,7 @@ class KeyedCPDSConnectionFactory
/**
* Returns the keyed object pool used to pool connections created by this factory.
- *
+ *
* @return KeyedObjectPool managing pooled connections
*/
public KeyedObjectPool<UserPassKey,PooledConnectionAndInfo> getPool() {
@@ -110,13 +112,13 @@ class KeyedCPDSConnectionFactory
/**
* Creates a new {@link PooledConnectionAndInfo} from the given {@link UserPassKey}.
- *
+ *
* @param upkey {@link UserPassKey} containing user credentials
* @throws SQLException if the connection could not be created.
* @see org.apache.commons.pool2.KeyedPoolableObjectFactory#makeObject(java.lang.Object)
*/
@Override
- public synchronized PooledConnectionAndInfo makeObject(UserPassKey upkey)
+ public synchronized PooledObject<PooledConnectionAndInfo> makeObject(UserPassKey upkey)
throws Exception {
PooledConnectionAndInfo pci = null;
@@ -139,33 +141,33 @@ class KeyedCPDSConnectionFactory
pci = new PooledConnectionAndInfo(pc, username, password);
pcMap.put(pc, pci);
- return pci;
+ return new PooledObjectImpl<>(pci);
}
/**
* Closes the PooledConnection and stops listening for events from it.
*/
@Override
- public void destroyObject(UserPassKey key, PooledConnectionAndInfo pci)
+ public void destroyObject(UserPassKey key, PooledObject<PooledConnectionAndInfo> p)
throws Exception {
- PooledConnection pc = pci.getPooledConnection();
+ PooledConnection pc = p.getObject().getPooledConnection();
pc.removeConnectionEventListener(this);
pcMap.remove(pc);
- pc.close();
+ pc.close();
}
/**
* Validates a pooled connection.
- *
+ *
* @param key ignored
* @param pci {@link PooledConnectionAndInfo} containing the connection to validate
* @return true if validation suceeds
*/
@Override
public boolean validateObject(UserPassKey key,
- PooledConnectionAndInfo pci) {
+ PooledObject<PooledConnectionAndInfo> p) {
boolean valid = false;
- PooledConnection pconn = pci.getPooledConnection();
+ PooledConnection pconn = p.getObject().getPooledConnection();
String query = _validationQuery;
if (null != query) {
Connection conn = null;
@@ -203,11 +205,11 @@ class KeyedCPDSConnectionFactory
}
@Override
- public void passivateObject(UserPassKey key, PooledConnectionAndInfo pci) {
+ public void passivateObject(UserPassKey key, PooledObject<PooledConnectionAndInfo> p) {
}
@Override
- public void activateObject(UserPassKey key, PooledConnectionAndInfo pci) {
+ public void activateObject(UserPassKey key, PooledObject<PooledConnectionAndInfo> p) {
}
// ***********************************************************************
@@ -273,11 +275,11 @@ class KeyedCPDSConnectionFactory
e.printStackTrace();
}
}
-
+
// ***********************************************************************
// PooledConnectionManager implementation
// ***********************************************************************
-
+
/**
* Invalidates the PooledConnection in the pool. The KeyedCPDSConnectionFactory
* closes the connection and pool counters are updated appropriately.
@@ -299,14 +301,14 @@ class KeyedCPDSConnectionFactory
throw (SQLException) new SQLException("Error invalidating connection").initCause(ex);
}
}
-
+
/**
* Does nothing. This factory does not cache user credentials.
*/
@Override
public void setPassword(String password) {
}
-
+
/**
* This implementation does not fully close the KeyedObjectPool, as
* this would affect all users. Instead, it clears the pool associated
@@ -318,7 +320,7 @@ class KeyedCPDSConnectionFactory
_pool.clear(new UserPassKey(username, null));
} catch (Exception ex) {
throw (SQLException) new SQLException("Error closing connection pool").initCause(ex);
- }
+ }
}
-
+
}