Class KeyedCPDSConnectionFactory
java.lang.Object
org.datanucleus.store.rdbms.datasource.dbcp2.datasources.KeyedCPDSConnectionFactory
- All Implemented Interfaces:
EventListener
,ConnectionEventListener
,PooledConnectionManager
,KeyedPooledObjectFactory<UserPassKey,
PooledConnectionAndInfo>
class KeyedCPDSConnectionFactory
extends Object
implements KeyedPooledObjectFactory<UserPassKey,PooledConnectionAndInfo>, ConnectionEventListener, PooledConnectionManager
A
KeyedPooledObjectFactory
that creates PoolableConnection
s.- Since:
- 2.0
-
Field Summary
FieldsModifier and TypeFieldDescriptionprivate final ConnectionPoolDataSource
private long
private static final String
private final Map
<PooledConnection, PooledConnectionAndInfo> Map of PooledConnectionAndInfo instancesprivate final boolean
private final Set
<PooledConnection> Map of PooledConnections for which close events are ignored.private final String
private final int
-
Constructor Summary
ConstructorsConstructorDescriptionKeyedCPDSConnectionFactory
(ConnectionPoolDataSource cpds, String validationQuery, int validationQueryTimeoutSeconds, boolean rollbackAfterValidation) Create a newKeyedPoolableConnectionFactory
. -
Method Summary
Modifier and TypeMethodDescriptionvoid
Reinitialize an instance to be returned by the pool.void
This implementation does not fully close the KeyedObjectPool, as this would affect all users.void
connectionClosed
(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.void
If a fatal error occurs, close the underlying physical connection so as not to be returned in the futurevoid
Closes the PooledConnection and stops listening for events from it.getPool()
Returns the keyed object pool used to pool connections created by this factory.void
Invalidates the PooledConnection in the pool.makeObject
(UserPassKey upkey) Creates a newPooledConnectionAndInfo
from the givenUserPassKey
.void
Uninitialize an instance to be returned to the idle object pool.void
setMaxConnLifetimeMillis
(long maxConnLifetimeMillis) Sets the maximum lifetime in milliseconds of a connection after which the connection will always fail activation, passivation and validation.void
setPassword
(String password) Does nothing.void
private void
boolean
validateObject
(UserPassKey key, PooledObject<PooledConnectionAndInfo> pooledObject) Validates a pooled connection.
-
Field Details
-
NO_KEY_MESSAGE
- See Also:
-
cpds
-
validationQuery
-
validationQueryTimeoutSeconds
private final int validationQueryTimeoutSeconds -
rollbackAfterValidation
private final boolean rollbackAfterValidation -
pool
-
maxConnLifetimeMillis
private long maxConnLifetimeMillis -
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, int validationQueryTimeoutSeconds, boolean rollbackAfterValidation) Create a newKeyedPoolableConnectionFactory
.- Parameters:
cpds
- the ConnectionPoolDataSource from which to obtain PooledConnectionsvalidationQuery
- a query to use tovalidate
Connection
s. Should return at least one row. May benull
in 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 aftervalidating
Connection
s.
-
-
Method Details
-
setPool
-
getPool
Returns the keyed object pool used to pool connections created by this factory.- Returns:
- KeyedObjectPool managing pooled connections
-
makeObject
Creates a newPooledConnectionAndInfo
from the givenUserPassKey
.- Specified by:
makeObject
in interfaceKeyedPooledObjectFactory<UserPassKey,
PooledConnectionAndInfo> - Parameters:
upkey
-UserPassKey
containing user credentials- Returns:
- a
PooledObject
wrapping an instance that can be served by the pool. - Throws:
SQLException
- if the connection could not be created.Exception
- if there is a problem creating a new instance, this will be propagated to the code requesting an object.- See Also:
-
destroyObject
public void destroyObject(UserPassKey key, PooledObject<PooledConnectionAndInfo> p) throws Exception Closes the PooledConnection and stops listening for events from it.- Specified by:
destroyObject
in interfaceKeyedPooledObjectFactory<UserPassKey,
PooledConnectionAndInfo> - Parameters:
key
- the key used when selecting the instancep
- aPooledObject
wrapping the instance to be destroyed- Throws:
Exception
- should be avoided as it may be swallowed by the pool implementation.- See Also:
-
validateObject
Validates a pooled connection.- Specified by:
validateObject
in interfaceKeyedPooledObjectFactory<UserPassKey,
PooledConnectionAndInfo> - Parameters:
key
- ignoredpooledObject
- wrappedPooledConnectionAndInfo
containing the connection to validate- Returns:
- true if validation succeeds
-
passivateObject
public void passivateObject(UserPassKey key, PooledObject<PooledConnectionAndInfo> p) throws Exception Description copied from interface:KeyedPooledObjectFactory
Uninitialize an instance to be returned to the idle object pool.- Specified by:
passivateObject
in interfaceKeyedPooledObjectFactory<UserPassKey,
PooledConnectionAndInfo> - Parameters:
key
- the key used when selecting the objectp
- aPooledObject
wrapping the instance to be passivated- Throws:
Exception
- if there is a problem passivatingobj
, this exception may be swallowed by the pool.- See Also:
-
activateObject
public void activateObject(UserPassKey key, PooledObject<PooledConnectionAndInfo> p) throws Exception Description copied from interface:KeyedPooledObjectFactory
Reinitialize an instance to be returned by the pool.- Specified by:
activateObject
in interfaceKeyedPooledObjectFactory<UserPassKey,
PooledConnectionAndInfo> - Parameters:
key
- the key used when selecting the objectp
- aPooledObject
wrapping the instance to be activated- Throws:
Exception
- if there is a problem activatingobj
, this exception may be swallowed by the pool.- See Also:
-
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:
connectionClosed
in interfaceConnectionEventListener
-
connectionErrorOccurred
If a fatal error occurs, close the underlying physical connection so as not to be returned in the future- Specified by:
connectionErrorOccurred
in interfaceConnectionEventListener
-
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:
invalidate
in interfacePooledConnectionManager
- Parameters:
pc
- PooledConnection to be invalidated- Throws:
SQLException
- if an SQL error occurs closing the connection
-
setPassword
Does nothing. This factory does not cache user credentials.- Specified by:
setPassword
in 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
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:
closePool
in interfacePooledConnectionManager
- Parameters:
userName
- user name- Throws:
SQLException
- if an error occurs closing idle connections in the pool
-
validateLifetime
- Throws:
Exception
-