Class CPDSConnectionFactory
java.lang.Object
org.datanucleus.store.rdbms.datasource.dbcp2.datasources.CPDSConnectionFactory
- All Implemented Interfaces:
EventListener
,ConnectionEventListener
,PooledConnectionManager
,PooledObjectFactory<PooledConnectionAndInfo>
class CPDSConnectionFactory
extends Object
implements PooledObjectFactory<PooledConnectionAndInfo>, ConnectionEventListener, PooledConnectionManager
A
PooledObjectFactory
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 ObjectPool
<PooledConnectionAndInfo> private final boolean
private final String
private char[]
private final Set
<PooledConnection> Map of PooledConnections for which close events are ignored.private final String
private final int
-
Constructor Summary
ConstructorsConstructorDescriptionCPDSConnectionFactory
(ConnectionPoolDataSource cpds, String validationQuery, int validationQueryTimeoutSeconds, boolean rollbackAfterValidation, String userName, char[] userPassword) Creates a newPoolableConnectionFactory
.CPDSConnectionFactory
(ConnectionPoolDataSource cpds, String validationQuery, int validationQueryTimeoutSeconds, boolean rollbackAfterValidation, String userName, String userPassword) Creates a newPoolableConnectionFactory
. -
Method Summary
Modifier and TypeMethodDescriptionvoid
Reinitializes an instance to be returned by the pool.void
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.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.private void
(package private) char[]
(Testing API) Gets the value of password for the default user.getPool()
Returns the object pool used to pool connections created by this factory.void
Invalidates the PooledConnection in the pool.Creates an instance that can be served by the pool and wrap it in aPooledObject
to be managed by the pool.void
Uninitializes 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
(char[] userPassword) Sets the database password used when creating new connections.void
setPassword
(String userPassword) Sets the database password used when creating new connections.void
toString()
private void
boolean
Ensures that the instance is safe to be returned by the pool.
-
Field Details
-
NO_KEY_MESSAGE
- See Also:
-
cpds
-
validationQuery
-
validationQueryTimeoutSeconds
private final int validationQueryTimeoutSeconds -
rollbackAfterValidation
private final boolean rollbackAfterValidation -
pool
-
userName
-
userPassword
private char[] userPassword -
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
-
CPDSConnectionFactory
public CPDSConnectionFactory(ConnectionPoolDataSource cpds, String validationQuery, int validationQueryTimeoutSeconds, boolean rollbackAfterValidation, String userName, char[] userPassword) Creates a newPoolableConnectionFactory
.- Parameters:
cpds
- the ConnectionPoolDataSource from which to obtain PooledConnection'svalidationQuery
- a query to use tovalidate
Connection
s. Should return at least one row. May benull
in which caseConnection.isValid(int)
will be used to validate connections.validationQueryTimeoutSeconds
- Timeout in seconds before validation failsrollbackAfterValidation
- whether a rollback should be issued aftervalidating
Connection
s.userName
- The user name to use to create connectionsuserPassword
- The password to use to create connections- Since:
- 2.4.0
-
CPDSConnectionFactory
public CPDSConnectionFactory(ConnectionPoolDataSource cpds, String validationQuery, int validationQueryTimeoutSeconds, boolean rollbackAfterValidation, String userName, String userPassword) Creates a newPoolableConnectionFactory
.- Parameters:
cpds
- the ConnectionPoolDataSource from which to obtain PooledConnection'svalidationQuery
- a query to use tovalidate
Connection
s. Should return at least one row. May benull
in which caseConnection.isValid(int)
will be used to validate connections.validationQueryTimeoutSeconds
- Timeout in seconds before validation failsrollbackAfterValidation
- whether a rollback should be issued aftervalidating
Connection
s.userName
- The user name to use to create connectionsuserPassword
- The password to use to create connections
-
-
Method Details
-
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
-
setPool
- Parameters:
pool
- theObjectPool
in which to pool thoseConnection
s
-
makeObject
Description copied from interface:PooledObjectFactory
Creates an instance that can be served by the pool and wrap it in aPooledObject
to be managed by the pool.- Specified by:
makeObject
in interfacePooledObjectFactory<PooledConnectionAndInfo>
- Returns:
- a
PooledObject
wrapping an instance that can be served by the pool
-
destroyObject
Closes the PooledConnection and stops listening for events from it.- Specified by:
destroyObject
in interfacePooledObjectFactory<PooledConnectionAndInfo>
- Parameters:
p
- aPooledObject
wrapping the instance to be destroyed- Throws:
Exception
- should be avoided as it may be swallowed by the pool implementation.- See Also:
-
doDestroyObject
- Throws:
Exception
-
validateObject
Description copied from interface:PooledObjectFactory
Ensures that the instance is safe to be returned by the pool.- Specified by:
validateObject
in interfacePooledObjectFactory<PooledConnectionAndInfo>
- Parameters:
p
- aPooledObject
wrapping the instance to be validated- Returns:
false
ifobj
is not valid and should be dropped from the pool,true
otherwise.
-
passivateObject
Description copied from interface:PooledObjectFactory
Uninitializes an instance to be returned to the idle object pool.- Specified by:
passivateObject
in interfacePooledObjectFactory<PooledConnectionAndInfo>
- Parameters:
p
- 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
Description copied from interface:PooledObjectFactory
Reinitializes an instance to be returned by the pool.- Specified by:
activateObject
in interfacePooledObjectFactory<PooledConnectionAndInfo>
- Parameters:
p
- 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 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:
invalidate
in interfacePooledConnectionManager
- Parameters:
pc
- PooledConnection to be invalidated- Throws:
SQLException
- if an SQL error occurs closing the connection
-
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:
setPassword
in interfacePooledConnectionManager
- Parameters:
userPassword
- new password
-
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
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:
closePool
in interfacePooledConnectionManager
- Parameters:
userName
- user name- Throws:
SQLException
- if an error occurs closing idle connections in the pool
-
validateLifetime
- Throws:
Exception
-
toString
-