Class CPDSConnectionFactory
java.lang.Object
org.apache.commons.dbcp2.datasources.CPDSConnectionFactory
- All Implemented Interfaces:
EventListener,ConnectionEventListener,PooledConnectionManager,org.apache.commons.pool2.PooledObjectFactory<PooledConnectionAndInfo>
final class CPDSConnectionFactory
extends Object
implements org.apache.commons.pool2.PooledObjectFactory<PooledConnectionAndInfo>, ConnectionEventListener, PooledConnectionManager
A
PooledObjectFactory that creates PoolableConnections.- Since:
- 2.0
-
Field Summary
FieldsModifier and TypeFieldDescriptionprivate final ConnectionPoolDataSourceprivate Durationprivate static final Stringprivate final Map<PooledConnection, PooledConnectionAndInfo> Map of PooledConnectionAndInfo instancesprivate org.apache.commons.pool2.ObjectPool<PooledConnectionAndInfo> private final booleanprivate UserPassKeyprivate final Set<PooledConnection> Map of PooledConnections for which close events are ignored.private final Stringprivate final Duration -
Constructor Summary
ConstructorsConstructorDescriptionCPDSConnectionFactory(ConnectionPoolDataSource cpds, String validationQuery, int validationQueryTimeoutSeconds, boolean rollbackAfterValidation, String userName, char[] userPassword) Deprecated.CPDSConnectionFactory(ConnectionPoolDataSource cpds, String validationQuery, int validationQueryTimeoutSeconds, boolean rollbackAfterValidation, String userName, String userPassword) CPDSConnectionFactory(ConnectionPoolDataSource cpds, String validationQuery, Duration validationQueryTimeoutDuration, boolean rollbackAfterValidation, String userName, char[] userPassword) Creates a newPoolableConnectionFactory.CPDSConnectionFactory(ConnectionPoolDataSource cpds, String validationQuery, Duration validationQueryTimeoutDuration, boolean rollbackAfterValidation, String userName, String userPassword) Creates a newPoolableConnectionFactory. -
Method Summary
Modifier and TypeMethodDescriptionvoidactivateObject(org.apache.commons.pool2.PooledObject<PooledConnectionAndInfo> p) voidVerifies that the user name matches the user whose connections are being managed by this factory and closes the pool if this is the case; otherwise does nothing.voidconnectionClosed(ConnectionEvent event) This will be called if the Connection returned by the getConnection method came from a PooledConnection, and the user calls the close() method of this connection object.voidIf a fatal error occurs, close the underlying physical connection so as not to be returned in the futurevoiddestroyObject(org.apache.commons.pool2.PooledObject<PooledConnectionAndInfo> p) Closes the PooledConnection and stops listening for events from it.private void(package private) char[](Testing API) Gets the value of password for the default user.org.apache.commons.pool2.ObjectPool<PooledConnectionAndInfo> getPool()Returns the object pool used to pool connections created by this factory.voidInvalidates the PooledConnection in the pool.org.apache.commons.pool2.PooledObject<PooledConnectionAndInfo> voidpassivateObject(org.apache.commons.pool2.PooledObject<PooledConnectionAndInfo> p) voidsetMaxConn(Duration maxConnDuration) Sets the maximum Duration of a connection after which the connection will always fail activation, passivation and validation.voidsetMaxConnLifetime(Duration maxConnDuration) Deprecated.UsesetMaxConn(Duration).voidsetMaxConnLifetimeMillis(long maxConnLifetimeMillis) Deprecated.UsesetMaxConn(Duration).voidsetPassword(char[] userPassword) Sets the database password used when creating new connections.voidsetPassword(String userPassword) Sets the database password used when creating new connections.voidsetPool(org.apache.commons.pool2.ObjectPool<PooledConnectionAndInfo> pool) toString()private voidvalidateLifetime(org.apache.commons.pool2.PooledObject<PooledConnectionAndInfo> p) booleanvalidateObject(org.apache.commons.pool2.PooledObject<PooledConnectionAndInfo> p) Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, wait, wait, waitMethods inherited from interface org.apache.commons.pool2.PooledObjectFactory
destroyObject
-
Field Details
-
NO_KEY_MESSAGE
- See Also:
-
cpds
-
validationQuery
-
validationQueryTimeoutDuration
-
rollbackAfterValidation
private final boolean rollbackAfterValidation -
pool
-
userPassKey
-
maxConnDuration
-
validatingSet
Map of PooledConnections for which close events are ignored. Connections are muted when they are being validated. -
pcMap
Map of PooledConnectionAndInfo instances
-
-
Constructor Details
-
CPDSConnectionFactory
public CPDSConnectionFactory(ConnectionPoolDataSource cpds, String validationQuery, Duration validationQueryTimeoutDuration, boolean rollbackAfterValidation, String userName, char[] userPassword) Creates a newPoolableConnectionFactory.- Parameters:
cpds- the ConnectionPoolDataSource from which to obtain PooledConnection'svalidationQuery- a query to use tovalidateConnections. Should return at least one row. May benullin which caseConnection.isValid(int)will be used to validate connections.validationQueryTimeoutDuration- Timeout Duration before validation failsrollbackAfterValidation- whether a rollback should be issued aftervalidatingConnections.userName- The user name to use to create connectionsuserPassword- The password to use to create connections- Since:
- 2.10.0
-
CPDSConnectionFactory
public CPDSConnectionFactory(ConnectionPoolDataSource cpds, String validationQuery, Duration validationQueryTimeoutDuration, boolean rollbackAfterValidation, String userName, String userPassword) Creates a newPoolableConnectionFactory.- Parameters:
cpds- the ConnectionPoolDataSource from which to obtain PooledConnection'svalidationQuery- a query to use tovalidateConnections. Should return at least one row. May benullin which caseConnection.isValid(int)will be used to validate connections.validationQueryTimeoutDuration- Timeout in seconds before validation failsrollbackAfterValidation- whether a rollback should be issued aftervalidatingConnections.userName- The user name to use to create connectionsuserPassword- The password to use to create connections- Since:
- 2.10.0
-
CPDSConnectionFactory
@Deprecated public CPDSConnectionFactory(ConnectionPoolDataSource cpds, String validationQuery, int validationQueryTimeoutSeconds, boolean rollbackAfterValidation, String userName, char[] userPassword) Deprecated.Creates a newPoolableConnectionFactory.- Parameters:
cpds- the ConnectionPoolDataSource from which to obtain PooledConnection'svalidationQuery- a query to use tovalidateConnections. Should return at least one row. May benullin which caseConnection.isValid(int)will be used to validate connections.validationQueryTimeoutSeconds- Timeout in seconds before validation failsrollbackAfterValidation- whether a rollback should be issued aftervalidatingConnections.userName- The user name to use to create connectionsuserPassword- The password to use to create connections- Since:
- 2.4.0
-
CPDSConnectionFactory
@Deprecated public CPDSConnectionFactory(ConnectionPoolDataSource cpds, String validationQuery, int validationQueryTimeoutSeconds, boolean rollbackAfterValidation, String userName, String userPassword) Deprecated.Creates a newPoolableConnectionFactory.- Parameters:
cpds- the ConnectionPoolDataSource from which to obtain PooledConnection'svalidationQuery- a query to use tovalidateConnections. Should return at least one row. May benullin which caseConnection.isValid(int)will be used to validate connections.validationQueryTimeoutSeconds- Timeout in seconds before validation failsrollbackAfterValidation- whether a rollback should be issued aftervalidatingConnections.userName- The user name to use to create connectionsuserPassword- The password to use to create connections
-
-
Method Details
-
activateObject
public void activateObject(org.apache.commons.pool2.PooledObject<PooledConnectionAndInfo> p) throws SQLException - Specified by:
activateObjectin interfaceorg.apache.commons.pool2.PooledObjectFactory<PooledConnectionAndInfo>- Throws:
SQLException
-
closePool
Verifies that the user name matches the user whose connections are being managed by this factory and closes the pool if this is the case; otherwise does nothing.- Specified by:
closePoolin interfacePooledConnectionManager- Parameters:
userName- user name- Throws:
SQLException- if an error occurs closing idle connections in the pool
-
connectionClosed
This will be called if the Connection returned by the getConnection method came from a PooledConnection, and the user calls the close() method of this connection object. What we need to do here is to release this PooledConnection from our pool...- Specified by:
connectionClosedin interfaceConnectionEventListener
-
connectionErrorOccurred
If a fatal error occurs, close the underlying physical connection so as not to be returned in the future- Specified by:
connectionErrorOccurredin interfaceConnectionEventListener
-
destroyObject
public void destroyObject(org.apache.commons.pool2.PooledObject<PooledConnectionAndInfo> p) throws SQLException Closes the PooledConnection and stops listening for events from it.- Specified by:
destroyObjectin interfaceorg.apache.commons.pool2.PooledObjectFactory<PooledConnectionAndInfo>- Throws:
SQLException
-
doDestroyObject
- Throws:
SQLException
-
getPasswordCharArray
char[] getPasswordCharArray()(Testing API) Gets the value of password for the default user.- Returns:
- value of password.
-
getPool
Returns the object pool used to pool connections created by this factory.- Returns:
- ObjectPool managing pooled connections
-
invalidate
Invalidates the PooledConnection in the pool. The CPDSConnectionFactory closes the connection and pool counters are updated appropriately. Also closes the pool. This ensures that all idle connections are closed and connections that are checked out are closed on return.- Specified by:
invalidatein interfacePooledConnectionManager- Parameters:
pc- PooledConnection to be invalidated- Throws:
SQLException- if an SQL error occurs closing the connection
-
makeObject
public org.apache.commons.pool2.PooledObject<PooledConnectionAndInfo> makeObject() throws SQLException- Specified by:
makeObjectin interfaceorg.apache.commons.pool2.PooledObjectFactory<PooledConnectionAndInfo>- Throws:
SQLException
-
passivateObject
public void passivateObject(org.apache.commons.pool2.PooledObject<PooledConnectionAndInfo> p) throws SQLException - Specified by:
passivateObjectin interfaceorg.apache.commons.pool2.PooledObjectFactory<PooledConnectionAndInfo>- Throws:
SQLException
-
setMaxConn
Sets the maximum Duration of a connection after which the connection will always fail activation, passivation and validation.- Parameters:
maxConnDuration- A value of zero or less indicates an infinite lifetime. The default value is -1 milliseconds.- Since:
- 2.10.0
-
setMaxConnLifetime
Deprecated.UsesetMaxConn(Duration).Sets the maximum lifetime in milliseconds of a connection after which the connection will always fail activation, passivation and validation.- Parameters:
maxConnDuration- A value of zero or less indicates an infinite lifetime. The default value is -1 milliseconds.- Since:
- 2.9.0
-
setMaxConnLifetimeMillis
Deprecated.UsesetMaxConn(Duration).Sets the maximum lifetime in milliseconds of a connection after which the connection will always fail activation, passivation and validation.- Parameters:
maxConnLifetimeMillis- A value of zero or less indicates an infinite lifetime. The default value is -1.
-
setPassword
public void setPassword(char[] userPassword) Sets the database password used when creating new connections.- Parameters:
userPassword- new password
-
setPassword
Sets the database password used when creating new connections.- Specified by:
setPasswordin interfacePooledConnectionManager- Parameters:
userPassword- new password
-
setPool
- Parameters:
pool- theObjectPoolin which to pool thoseConnections
-
toString
-
validateLifetime
private void validateLifetime(org.apache.commons.pool2.PooledObject<PooledConnectionAndInfo> p) throws SQLException - Throws:
SQLException
-
validateObject
- Specified by:
validateObjectin interfaceorg.apache.commons.pool2.PooledObjectFactory<PooledConnectionAndInfo>
-
CPDSConnectionFactory(ConnectionPoolDataSource, String, Duration, boolean, String, char[]).