Class PooledConnectionImpl
- java.lang.Object
-
- org.datanucleus.store.rdbms.datasource.dbcp2.cpdsadapter.PooledConnectionImpl
-
- All Implemented Interfaces:
javax.sql.PooledConnection,KeyedPooledObjectFactory<PStmtKey,DelegatingPreparedStatement>
class PooledConnectionImpl extends java.lang.Object implements javax.sql.PooledConnection, KeyedPooledObjectFactory<PStmtKey,DelegatingPreparedStatement>
Implementation of PooledConnection that is returned by PooledConnectionDataSource.- Since:
- 2.0
-
-
Field Summary
Fields Modifier and Type Field Description private booleanaccessToUnderlyingConnectionAllowedControls access to the underlying connection.private booleanclosedFlag set to true, onceclose()is called.private static java.lang.StringCLOSEDprivate java.sql.ConnectionconnectionThe JDBC database connection that represents the physical db connection.private DelegatingConnection<?>delegatingConnectionA DelegatingConnection used to create a PoolablePreparedStatementStub.private java.util.Vector<javax.sql.ConnectionEventListener>eventListenersConnectionEventListeners.private java.sql.ConnectionlogicalConnectionThe JDBC database logical connection.private KeyedObjectPool<PStmtKey,DelegatingPreparedStatement>pStmtPoolMy pool ofPreparedStatements.private java.util.Vector<javax.sql.StatementEventListener>statementEventListenersStatementEventListeners.
-
Constructor Summary
Constructors Constructor Description PooledConnectionImpl(java.sql.Connection connection)Wraps the real connection.
-
Method Summary
All Methods Instance Methods Concrete Methods Modifier and Type Method Description voidactivateObject(PStmtKey key, PooledObject<DelegatingPreparedStatement> pooledObject)MyKeyedPooledObjectFactorymethod for activatingPreparedStatements.voidaddConnectionEventListener(javax.sql.ConnectionEventListener listener)voidaddStatementEventListener(javax.sql.StatementEventListener listener)private voidassertOpen()Throws an SQLException, if isClosed is truevoidclose()Closes the physical connection and marks thisPooledConnectionso that it may not be used to generate any more logicalConnections.protected PStmtKeycreateKey(java.lang.String sql)Creates aPStmtKeyfor the given arguments.protected PStmtKeycreateKey(java.lang.String sql, int autoGeneratedKeys)Creates aPStmtKeyfor the given arguments.protected PStmtKeycreateKey(java.lang.String sql, int[] columnIndexes)Creates aPStmtKeyfor the given arguments.protected PStmtKeycreateKey(java.lang.String sql, int resultSetType, int resultSetConcurrency)Creates aPStmtKeyfor the given arguments.protected PStmtKeycreateKey(java.lang.String sql, int resultSetType, int resultSetConcurrency, int resultSetHoldability)Creates aPStmtKeyfor the given arguments.protected PStmtKeycreateKey(java.lang.String sql, int resultSetType, int resultSetConcurrency, int resultSetHoldability, PoolingConnection.StatementType statementType)Creates aPStmtKeyfor the given arguments.protected PStmtKeycreateKey(java.lang.String sql, int resultSetType, int resultSetConcurrency, PoolingConnection.StatementType statementType)Creates aPStmtKeyfor the given arguments.protected PStmtKeycreateKey(java.lang.String sql, java.lang.String[] columnNames)Creates aPStmtKeyfor the given arguments.protected PStmtKeycreateKey(java.lang.String sql, PoolingConnection.StatementType statementType)Creates aPStmtKeyfor the given arguments.voiddestroyObject(PStmtKey key, PooledObject<DelegatingPreparedStatement> pooledObject)MyKeyedPooledObjectFactorymethod for destroyingPreparedStatements.protected voidfinalize()Closes the physical connection and checks that the logical connection was closed as well.private java.lang.StringgetCatalogOrNull()java.sql.ConnectiongetConnection()Returns a JDBC connection.private java.lang.StringgetSchemaOrNull()booleanisAccessToUnderlyingConnectionAllowed()Returns the value of the accessToUnderlyingConnectionAllowed property.PooledObject<DelegatingPreparedStatement>makeObject(PStmtKey key)MyKeyedPooledObjectFactorymethod for creatingPreparedStatements.protected java.lang.StringnormalizeSQL(java.lang.String sql)Normalizes the given SQL statement, producing a canonical form that is semantically equivalent to the original.(package private) voidnotifyListeners()Sends a connectionClosed event.voidpassivateObject(PStmtKey key, PooledObject<DelegatingPreparedStatement> pooledObject)MyKeyedPooledObjectFactorymethod for passivatingPreparedStatements.(package private) java.sql.CallableStatementprepareCall(java.lang.String sql)Creates or obtains aCallableStatementfrom my pool.(package private) java.sql.CallableStatementprepareCall(java.lang.String sql, int resultSetType, int resultSetConcurrency)Creates or obtains aCallableStatementfrom my pool.(package private) java.sql.CallableStatementprepareCall(java.lang.String sql, int resultSetType, int resultSetConcurrency, int resultSetHoldability)Creates or obtains aCallableStatementfrom my pool.(package private) java.sql.PreparedStatementprepareStatement(java.lang.String sql)Creates or obtains aPreparedStatementfrom my pool.(package private) java.sql.PreparedStatementprepareStatement(java.lang.String sql, int autoGeneratedKeys)Creates or obtains aPreparedStatementfrom my pool.(package private) java.sql.PreparedStatementprepareStatement(java.lang.String sql, int[] columnIndexes)(package private) java.sql.PreparedStatementprepareStatement(java.lang.String sql, int resultSetType, int resultSetConcurrency)Creates or obtains aPreparedStatementfrom my pool.(package private) java.sql.PreparedStatementprepareStatement(java.lang.String sql, int resultSetType, int resultSetConcurrency, int resultSetHoldability)(package private) java.sql.PreparedStatementprepareStatement(java.lang.String sql, java.lang.String[] columnNames)voidremoveConnectionEventListener(javax.sql.ConnectionEventListener listener)voidremoveStatementEventListener(javax.sql.StatementEventListener listener)voidsetAccessToUnderlyingConnectionAllowed(boolean allow)Sets the value of the accessToUnderlyingConnectionAllowed property.voidsetStatementPool(KeyedObjectPool<PStmtKey,DelegatingPreparedStatement> statementPool)java.lang.StringtoString()booleanvalidateObject(PStmtKey key, PooledObject<DelegatingPreparedStatement> pooledObject)MyKeyedPooledObjectFactorymethod for validatingPreparedStatements.
-
-
-
Field Detail
-
CLOSED
private static final java.lang.String CLOSED
- See Also:
- Constant Field Values
-
connection
private java.sql.Connection connection
The JDBC database connection that represents the physical db connection.
-
delegatingConnection
private final DelegatingConnection<?> delegatingConnection
A DelegatingConnection used to create a PoolablePreparedStatementStub.
-
logicalConnection
private java.sql.Connection logicalConnection
The JDBC database logical connection.
-
eventListeners
private final java.util.Vector<javax.sql.ConnectionEventListener> eventListeners
ConnectionEventListeners.
-
statementEventListeners
private final java.util.Vector<javax.sql.StatementEventListener> statementEventListeners
StatementEventListeners.
-
closed
private boolean closed
Flag set to true, onceclose()is called.
-
pStmtPool
private KeyedObjectPool<PStmtKey,DelegatingPreparedStatement> pStmtPool
My pool ofPreparedStatements.
-
accessToUnderlyingConnectionAllowed
private boolean accessToUnderlyingConnectionAllowed
Controls access to the underlying connection.
-
-
Method Detail
-
activateObject
public void activateObject(PStmtKey key, PooledObject<DelegatingPreparedStatement> pooledObject) throws java.lang.Exception
MyKeyedPooledObjectFactorymethod for activatingPreparedStatements.- Specified by:
activateObjectin interfaceKeyedPooledObjectFactory<PStmtKey,DelegatingPreparedStatement>- Parameters:
key- Ignored.pooledObject- Ignored.- 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>)
-
addConnectionEventListener
public void addConnectionEventListener(javax.sql.ConnectionEventListener listener)
- Specified by:
addConnectionEventListenerin interfacejavax.sql.PooledConnection
-
addStatementEventListener
public void addStatementEventListener(javax.sql.StatementEventListener listener)
- Specified by:
addStatementEventListenerin interfacejavax.sql.PooledConnection
-
assertOpen
private void assertOpen() throws java.sql.SQLExceptionThrows an SQLException, if isClosed is true- Throws:
java.sql.SQLException
-
close
public void close() throws java.sql.SQLExceptionCloses the physical connection and marks thisPooledConnectionso that it may not be used to generate any more logicalConnections.- Specified by:
closein interfacejavax.sql.PooledConnection- Throws:
java.sql.SQLException- Thrown when an error occurs or the connection is already closed.
-
createKey
protected PStmtKey createKey(java.lang.String sql)
Creates aPStmtKeyfor the given arguments.- Parameters:
sql- The SQL statement.- Returns:
- a
PStmtKeyfor the given arguments.
-
createKey
protected PStmtKey createKey(java.lang.String sql, int autoGeneratedKeys)
Creates aPStmtKeyfor the given arguments.- Parameters:
sql- The SQL statement.autoGeneratedKeys- A flag indicating whether auto-generated keys should be returned; one ofStatement.RETURN_GENERATED_KEYSorStatement.NO_GENERATED_KEYS.- Returns:
- a key to uniquely identify a prepared statement.
-
createKey
protected PStmtKey createKey(java.lang.String sql, int[] columnIndexes)
Creates aPStmtKeyfor the given arguments.- Parameters:
sql- The SQL statement.columnIndexes- An array of column indexes indicating the columns that should be returned from the inserted row or rows.- Returns:
- a key to uniquely identify a prepared statement.
-
createKey
protected PStmtKey createKey(java.lang.String sql, int resultSetType, int resultSetConcurrency)
Creates aPStmtKeyfor the given arguments.- Parameters:
sql- The SQL statement.resultSetType- A result set type; one ofResultSet.TYPE_FORWARD_ONLY,ResultSet.TYPE_SCROLL_INSENSITIVE, orResultSet.TYPE_SCROLL_SENSITIVE.resultSetConcurrency- A concurrency type; one ofResultSet.CONCUR_READ_ONLYorResultSet.CONCUR_UPDATABLE.- Returns:
- a key to uniquely identify a prepared statement.
-
createKey
protected PStmtKey createKey(java.lang.String sql, int resultSetType, int resultSetConcurrency, int resultSetHoldability)
Creates aPStmtKeyfor the given arguments.- Parameters:
sql- The SQL statement.resultSetType- a result set type; one ofResultSet.TYPE_FORWARD_ONLY,ResultSet.TYPE_SCROLL_INSENSITIVE, orResultSet.TYPE_SCROLL_SENSITIVE.resultSetConcurrency- A concurrency type; one ofResultSet.CONCUR_READ_ONLYorResultSet.CONCUR_UPDATABLEresultSetHoldability- One of the followingResultSetconstants:ResultSet.HOLD_CURSORS_OVER_COMMITorResultSet.CLOSE_CURSORS_AT_COMMIT.- Returns:
- a key to uniquely identify a prepared statement.
-
createKey
protected PStmtKey createKey(java.lang.String sql, int resultSetType, int resultSetConcurrency, int resultSetHoldability, PoolingConnection.StatementType statementType)
Creates aPStmtKeyfor the given arguments.- Parameters:
sql- The SQL statement.resultSetType- a result set type; one ofResultSet.TYPE_FORWARD_ONLY,ResultSet.TYPE_SCROLL_INSENSITIVE, orResultSet.TYPE_SCROLL_SENSITIVEresultSetConcurrency- A concurrency type; one ofResultSet.CONCUR_READ_ONLYorResultSet.CONCUR_UPDATABLE.resultSetHoldability- One of the followingResultSetconstants:ResultSet.HOLD_CURSORS_OVER_COMMITorResultSet.CLOSE_CURSORS_AT_COMMIT.statementType- The SQL statement type, prepared or callable.- Returns:
- a key to uniquely identify a prepared statement.
- Since:
- 2.4.0
-
createKey
protected PStmtKey createKey(java.lang.String sql, int resultSetType, int resultSetConcurrency, PoolingConnection.StatementType statementType)
Creates aPStmtKeyfor the given arguments.- Parameters:
sql- The SQL statement.resultSetType- A result set type; one ofResultSet.TYPE_FORWARD_ONLY,ResultSet.TYPE_SCROLL_INSENSITIVE, orResultSet.TYPE_SCROLL_SENSITIVE.resultSetConcurrency- A concurrency type; one ofResultSet.CONCUR_READ_ONLYorResultSet.CONCUR_UPDATABLE.statementType- The SQL statement type, prepared or callable.- Returns:
- a key to uniquely identify a prepared statement.
- Since:
- 2.4.0
-
createKey
protected PStmtKey createKey(java.lang.String sql, PoolingConnection.StatementType statementType)
Creates aPStmtKeyfor the given arguments.- Parameters:
sql- The SQL statement.statementType- The SQL statement type, prepared or callable.- Returns:
- a key to uniquely identify a prepared statement.
-
createKey
protected PStmtKey createKey(java.lang.String sql, java.lang.String[] columnNames)
Creates aPStmtKeyfor the given arguments.- Parameters:
sql- The SQL statement.columnNames- An array of column names indicating the columns that should be returned from the inserted row or rows.- Returns:
- a key to uniquely identify a prepared statement.
-
destroyObject
public void destroyObject(PStmtKey key, PooledObject<DelegatingPreparedStatement> pooledObject) throws java.lang.Exception
MyKeyedPooledObjectFactorymethod for destroyingPreparedStatements.- Specified by:
destroyObjectin interfaceKeyedPooledObjectFactory<PStmtKey,DelegatingPreparedStatement>- Parameters:
key- ignoredpooledObject- the wrappedPreparedStatementto 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)
-
finalize
protected void finalize() throws java.lang.ThrowableCloses the physical connection and checks that the logical connection was closed as well.- Overrides:
finalizein classjava.lang.Object- Throws:
java.lang.Throwable
-
getCatalogOrNull
private java.lang.String getCatalogOrNull()
-
getSchemaOrNull
private java.lang.String getSchemaOrNull()
-
getConnection
public java.sql.Connection getConnection() throws java.sql.SQLExceptionReturns a JDBC connection.- Specified by:
getConnectionin interfacejavax.sql.PooledConnection- Returns:
- The database connection.
- Throws:
java.sql.SQLException- if the connection is not open or the previous logical connection is still open
-
isAccessToUnderlyingConnectionAllowed
public boolean isAccessToUnderlyingConnectionAllowed()
Returns the value of the accessToUnderlyingConnectionAllowed property.- Returns:
- true if access to the underlying is allowed, false otherwise.
-
makeObject
public PooledObject<DelegatingPreparedStatement> makeObject(PStmtKey key) throws java.lang.Exception
MyKeyedPooledObjectFactorymethod for creatingPreparedStatements.- Specified by:
makeObjectin interfaceKeyedPooledObjectFactory<PStmtKey,DelegatingPreparedStatement>- Parameters:
key- The key for thePreparedStatementto be created.- Returns:
- a
PooledObjectwrapping an instance that can be served by the pool. - Throws:
java.lang.Exception- if there is a problem creating a new instance, this will be propagated to the code requesting an object.
-
normalizeSQL
protected java.lang.String normalizeSQL(java.lang.String sql)
Normalizes the given SQL statement, producing a canonical form that is semantically equivalent to the original.- Parameters:
sql- The SQL statement.- Returns:
- the normalized SQL statement.
-
notifyListeners
void notifyListeners()
Sends a connectionClosed event.
-
passivateObject
public void passivateObject(PStmtKey key, PooledObject<DelegatingPreparedStatement> pooledObject) throws java.lang.Exception
MyKeyedPooledObjectFactorymethod for passivatingPreparedStatements. Currently invokesPreparedStatement.clearParameters().- Specified by:
passivateObjectin interfaceKeyedPooledObjectFactory<PStmtKey,DelegatingPreparedStatement>- Parameters:
key- ignoredpooledObject- a wrappedPreparedStatement- 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>)
-
prepareCall
java.sql.CallableStatement prepareCall(java.lang.String sql) throws java.sql.SQLExceptionCreates or obtains aCallableStatementfrom my pool.- Parameters:
sql- an SQL statement that may contain one or more '?' parameter placeholders. Typically this statement is specified using JDBC call escape syntax.- Returns:
- a default
CallableStatementobject containing the pre-compiled SQL statement. - Throws:
java.sql.SQLException- Thrown if a database access error occurs or this method is called on a closed connection.- Since:
- 2.4.0
-
prepareCall
java.sql.CallableStatement prepareCall(java.lang.String sql, int resultSetType, int resultSetConcurrency) throws java.sql.SQLExceptionCreates or obtains aCallableStatementfrom my pool.- Parameters:
sql- aStringobject that is the SQL statement to be sent to the database; may contain on or more '?' parameters.resultSetType- a result set type; one ofResultSet.TYPE_FORWARD_ONLY,ResultSet.TYPE_SCROLL_INSENSITIVE, orResultSet.TYPE_SCROLL_SENSITIVE.resultSetConcurrency- a concurrency type; one ofResultSet.CONCUR_READ_ONLYorResultSet.CONCUR_UPDATABLE.- Returns:
- a
CallableStatementobject containing the pre-compiled SQL statement that will produceResultSetobjects with the given type and concurrency. - Throws:
java.sql.SQLException- Thrown if a database access error occurs, this method is called on a closed connection or the given parameters are notResultSetconstants indicating type and concurrency.- Since:
- 2.4.0
-
prepareCall
java.sql.CallableStatement prepareCall(java.lang.String sql, int resultSetType, int resultSetConcurrency, int resultSetHoldability) throws java.sql.SQLExceptionCreates or obtains aCallableStatementfrom my pool.- Parameters:
sql- aStringobject that is the SQL statement to be sent to the database; may contain on or more '?' parameters.resultSetType- one of the followingResultSetconstants:ResultSet.TYPE_FORWARD_ONLY,ResultSet.TYPE_SCROLL_INSENSITIVE, orResultSet.TYPE_SCROLL_SENSITIVE.resultSetConcurrency- one of the followingResultSetconstants:ResultSet.CONCUR_READ_ONLYorResultSet.CONCUR_UPDATABLE.resultSetHoldability- one of the followingResultSetconstants:ResultSet.HOLD_CURSORS_OVER_COMMITorResultSet.CLOSE_CURSORS_AT_COMMIT.- Returns:
- a new
CallableStatementobject, containing the pre-compiled SQL statement, that will generateResultSetobjects with the given type, concurrency, and holdability. - Throws:
java.sql.SQLException- Thrown if a database access error occurs, this method is called on a closed connection or the given parameters are notResultSetconstants indicating type, concurrency, and holdability.- Since:
- 2.4.0
-
prepareStatement
java.sql.PreparedStatement prepareStatement(java.lang.String sql) throws java.sql.SQLExceptionCreates or obtains aPreparedStatementfrom my pool.- Parameters:
sql- the SQL statement.- Returns:
- a
PoolablePreparedStatement - Throws:
java.sql.SQLException
-
prepareStatement
java.sql.PreparedStatement prepareStatement(java.lang.String sql, int autoGeneratedKeys) throws java.sql.SQLExceptionCreates or obtains aPreparedStatementfrom my pool.- Parameters:
sql- an SQL statement that may contain one or more '?' IN parameter placeholders.autoGeneratedKeys- a flag indicating whether auto-generated keys should be returned; one ofStatement.RETURN_GENERATED_KEYSorStatement.NO_GENERATED_KEYS.- Returns:
- a
PoolablePreparedStatement - Throws:
java.sql.SQLException- See Also:
Connection.prepareStatement(String, int)
-
prepareStatement
java.sql.PreparedStatement prepareStatement(java.lang.String sql, int[] columnIndexes) throws java.sql.SQLException- Throws:
java.sql.SQLException
-
prepareStatement
java.sql.PreparedStatement prepareStatement(java.lang.String sql, int resultSetType, int resultSetConcurrency) throws java.sql.SQLExceptionCreates or obtains aPreparedStatementfrom my pool.- Parameters:
sql- aStringobject that is the SQL statement to be sent to the database; may contain one or more '?' IN parameters.resultSetType- a result set type; one ofResultSet.TYPE_FORWARD_ONLY,ResultSet.TYPE_SCROLL_INSENSITIVE, orResultSet.TYPE_SCROLL_SENSITIVE.resultSetConcurrency- a concurrency type; one ofResultSet.CONCUR_READ_ONLYorResultSet.CONCUR_UPDATABLE.- Returns:
- a
PoolablePreparedStatement. - Throws:
java.sql.SQLException- See Also:
Connection.prepareStatement(String, int, int)
-
prepareStatement
java.sql.PreparedStatement prepareStatement(java.lang.String sql, int resultSetType, int resultSetConcurrency, int resultSetHoldability) throws java.sql.SQLException- Throws:
java.sql.SQLException
-
prepareStatement
java.sql.PreparedStatement prepareStatement(java.lang.String sql, java.lang.String[] columnNames) throws java.sql.SQLException- Throws:
java.sql.SQLException
-
removeConnectionEventListener
public void removeConnectionEventListener(javax.sql.ConnectionEventListener listener)
- Specified by:
removeConnectionEventListenerin interfacejavax.sql.PooledConnection
-
removeStatementEventListener
public void removeStatementEventListener(javax.sql.StatementEventListener listener)
- Specified by:
removeStatementEventListenerin interfacejavax.sql.PooledConnection
-
setAccessToUnderlyingConnectionAllowed
public void setAccessToUnderlyingConnectionAllowed(boolean allow)
Sets the value of the accessToUnderlyingConnectionAllowed property. It controls if the PoolGuard allows access to the underlying connection. (Default: false.)- Parameters:
allow- Access to the underlying connection is granted when true.
-
setStatementPool
public void setStatementPool(KeyedObjectPool<PStmtKey,DelegatingPreparedStatement> statementPool)
-
validateObject
public boolean validateObject(PStmtKey key, PooledObject<DelegatingPreparedStatement> pooledObject)
MyKeyedPooledObjectFactorymethod for validatingPreparedStatements.- Specified by:
validateObjectin interfaceKeyedPooledObjectFactory<PStmtKey,DelegatingPreparedStatement>- Parameters:
key- Ignored.pooledObject- Ignored.- Returns:
true
-
toString
public java.lang.String toString()
- Overrides:
toStringin classjava.lang.Object- Since:
- 2.6.0
-
-