Class KeyedCPDSConnectionFactory
- java.lang.Object
-
- org.datanucleus.store.rdbms.datasource.dbcp2.datasources.KeyedCPDSConnectionFactory
-
- All Implemented Interfaces:
java.util.EventListener,javax.sql.ConnectionEventListener,PooledConnectionManager,KeyedPooledObjectFactory<UserPassKey,PooledConnectionAndInfo>
class KeyedCPDSConnectionFactory extends java.lang.Object implements KeyedPooledObjectFactory<UserPassKey,PooledConnectionAndInfo>, javax.sql.ConnectionEventListener, PooledConnectionManager
AKeyedPooledObjectFactorythat createsPoolableConnections.- Since:
- 2.0
-
-
Field Summary
Fields Modifier and Type Field Description private javax.sql.ConnectionPoolDataSourcecpdsprivate longmaxConnLifetimeMillisprivate static java.lang.StringNO_KEY_MESSAGEprivate java.util.Map<javax.sql.PooledConnection,PooledConnectionAndInfo>pcMapMap of PooledConnectionAndInfo instancesprivate KeyedObjectPool<UserPassKey,PooledConnectionAndInfo>poolprivate booleanrollbackAfterValidationprivate java.util.Set<javax.sql.PooledConnection>validatingSetMap of PooledConnections for which close events are ignored.private java.lang.StringvalidationQueryprivate intvalidationQueryTimeoutSeconds
-
Constructor Summary
Constructors Constructor Description KeyedCPDSConnectionFactory(javax.sql.ConnectionPoolDataSource cpds, java.lang.String validationQuery, int validationQueryTimeoutSeconds, boolean rollbackAfterValidation)Create a newKeyedPoolableConnectionFactory.
-
Method Summary
All Methods Instance Methods Concrete Methods Modifier and Type Method Description voidactivateObject(UserPassKey key, PooledObject<PooledConnectionAndInfo> p)Reinitialize an instance to be returned by the pool.voidclosePool(java.lang.String userName)This implementation does not fully close the KeyedObjectPool, as this would affect all users.voidconnectionClosed(javax.sql.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.voidconnectionErrorOccurred(javax.sql.ConnectionEvent event)If a fatal error occurs, close the underlying physical connection so as not to be returned in the futurevoiddestroyObject(UserPassKey key, PooledObject<PooledConnectionAndInfo> p)Closes the PooledConnection and stops listening for events from it.KeyedObjectPool<UserPassKey,PooledConnectionAndInfo>getPool()Returns the keyed object pool used to pool connections created by this factory.voidinvalidate(javax.sql.PooledConnection pc)Invalidates the PooledConnection in the pool.PooledObject<PooledConnectionAndInfo>makeObject(UserPassKey upkey)Creates a newPooledConnectionAndInfofrom the givenUserPassKey.voidpassivateObject(UserPassKey key, PooledObject<PooledConnectionAndInfo> p)Uninitialize an instance to be returned to the idle object pool.voidsetMaxConnLifetimeMillis(long maxConnLifetimeMillis)Sets the maximum lifetime in milliseconds of a connection after which the connection will always fail activation, passivation and validation.voidsetPassword(java.lang.String password)Does nothing.voidsetPool(KeyedObjectPool<UserPassKey,PooledConnectionAndInfo> pool)private voidvalidateLifetime(PooledObject<PooledConnectionAndInfo> p)booleanvalidateObject(UserPassKey key, PooledObject<PooledConnectionAndInfo> pooledObject)Validates a pooled connection.
-
-
-
Field Detail
-
NO_KEY_MESSAGE
private static final java.lang.String NO_KEY_MESSAGE
- See Also:
- Constant Field Values
-
cpds
private final javax.sql.ConnectionPoolDataSource cpds
-
validationQuery
private final java.lang.String validationQuery
-
validationQueryTimeoutSeconds
private final int validationQueryTimeoutSeconds
-
rollbackAfterValidation
private final boolean rollbackAfterValidation
-
pool
private KeyedObjectPool<UserPassKey,PooledConnectionAndInfo> pool
-
maxConnLifetimeMillis
private long maxConnLifetimeMillis
-
validatingSet
private final java.util.Set<javax.sql.PooledConnection> validatingSet
Map of PooledConnections for which close events are ignored. Connections are muted when they are being validated.
-
pcMap
private final java.util.Map<javax.sql.PooledConnection,PooledConnectionAndInfo> pcMap
Map of PooledConnectionAndInfo instances
-
-
Constructor Detail
-
KeyedCPDSConnectionFactory
public KeyedCPDSConnectionFactory(javax.sql.ConnectionPoolDataSource cpds, java.lang.String validationQuery, int validationQueryTimeoutSeconds, boolean rollbackAfterValidation)Create 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 Detail
-
setPool
public void setPool(KeyedObjectPool<UserPassKey,PooledConnectionAndInfo> pool)
-
getPool
public KeyedObjectPool<UserPassKey,PooledConnectionAndInfo> getPool()
Returns the keyed object pool used to pool connections created by this factory.- Returns:
- KeyedObjectPool managing pooled connections
-
makeObject
public PooledObject<PooledConnectionAndInfo> makeObject(UserPassKey upkey) throws java.lang.Exception
Creates a newPooledConnectionAndInfofrom the givenUserPassKey.- Specified by:
makeObjectin interfaceKeyedPooledObjectFactory<UserPassKey,PooledConnectionAndInfo>- Parameters:
upkey-UserPassKeycontaining user credentials- Returns:
- a
PooledObjectwrapping an instance that can be served by the pool. - Throws:
java.sql.SQLException- if the connection could not be created.java.lang.Exception- if there is a problem creating a new instance, this will be propagated to the code requesting an object.- See Also:
KeyedPooledObjectFactory.makeObject(java.lang.Object)
-
destroyObject
public void destroyObject(UserPassKey key, PooledObject<PooledConnectionAndInfo> p) throws java.lang.Exception
Closes the PooledConnection and stops listening for events from it.- Specified by:
destroyObjectin interfaceKeyedPooledObjectFactory<UserPassKey,PooledConnectionAndInfo>- Parameters:
key- the key used when selecting the instancep- aPooledObjectwrapping the instance to be destroyed- Throws:
java.lang.Exception- should be avoided as it may be swallowed by the pool implementation.- See Also:
KeyedPooledObjectFactory.validateObject(K, org.datanucleus.store.rdbms.datasource.dbcp2.pool2.PooledObject<V>),KeyedObjectPool.invalidateObject(K, V)
-
validateObject
public boolean validateObject(UserPassKey key, PooledObject<PooledConnectionAndInfo> pooledObject)
Validates a pooled connection.- Specified by:
validateObjectin interfaceKeyedPooledObjectFactory<UserPassKey,PooledConnectionAndInfo>- Parameters:
key- ignoredpooledObject- wrappedPooledConnectionAndInfocontaining the connection to validate- Returns:
- true if validation succeeds
-
passivateObject
public void passivateObject(UserPassKey key, PooledObject<PooledConnectionAndInfo> p) throws java.lang.Exception
Description copied from interface:KeyedPooledObjectFactoryUninitialize an instance to be returned to the idle object pool.- Specified by:
passivateObjectin interfaceKeyedPooledObjectFactory<UserPassKey,PooledConnectionAndInfo>- Parameters:
key- the key used when selecting the objectp- aPooledObjectwrapping the instance to be passivated- Throws:
java.lang.Exception- if there is a problem passivatingobj, this exception may be swallowed by the pool.- See Also:
KeyedPooledObjectFactory.destroyObject(K, org.datanucleus.store.rdbms.datasource.dbcp2.pool2.PooledObject<V>)
-
activateObject
public void activateObject(UserPassKey key, PooledObject<PooledConnectionAndInfo> p) throws java.lang.Exception
Description copied from interface:KeyedPooledObjectFactoryReinitialize an instance to be returned by the pool.- Specified by:
activateObjectin interfaceKeyedPooledObjectFactory<UserPassKey,PooledConnectionAndInfo>- Parameters:
key- the key used when selecting the objectp- aPooledObjectwrapping the instance to be activated- Throws:
java.lang.Exception- if there is a problem activatingobj, this exception may be swallowed by the pool.- See Also:
KeyedPooledObjectFactory.destroyObject(K, org.datanucleus.store.rdbms.datasource.dbcp2.pool2.PooledObject<V>)
-
connectionClosed
public void connectionClosed(javax.sql.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. What we need to do here is to release this PooledConnection from our pool...- Specified by:
connectionClosedin interfacejavax.sql.ConnectionEventListener
-
connectionErrorOccurred
public void connectionErrorOccurred(javax.sql.ConnectionEvent event)
If a fatal error occurs, close the underlying physical connection so as not to be returned in the future- Specified by:
connectionErrorOccurredin interfacejavax.sql.ConnectionEventListener
-
invalidate
public void invalidate(javax.sql.PooledConnection pc) throws java.sql.SQLExceptionInvalidates 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:
java.sql.SQLException- if an SQL error occurs closing the connection
-
setPassword
public void setPassword(java.lang.String password)
Does nothing. This factory does not cache user credentials.- Specified by:
setPasswordin interfacePooledConnectionManager- Parameters:
password- password used when authenticating to the database
-
setMaxConnLifetimeMillis
public void setMaxConnLifetimeMillis(long maxConnLifetimeMillis)
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.
-
closePool
public void closePool(java.lang.String userName) throws java.sql.SQLExceptionThis 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:
java.sql.SQLException- if an error occurs closing idle connections in the pool
-
validateLifetime
private void validateLifetime(PooledObject<PooledConnectionAndInfo> p) throws java.lang.Exception
- Throws:
java.lang.Exception
-
-