Class PooledConnectionImpl
java.lang.Object
org.apache.commons.dbcp2.cpdsadapter.PooledConnectionImpl
- All Implemented Interfaces:
PooledConnection,org.apache.commons.pool2.KeyedPooledObjectFactory<PStmtKey,DelegatingPreparedStatement>
final class PooledConnectionImpl
extends Object
implements PooledConnection, org.apache.commons.pool2.KeyedPooledObjectFactory<PStmtKey,DelegatingPreparedStatement>
Implementation of
PooledConnection that is returned by DriverAdapterCPDS.- Since:
- 2.0
-
Field Summary
FieldsModifier and TypeFieldDescriptionprivate booleanControls access to the underlying connection.private booleanFlag set to true, onceclose()is called.private static final Stringprivate ConnectionThe JDBC database connection that represents the physical db connection.private final DelegatingConnection<?> A DelegatingConnection used to create a PoolablePreparedStatementStub.private final List<ConnectionEventListener> ConnectionEventListeners.private ConnectionThe JDBC database logical connection.private org.apache.commons.pool2.KeyedObjectPool<PStmtKey, DelegatingPreparedStatement> My pool ofPreparedStatements.private final List<StatementEventListener> StatementEventListeners. -
Constructor Summary
Constructors -
Method Summary
Modifier and TypeMethodDescriptionvoidactivateObject(PStmtKey key, org.apache.commons.pool2.PooledObject<DelegatingPreparedStatement> pooledObject) MyKeyedPooledObjectFactorymethod for activatingPreparedStatements.voidvoidprivate voidThrows 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 PStmtKeyCreates aPStmtKeyfor the given arguments.protected PStmtKeyCreates aPStmtKeyfor the given arguments.protected PStmtKeyCreates aPStmtKeyfor the given arguments.protected PStmtKeyCreates aPStmtKeyfor the given arguments.protected PStmtKeyCreates aPStmtKeyfor the given arguments.protected PStmtKeycreateKey(String sql, int resultSetType, int resultSetConcurrency, int resultSetHoldability, PoolingConnection.StatementType statementType) Creates aPStmtKeyfor the given arguments.protected PStmtKeycreateKey(String sql, int resultSetType, int resultSetConcurrency, PoolingConnection.StatementType statementType) Creates aPStmtKeyfor the given arguments.protected PStmtKeyCreates aPStmtKeyfor the given arguments.protected PStmtKeycreateKey(String sql, PoolingConnection.StatementType statementType) Creates aPStmtKeyfor the given arguments.voiddestroyObject(PStmtKey key, org.apache.commons.pool2.PooledObject<DelegatingPreparedStatement> pooledObject) MyKeyedPooledObjectFactorymethod for destroyingPreparedStatements.protected voidfinalize()Closes the physical connection and checks that the logical connection was closed as well.private StringReturns a JDBC connection.private Connectionprivate StringbooleanReturns the value of the accessToUnderlyingConnectionAllowed property.org.apache.commons.pool2.PooledObject<DelegatingPreparedStatement> makeObject(PStmtKey key) MyKeyedPooledObjectFactorymethod for creatingPreparedStatements.(package private) voidSends a connectionClosed event.voidpassivateObject(PStmtKey key, org.apache.commons.pool2.PooledObject<DelegatingPreparedStatement> pooledObject) MyKeyedPooledObjectFactorymethod for passivatingPreparedStatements.(package private) CallableStatementprepareCall(String sql) Creates or obtains aCallableStatementfrom my pool.(package private) CallableStatementprepareCall(String sql, int resultSetType, int resultSetConcurrency) Creates or obtains aCallableStatementfrom my pool.(package private) CallableStatementprepareCall(String sql, int resultSetType, int resultSetConcurrency, int resultSetHoldability) Creates or obtains aCallableStatementfrom my pool.(package private) PreparedStatementprepareStatement(String sql) Creates or obtains aPreparedStatementfrom my pool.(package private) PreparedStatementprepareStatement(String sql, int autoGeneratedKeys) Creates or obtains aPreparedStatementfrom my pool.(package private) PreparedStatementprepareStatement(String sql, int[] columnIndexes) (package private) PreparedStatementprepareStatement(String sql, int resultSetType, int resultSetConcurrency) Creates or obtains aPreparedStatementfrom my pool.(package private) PreparedStatementprepareStatement(String sql, int resultSetType, int resultSetConcurrency, int resultSetHoldability) (package private) PreparedStatementprepareStatement(String sql, String[] columnNames) voidvoidvoidsetAccessToUnderlyingConnectionAllowed(boolean allow) Sets the value of the accessToUnderlyingConnectionAllowed property.voidsetStatementPool(org.apache.commons.pool2.KeyedObjectPool<PStmtKey, DelegatingPreparedStatement> statementPool) toString()booleanvalidateObject(PStmtKey key, org.apache.commons.pool2.PooledObject<DelegatingPreparedStatement> pooledObject) MyKeyedPooledObjectFactorymethod for validatingPreparedStatements.Methods inherited from class java.lang.Object
clone, equals, getClass, hashCode, notify, notifyAll, wait, wait, waitMethods inherited from interface org.apache.commons.pool2.KeyedPooledObjectFactory
destroyObject
-
Field Details
-
CLOSED
- See Also:
-
connection
The JDBC database connection that represents the physical db connection. -
delegatingConnection
A DelegatingConnection used to create a PoolablePreparedStatementStub. -
logicalConnection
The JDBC database logical connection. -
eventListeners
ConnectionEventListeners. -
statementEventListeners
StatementEventListeners. -
closed
private boolean closedFlag set to true, onceclose()is called. -
pStmtPool
My pool ofPreparedStatements. -
accessToUnderlyingConnectionAllowed
private boolean accessToUnderlyingConnectionAllowedControls access to the underlying connection.
-
-
Constructor Details
-
PooledConnectionImpl
PooledConnectionImpl(Connection connection) Wraps a real connection.- Parameters:
connection- the connection to be wrapped.
-
-
Method Details
-
activateObject
public void activateObject(PStmtKey key, org.apache.commons.pool2.PooledObject<DelegatingPreparedStatement> pooledObject) throws SQLException MyKeyedPooledObjectFactorymethod for activatingPreparedStatements.- Specified by:
activateObjectin interfaceorg.apache.commons.pool2.KeyedPooledObjectFactory<PStmtKey,DelegatingPreparedStatement> - Parameters:
pooledObject- Activates the underlying object.- Throws:
SQLException
-
addConnectionEventListener
- Specified by:
addConnectionEventListenerin interfacePooledConnection
-
addStatementEventListener
- Specified by:
addStatementEventListenerin interfacePooledConnection
-
assertOpen
Throws an SQLException, if isClosed is true- Throws:
SQLException
-
close
Closes the physical connection and marks thisPooledConnectionso that it may not be used to generate any more logicalConnections.- Specified by:
closein interfacePooledConnection- Throws:
SQLException- Thrown when an error occurs or the connection is already closed.
-
createKey
Creates aPStmtKeyfor the given arguments.- Parameters:
sql- The SQL statement.- Returns:
- a
PStmtKeyfor the given arguments.
-
createKey
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
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(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(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(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
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
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
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, org.apache.commons.pool2.PooledObject<DelegatingPreparedStatement> pooledObject) throws SQLException MyKeyedPooledObjectFactorymethod for destroyingPreparedStatements.- Specified by:
destroyObjectin interfaceorg.apache.commons.pool2.KeyedPooledObjectFactory<PStmtKey,DelegatingPreparedStatement> - Parameters:
key- ignoredpooledObject- the wrappedPreparedStatementto be destroyed.- Throws:
SQLException
-
finalize
Closes the physical connection and checks that the logical connection was closed as well. -
getCatalogOrNull
-
getConnection
Returns a JDBC connection.- Specified by:
getConnectionin interfacePooledConnection- Returns:
- The database connection.
- Throws:
SQLException- if the connection is not open or the previous logical connection is still open
-
getRawConnection
- Throws:
SQLException
-
getSchemaOrNull
-
isAccessToUnderlyingConnectionAllowed
public boolean isAccessToUnderlyingConnectionAllowed()Returns the value of the accessToUnderlyingConnectionAllowed property.- Returns:
- true if access to the underlying is allowed, false otherwise.
-
makeObject
public org.apache.commons.pool2.PooledObject<DelegatingPreparedStatement> makeObject(PStmtKey key) throws SQLException MyKeyedPooledObjectFactorymethod for creatingPreparedStatements.- Specified by:
makeObjectin interfaceorg.apache.commons.pool2.KeyedPooledObjectFactory<PStmtKey,DelegatingPreparedStatement> - Parameters:
key- The key for thePreparedStatementto be created.- Throws:
SQLException
-
notifyListeners
void notifyListeners()Sends a connectionClosed event. -
passivateObject
public void passivateObject(PStmtKey key, org.apache.commons.pool2.PooledObject<DelegatingPreparedStatement> pooledObject) throws SQLException MyKeyedPooledObjectFactorymethod for passivatingPreparedStatements. Currently, invokesPreparedStatement.clearParameters().- Specified by:
passivateObjectin interfaceorg.apache.commons.pool2.KeyedPooledObjectFactory<PStmtKey,DelegatingPreparedStatement> - Parameters:
key- ignoredpooledObject- a wrappedPreparedStatement- Throws:
SQLException
-
prepareCall
Creates 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:
SQLException- Thrown if a database access error occurs or this method is called on a closed connection.- Since:
- 2.4.0
-
prepareCall
CallableStatement prepareCall(String sql, int resultSetType, int resultSetConcurrency) throws SQLException Creates 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:
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
CallableStatement prepareCall(String sql, int resultSetType, int resultSetConcurrency, int resultSetHoldability) throws SQLException Creates 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:
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
Creates or obtains aPreparedStatementfrom my pool.- Parameters:
sql- the SQL statement.- Returns:
- a
PoolablePreparedStatement - Throws:
SQLException- Thrown if a database access error occurs, this method is called on a closed connection, or the borrow failed.
-
prepareStatement
Creates 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:
SQLException- Thrown if a database access error occurs, this method is called on a closed connection, or the borrow failed.- See Also:
-
prepareStatement
PreparedStatement prepareStatement(String sql, int resultSetType, int resultSetConcurrency) throws SQLException Creates 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:
SQLException- Thrown if a database access error occurs, this method is called on a closed connection, or the borrow failed.- See Also:
-
prepareStatement
PreparedStatement prepareStatement(String sql, int resultSetType, int resultSetConcurrency, int resultSetHoldability) throws SQLException - Throws:
SQLException
-
prepareStatement
- Throws:
SQLException
-
prepareStatement
- Throws:
SQLException
-
removeConnectionEventListener
- Specified by:
removeConnectionEventListenerin interfacePooledConnection
-
removeStatementEventListener
- Specified by:
removeStatementEventListenerin interfacePooledConnection
-
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(org.apache.commons.pool2.KeyedObjectPool<PStmtKey, DelegatingPreparedStatement> statementPool) -
toString
-
validateObject
public boolean validateObject(PStmtKey key, org.apache.commons.pool2.PooledObject<DelegatingPreparedStatement> pooledObject) MyKeyedPooledObjectFactorymethod for validatingPreparedStatements.- Specified by:
validateObjectin interfaceorg.apache.commons.pool2.KeyedPooledObjectFactory<PStmtKey,DelegatingPreparedStatement> - Parameters:
key- Ignored.pooledObject- Ignored.- Returns:
true
-