Class CPDSConnectionFactory
- java.lang.Object
-
- org.datanucleus.store.rdbms.datasource.dbcp2.datasources.CPDSConnectionFactory
-
- All Implemented Interfaces:
java.util.EventListener
,javax.sql.ConnectionEventListener
,PooledConnectionManager
,PooledObjectFactory<PooledConnectionAndInfo>
class CPDSConnectionFactory extends java.lang.Object implements PooledObjectFactory<PooledConnectionAndInfo>, javax.sql.ConnectionEventListener, PooledConnectionManager
APooledObjectFactory
that createsPoolableConnection
s.- Since:
- 2.0
-
-
Field Summary
Fields Modifier and Type Field Description private javax.sql.ConnectionPoolDataSource
cpds
private long
maxConnLifetimeMillis
private static java.lang.String
NO_KEY_MESSAGE
private java.util.Map<javax.sql.PooledConnection,PooledConnectionAndInfo>
pcMap
Map of PooledConnectionAndInfo instancesprivate ObjectPool<PooledConnectionAndInfo>
pool
private boolean
rollbackAfterValidation
private java.lang.String
userName
private char[]
userPassword
private java.util.Set<javax.sql.PooledConnection>
validatingSet
Map of PooledConnections for which close events are ignored.private java.lang.String
validationQuery
private int
validationQueryTimeoutSeconds
-
Constructor Summary
Constructors Constructor Description CPDSConnectionFactory(javax.sql.ConnectionPoolDataSource cpds, java.lang.String validationQuery, int validationQueryTimeoutSeconds, boolean rollbackAfterValidation, java.lang.String userName, char[] userPassword)
Creates a newPoolableConnectionFactory
.CPDSConnectionFactory(javax.sql.ConnectionPoolDataSource cpds, java.lang.String validationQuery, int validationQueryTimeoutSeconds, boolean rollbackAfterValidation, java.lang.String userName, java.lang.String userPassword)
Creates a newPoolableConnectionFactory
.
-
Method Summary
All Methods Instance Methods Concrete Methods Modifier and Type Method Description void
activateObject(PooledObject<PooledConnectionAndInfo> p)
Reinitializes an instance to be returned by the pool.void
closePool(java.lang.String userName)
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(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.void
connectionErrorOccurred(javax.sql.ConnectionEvent event)
If a fatal error occurs, close the underlying physical connection so as not to be returned in the futurevoid
destroyObject(PooledObject<PooledConnectionAndInfo> p)
Closes the PooledConnection and stops listening for events from it.private void
doDestroyObject(PooledConnectionAndInfo pci)
(package private) char[]
getPasswordCharArray()
(Testing API) Gets the value of password for the default user.ObjectPool<PooledConnectionAndInfo>
getPool()
Returns the object pool used to pool connections created by this factory.void
invalidate(javax.sql.PooledConnection pc)
Invalidates the PooledConnection in the pool.PooledObject<PooledConnectionAndInfo>
makeObject()
Creates an instance that can be served by the pool and wrap it in aPooledObject
to be managed by the pool.void
passivateObject(PooledObject<PooledConnectionAndInfo> p)
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(java.lang.String userPassword)
Sets the database password used when creating new connections.void
setPool(ObjectPool<PooledConnectionAndInfo> pool)
java.lang.String
toString()
private void
validateLifetime(PooledObject<PooledConnectionAndInfo> p)
boolean
validateObject(PooledObject<PooledConnectionAndInfo> p)
Ensures that the instance is safe to be returned by the pool.
-
-
-
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 ObjectPool<PooledConnectionAndInfo> pool
-
userName
private final java.lang.String userName
-
userPassword
private char[] userPassword
-
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
-
CPDSConnectionFactory
public CPDSConnectionFactory(javax.sql.ConnectionPoolDataSource cpds, java.lang.String validationQuery, int validationQueryTimeoutSeconds, boolean rollbackAfterValidation, java.lang.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(javax.sql.ConnectionPoolDataSource cpds, java.lang.String validationQuery, int validationQueryTimeoutSeconds, boolean rollbackAfterValidation, java.lang.String userName, java.lang.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 Detail
-
getPasswordCharArray
char[] getPasswordCharArray()
(Testing API) Gets the value of password for the default user.- Returns:
- value of password.
-
getPool
public ObjectPool<PooledConnectionAndInfo> getPool()
Returns the object pool used to pool connections created by this factory.- Returns:
- ObjectPool managing pooled connections
-
setPool
public void setPool(ObjectPool<PooledConnectionAndInfo> pool)
- Parameters:
pool
- theObjectPool
in which to pool thoseConnection
s
-
makeObject
public PooledObject<PooledConnectionAndInfo> 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
public void destroyObject(PooledObject<PooledConnectionAndInfo> p) throws java.lang.Exception
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:
java.lang.Exception
- should be avoided as it may be swallowed by the pool implementation.- See Also:
PooledObjectFactory.validateObject(org.datanucleus.store.rdbms.datasource.dbcp2.pool2.PooledObject<T>)
,ObjectPool.invalidateObject(T)
-
doDestroyObject
private void doDestroyObject(PooledConnectionAndInfo pci) throws java.lang.Exception
- Throws:
java.lang.Exception
-
validateObject
public boolean validateObject(PooledObject<PooledConnectionAndInfo> p)
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
public void passivateObject(PooledObject<PooledConnectionAndInfo> p) throws java.lang.Exception
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:
java.lang.Exception
- if there is a problem passivatingobj
, this exception may be swallowed by the pool.- See Also:
PooledObjectFactory.destroyObject(org.datanucleus.store.rdbms.datasource.dbcp2.pool2.PooledObject<T>)
-
activateObject
public void activateObject(PooledObject<PooledConnectionAndInfo> p) throws java.lang.Exception
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:
java.lang.Exception
- if there is a problem activatingobj
, this exception may be swallowed by the pool.- See Also:
PooledObjectFactory.destroyObject(org.datanucleus.store.rdbms.datasource.dbcp2.pool2.PooledObject<T>)
-
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:
connectionClosed
in 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:
connectionErrorOccurred
in interfacejavax.sql.ConnectionEventListener
-
invalidate
public void invalidate(javax.sql.PooledConnection pc) throws java.sql.SQLException
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:
java.sql.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
public void setPassword(java.lang.String userPassword)
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
public void closePool(java.lang.String userName) throws java.sql.SQLException
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:
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
-
toString
public java.lang.String toString()
- Overrides:
toString
in classjava.lang.Object
- Since:
- 2.6.0
-
-