Class PooledConnectionImpl
java.lang.Object
org.datanucleus.store.rdbms.datasource.dbcp2.cpdsadapter.PooledConnectionImpl
- All Implemented Interfaces:
PooledConnection
,KeyedPooledObjectFactory<PStmtKey,
DelegatingPreparedStatement>
class PooledConnectionImpl
extends Object
implements PooledConnection, KeyedPooledObjectFactory<PStmtKey,DelegatingPreparedStatement>
Implementation of PooledConnection that is returned by PooledConnectionDataSource.
- Since:
- 2.0
-
Field Summary
FieldsModifier and TypeFieldDescriptionprivate boolean
Controls access to the underlying connection.private boolean
Flag set to true, onceclose()
is called.private static final String
private Connection
The JDBC database connection that represents the physical db connection.private final DelegatingConnection
<?> A DelegatingConnection used to create a PoolablePreparedStatementStub.private final Vector
<ConnectionEventListener> ConnectionEventListeners.private Connection
The JDBC database logical connection.My pool ofPreparedStatement
s.private final Vector
<StatementEventListener> StatementEventListeners. -
Constructor Summary
Constructors -
Method Summary
Modifier and TypeMethodDescriptionvoid
activateObject
(PStmtKey key, PooledObject<DelegatingPreparedStatement> pooledObject) MyKeyedPooledObjectFactory
method for activatingPreparedStatement
s.void
void
private void
Throws an SQLException, if isClosed is truevoid
close()
Closes the physical connection and marks thisPooledConnection
so that it may not be used to generate any more logicalConnection
s.protected PStmtKey
Creates aPStmtKey
for the given arguments.protected PStmtKey
Creates aPStmtKey
for the given arguments.protected PStmtKey
Creates aPStmtKey
for the given arguments.protected PStmtKey
Creates aPStmtKey
for the given arguments.protected PStmtKey
Creates aPStmtKey
for the given arguments.protected PStmtKey
createKey
(String sql, int resultSetType, int resultSetConcurrency, int resultSetHoldability, PoolingConnection.StatementType statementType) Creates aPStmtKey
for the given arguments.protected PStmtKey
createKey
(String sql, int resultSetType, int resultSetConcurrency, PoolingConnection.StatementType statementType) Creates aPStmtKey
for the given arguments.protected PStmtKey
Creates aPStmtKey
for the given arguments.protected PStmtKey
createKey
(String sql, PoolingConnection.StatementType statementType) Creates aPStmtKey
for the given arguments.void
destroyObject
(PStmtKey key, PooledObject<DelegatingPreparedStatement> pooledObject) MyKeyedPooledObjectFactory
method for destroyingPreparedStatement
s.protected void
finalize()
Closes the physical connection and checks that the logical connection was closed as well.private String
Returns a JDBC connection.private String
boolean
Returns the value of the accessToUnderlyingConnectionAllowed property.makeObject
(PStmtKey key) MyKeyedPooledObjectFactory
method for creatingPreparedStatement
s.protected String
normalizeSQL
(String sql) Normalizes the given SQL statement, producing a canonical form that is semantically equivalent to the original.(package private) void
Sends a connectionClosed event.void
passivateObject
(PStmtKey key, PooledObject<DelegatingPreparedStatement> pooledObject) MyKeyedPooledObjectFactory
method for passivatingPreparedStatement
s.(package private) CallableStatement
prepareCall
(String sql) Creates or obtains aCallableStatement
from my pool.(package private) CallableStatement
prepareCall
(String sql, int resultSetType, int resultSetConcurrency) Creates or obtains aCallableStatement
from my pool.(package private) CallableStatement
prepareCall
(String sql, int resultSetType, int resultSetConcurrency, int resultSetHoldability) Creates or obtains aCallableStatement
from my pool.(package private) PreparedStatement
prepareStatement
(String sql) Creates or obtains aPreparedStatement
from my pool.(package private) PreparedStatement
prepareStatement
(String sql, int autoGeneratedKeys) Creates or obtains aPreparedStatement
from my pool.(package private) PreparedStatement
prepareStatement
(String sql, int[] columnIndexes) (package private) PreparedStatement
prepareStatement
(String sql, int resultSetType, int resultSetConcurrency) Creates or obtains aPreparedStatement
from my pool.(package private) PreparedStatement
prepareStatement
(String sql, int resultSetType, int resultSetConcurrency, int resultSetHoldability) (package private) PreparedStatement
prepareStatement
(String sql, String[] columnNames) void
void
void
setAccessToUnderlyingConnectionAllowed
(boolean allow) Sets the value of the accessToUnderlyingConnectionAllowed property.void
setStatementPool
(KeyedObjectPool<PStmtKey, DelegatingPreparedStatement> statementPool) toString()
boolean
validateObject
(PStmtKey key, PooledObject<DelegatingPreparedStatement> pooledObject) MyKeyedPooledObjectFactory
method for validatingPreparedStatement
s.
-
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 ofPreparedStatement
s. -
accessToUnderlyingConnectionAllowed
private boolean accessToUnderlyingConnectionAllowedControls access to the underlying connection.
-
-
Constructor Details
-
PooledConnectionImpl
PooledConnectionImpl(Connection connection) Wraps the real connection.- Parameters:
connection
- the connection to be wrapped.
-
-
Method Details
-
activateObject
public void activateObject(PStmtKey key, PooledObject<DelegatingPreparedStatement> pooledObject) throws Exception MyKeyedPooledObjectFactory
method for activatingPreparedStatement
s.- Specified by:
activateObject
in interfaceKeyedPooledObjectFactory<PStmtKey,
DelegatingPreparedStatement> - Parameters:
key
- Ignored.pooledObject
- Ignored.- Throws:
Exception
- if there is a problem activatingobj
, this exception may be swallowed by the pool.- See Also:
-
addConnectionEventListener
- Specified by:
addConnectionEventListener
in interfacePooledConnection
-
addStatementEventListener
- Specified by:
addStatementEventListener
in interfacePooledConnection
-
assertOpen
Throws an SQLException, if isClosed is true- Throws:
SQLException
-
close
Closes the physical connection and marks thisPooledConnection
so that it may not be used to generate any more logicalConnection
s.- Specified by:
close
in interfacePooledConnection
- Throws:
SQLException
- Thrown when an error occurs or the connection is already closed.
-
createKey
Creates aPStmtKey
for the given arguments.- Parameters:
sql
- The SQL statement.- Returns:
- a
PStmtKey
for the given arguments.
-
createKey
Creates aPStmtKey
for the given arguments.- Parameters:
sql
- The SQL statement.autoGeneratedKeys
- A flag indicating whether auto-generated keys should be returned; one ofStatement.RETURN_GENERATED_KEYS
orStatement.NO_GENERATED_KEYS
.- Returns:
- a key to uniquely identify a prepared statement.
-
createKey
Creates aPStmtKey
for 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 aPStmtKey
for 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_ONLY
orResultSet.CONCUR_UPDATABLE
.- Returns:
- a key to uniquely identify a prepared statement.
-
createKey
protected PStmtKey createKey(String sql, int resultSetType, int resultSetConcurrency, int resultSetHoldability) Creates aPStmtKey
for 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_ONLY
orResultSet.CONCUR_UPDATABLE
resultSetHoldability
- One of the followingResultSet
constants:ResultSet.HOLD_CURSORS_OVER_COMMIT
orResultSet.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 aPStmtKey
for 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_ONLY
orResultSet.CONCUR_UPDATABLE
.resultSetHoldability
- One of the followingResultSet
constants:ResultSet.HOLD_CURSORS_OVER_COMMIT
orResultSet.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 aPStmtKey
for 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_ONLY
orResultSet.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 aPStmtKey
for 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 aPStmtKey
for 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 Exception MyKeyedPooledObjectFactory
method for destroyingPreparedStatement
s.- Specified by:
destroyObject
in interfaceKeyedPooledObjectFactory<PStmtKey,
DelegatingPreparedStatement> - Parameters:
key
- ignoredpooledObject
- the wrappedPreparedStatement
to be destroyed.- Throws:
Exception
- should be avoided as it may be swallowed by the pool implementation.- See Also:
-
finalize
Closes the physical connection and checks that the logical connection was closed as well. -
getCatalogOrNull
-
getSchemaOrNull
-
getConnection
Returns a JDBC connection.- Specified by:
getConnection
in interfacePooledConnection
- Returns:
- The database connection.
- Throws:
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
MyKeyedPooledObjectFactory
method for creatingPreparedStatement
s.- Specified by:
makeObject
in interfaceKeyedPooledObjectFactory<PStmtKey,
DelegatingPreparedStatement> - Parameters:
key
- The key for thePreparedStatement
to be created.- Returns:
- a
PooledObject
wrapping an instance that can be served by the pool. - Throws:
Exception
- if there is a problem creating a new instance, this will be propagated to the code requesting an object.
-
normalizeSQL
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 Exception MyKeyedPooledObjectFactory
method for passivatingPreparedStatement
s. Currently invokesPreparedStatement.clearParameters()
.- Specified by:
passivateObject
in interfaceKeyedPooledObjectFactory<PStmtKey,
DelegatingPreparedStatement> - Parameters:
key
- ignoredpooledObject
- a wrappedPreparedStatement
- Throws:
Exception
- if there is a problem passivatingobj
, this exception may be swallowed by the pool.- See Also:
-
prepareCall
Creates or obtains aCallableStatement
from 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
CallableStatement
object 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 aCallableStatement
from my pool.- Parameters:
sql
- aString
object 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_ONLY
orResultSet.CONCUR_UPDATABLE
.- Returns:
- a
CallableStatement
object containing the pre-compiled SQL statement that will produceResultSet
objects 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 notResultSet
constants indicating type and concurrency.- Since:
- 2.4.0
-
prepareCall
CallableStatement prepareCall(String sql, int resultSetType, int resultSetConcurrency, int resultSetHoldability) throws SQLException Creates or obtains aCallableStatement
from my pool.- Parameters:
sql
- aString
object that is the SQL statement to be sent to the database; may contain on or more '?' parameters.resultSetType
- one of the followingResultSet
constants:ResultSet.TYPE_FORWARD_ONLY
,ResultSet.TYPE_SCROLL_INSENSITIVE
, orResultSet.TYPE_SCROLL_SENSITIVE
.resultSetConcurrency
- one of the followingResultSet
constants:ResultSet.CONCUR_READ_ONLY
orResultSet.CONCUR_UPDATABLE
.resultSetHoldability
- one of the followingResultSet
constants:ResultSet.HOLD_CURSORS_OVER_COMMIT
orResultSet.CLOSE_CURSORS_AT_COMMIT
.- Returns:
- a new
CallableStatement
object, containing the pre-compiled SQL statement, that will generateResultSet
objects 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 notResultSet
constants indicating type, concurrency, and holdability.- Since:
- 2.4.0
-
prepareStatement
Creates or obtains aPreparedStatement
from my pool.- Parameters:
sql
- the SQL statement.- Returns:
- a
PoolablePreparedStatement
- Throws:
SQLException
-
prepareStatement
Creates or obtains aPreparedStatement
from 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_KEYS
orStatement.NO_GENERATED_KEYS
.- Returns:
- a
PoolablePreparedStatement
- Throws:
SQLException
- See Also:
-
prepareStatement
- Throws:
SQLException
-
prepareStatement
PreparedStatement prepareStatement(String sql, int resultSetType, int resultSetConcurrency) throws SQLException Creates or obtains aPreparedStatement
from my pool.- Parameters:
sql
- aString
object 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_ONLY
orResultSet.CONCUR_UPDATABLE
.- Returns:
- a
PoolablePreparedStatement
. - Throws:
SQLException
- See Also:
-
prepareStatement
PreparedStatement prepareStatement(String sql, int resultSetType, int resultSetConcurrency, int resultSetHoldability) throws SQLException - Throws:
SQLException
-
prepareStatement
- Throws:
SQLException
-
removeConnectionEventListener
- Specified by:
removeConnectionEventListener
in interfacePooledConnection
-
removeStatementEventListener
- Specified by:
removeStatementEventListener
in 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
-
validateObject
MyKeyedPooledObjectFactory
method for validatingPreparedStatement
s.- Specified by:
validateObject
in interfaceKeyedPooledObjectFactory<PStmtKey,
DelegatingPreparedStatement> - Parameters:
key
- Ignored.pooledObject
- Ignored.- Returns:
true
-
toString
-