Class KeyedCPDSConnectionFactory
java.lang.Object
org.apache.commons.dbcp2.datasources.KeyedCPDSConnectionFactory
- All Implemented Interfaces:
EventListener,ConnectionEventListener,PooledConnectionManager,org.apache.commons.pool2.KeyedPooledObjectFactory<UserPassKey,PooledConnectionAndInfo>
final class KeyedCPDSConnectionFactory
extends Object
implements org.apache.commons.pool2.KeyedPooledObjectFactory<UserPassKey,PooledConnectionAndInfo>, ConnectionEventListener, PooledConnectionManager
A
KeyedPooledObjectFactory 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.KeyedObjectPool<UserPassKey, PooledConnectionAndInfo> private final booleanprivate final Set<PooledConnection> Map of PooledConnections for which close events are ignored.private final Stringprivate final Duration -
Constructor Summary
ConstructorsConstructorDescriptionKeyedCPDSConnectionFactory(ConnectionPoolDataSource cpds, String validationQuery, int validationQueryTimeoutSeconds, boolean rollbackAfterValidation) Deprecated.KeyedCPDSConnectionFactory(ConnectionPoolDataSource cpds, String validationQuery, Duration validationQueryTimeoutSeconds, boolean rollbackAfterValidation) Creates a newKeyedPoolableConnectionFactory. -
Method Summary
Modifier and TypeMethodDescriptionvoidactivateObject(UserPassKey key, org.apache.commons.pool2.PooledObject<PooledConnectionAndInfo> p) voidThis implementation does not fully close the KeyedObjectPool, as this would affect all users.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(UserPassKey key, org.apache.commons.pool2.PooledObject<PooledConnectionAndInfo> p) Closes the PooledConnection and stops listening for events from it.org.apache.commons.pool2.KeyedObjectPool<UserPassKey, PooledConnectionAndInfo> getPool()Returns the keyed object pool used to pool connections created by this factory.voidInvalidates the PooledConnection in the pool.org.apache.commons.pool2.PooledObject<PooledConnectionAndInfo> makeObject(UserPassKey userPassKey) Creates a newPooledConnectionAndInfofrom the givenUserPassKey.voidpassivateObject(UserPassKey key, org.apache.commons.pool2.PooledObject<PooledConnectionAndInfo> p) voidsetMaxConn(Duration maxConnLifetimeMillis) Sets the maximum lifetime of a connection after which the connection will always fail activation, passivation and validation.voidsetMaxConnLifetime(Duration maxConnLifetimeMillis) Deprecated.UsesetMaxConn(Duration).voidsetMaxConnLifetimeMillis(long maxConnLifetimeMillis) Deprecated.voidsetPassword(String password) Does nothing.voidsetPool(org.apache.commons.pool2.KeyedObjectPool<UserPassKey, PooledConnectionAndInfo> pool) private voidvalidateLifetime(org.apache.commons.pool2.PooledObject<PooledConnectionAndInfo> pooledObject) booleanvalidateObject(UserPassKey key, org.apache.commons.pool2.PooledObject<PooledConnectionAndInfo> pooledObject) Validates a pooled connection.Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, waitMethods inherited from interface org.apache.commons.pool2.KeyedPooledObjectFactory
destroyObject
-
Field Details
-
NO_KEY_MESSAGE
- See Also:
-
cpds
-
validationQuery
-
validationQueryTimeoutDuration
-
rollbackAfterValidation
private final boolean rollbackAfterValidation -
pool
-
maxConnLifetime
-
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
-
KeyedCPDSConnectionFactory
public KeyedCPDSConnectionFactory(ConnectionPoolDataSource cpds, String validationQuery, Duration validationQueryTimeoutSeconds, boolean rollbackAfterValidation) Creates a newKeyedPoolableConnectionFactory.- Parameters:
cpds- the ConnectionPoolDataSource from which to obtain PooledConnectionsvalidationQuery- a query to use tovalidateConnections. Should return at least one row. May benullin which case3Connection.isValid(int)will be used to validate connections.validationQueryTimeoutSeconds- The Duration to allow for the validation query to completerollbackAfterValidation- whether a rollback should be issued aftervalidatingConnections.- Since:
- 2.10.0
-
KeyedCPDSConnectionFactory
@Deprecated public KeyedCPDSConnectionFactory(ConnectionPoolDataSource cpds, String validationQuery, int validationQueryTimeoutSeconds, boolean rollbackAfterValidation) Creates a newKeyedPoolableConnectionFactory.- Parameters:
cpds- the ConnectionPoolDataSource from which to obtain PooledConnectionsvalidationQuery- a query to use tovalidateConnections. Should return at least one row. May benullin which case3Connection.isValid(int)will be used to validate connections.validationQueryTimeoutSeconds- The time, in seconds, to allow for the validation query to completerollbackAfterValidation- whether a rollback should be issued aftervalidatingConnections.
-
-
Method Details
-
activateObject
public void activateObject(UserPassKey key, org.apache.commons.pool2.PooledObject<PooledConnectionAndInfo> p) throws SQLException - Specified by:
activateObjectin interfaceorg.apache.commons.pool2.KeyedPooledObjectFactory<UserPassKey,PooledConnectionAndInfo> - Throws:
SQLException
-
closePool
This implementation does not fully close the KeyedObjectPool, as this would affect all users. Instead, it clears the pool associated with the given user. This method is not currently used.- 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(UserPassKey key, 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.KeyedPooledObjectFactory<UserPassKey,PooledConnectionAndInfo> - Throws:
SQLException
-
getPool
Returns the keyed object pool used to pool connections created by this factory.- Returns:
- KeyedObjectPool managing pooled connections
-
invalidate
Invalidates the PooledConnection in the pool. The KeyedCPDSConnectionFactory closes the connection and pool counters are updated appropriately. Also clears any idle instances associated with the user name that was used to create the PooledConnection. Connections associated with this user are not affected, and they will not be automatically closed on return to the pool.- 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(UserPassKey userPassKey) throws SQLException Creates a newPooledConnectionAndInfofrom the givenUserPassKey.- Specified by:
makeObjectin interfaceorg.apache.commons.pool2.KeyedPooledObjectFactory<UserPassKey,PooledConnectionAndInfo> - Parameters:
userPassKey-UserPassKeycontaining user credentials- Throws:
SQLException- if the connection could not be created.- See Also:
-
passivateObject
public void passivateObject(UserPassKey key, org.apache.commons.pool2.PooledObject<PooledConnectionAndInfo> p) throws SQLException - Specified by:
passivateObjectin interfaceorg.apache.commons.pool2.KeyedPooledObjectFactory<UserPassKey,PooledConnectionAndInfo> - Throws:
SQLException
-
setMaxConn
Sets the maximum lifetime 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 milliseconds.- Since:
- 2.10.0
-
setMaxConnLifetime
Deprecated.UsesetMaxConn(Duration).Sets the maximum lifetime 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 milliseconds.- Since:
- 2.9.0
-
setMaxConnLifetimeMillis
Deprecated.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
Does nothing. This factory does not cache user credentials.- Specified by:
setPasswordin interfacePooledConnectionManager- Parameters:
password- password used when authenticating to the database
-
setPool
public void setPool(org.apache.commons.pool2.KeyedObjectPool<UserPassKey, PooledConnectionAndInfo> pool) -
validateLifetime
private void validateLifetime(org.apache.commons.pool2.PooledObject<PooledConnectionAndInfo> pooledObject) throws SQLException - Throws:
SQLException
-
validateObject
public boolean validateObject(UserPassKey key, org.apache.commons.pool2.PooledObject<PooledConnectionAndInfo> pooledObject) Validates a pooled connection.- Specified by:
validateObjectin interfaceorg.apache.commons.pool2.KeyedPooledObjectFactory<UserPassKey,PooledConnectionAndInfo> - Parameters:
key- ignoredpooledObject- wrappedPooledConnectionAndInfocontaining the connection to validate- Returns:
- true if validation succeeds
-
KeyedCPDSConnectionFactory(ConnectionPoolDataSource, String, Duration, boolean).