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 boolean
accessToUnderlyingConnectionAllowed
Controls access to the underlying connection.private boolean
closed
Flag set to true, onceclose()
is called.private static java.lang.String
CLOSED
private java.sql.Connection
connection
The JDBC database connection that represents the physical db connection.private DelegatingConnection<?>
delegatingConnection
A DelegatingConnection used to create a PoolablePreparedStatementStub.private java.util.Vector<javax.sql.ConnectionEventListener>
eventListeners
ConnectionEventListeners.private java.sql.Connection
logicalConnection
The JDBC database logical connection.private KeyedObjectPool<PStmtKey,DelegatingPreparedStatement>
pStmtPool
My pool ofPreparedStatement
s.private java.util.Vector<javax.sql.StatementEventListener>
statementEventListeners
StatementEventListeners.
-
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 void
activateObject(PStmtKey key, PooledObject<DelegatingPreparedStatement> pooledObject)
MyKeyedPooledObjectFactory
method for activatingPreparedStatement
s.void
addConnectionEventListener(javax.sql.ConnectionEventListener listener)
void
addStatementEventListener(javax.sql.StatementEventListener listener)
private void
assertOpen()
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
createKey(java.lang.String sql)
Creates aPStmtKey
for the given arguments.protected PStmtKey
createKey(java.lang.String sql, int autoGeneratedKeys)
Creates aPStmtKey
for the given arguments.protected PStmtKey
createKey(java.lang.String sql, int[] columnIndexes)
Creates aPStmtKey
for the given arguments.protected PStmtKey
createKey(java.lang.String sql, int resultSetType, int resultSetConcurrency)
Creates aPStmtKey
for the given arguments.protected PStmtKey
createKey(java.lang.String sql, int resultSetType, int resultSetConcurrency, int resultSetHoldability)
Creates aPStmtKey
for the given arguments.protected PStmtKey
createKey(java.lang.String sql, int resultSetType, int resultSetConcurrency, int resultSetHoldability, PoolingConnection.StatementType statementType)
Creates aPStmtKey
for the given arguments.protected PStmtKey
createKey(java.lang.String sql, int resultSetType, int resultSetConcurrency, PoolingConnection.StatementType statementType)
Creates aPStmtKey
for the given arguments.protected PStmtKey
createKey(java.lang.String sql, java.lang.String[] columnNames)
Creates aPStmtKey
for the given arguments.protected PStmtKey
createKey(java.lang.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 java.lang.String
getCatalogOrNull()
java.sql.Connection
getConnection()
Returns a JDBC connection.private java.lang.String
getSchemaOrNull()
boolean
isAccessToUnderlyingConnectionAllowed()
Returns the value of the accessToUnderlyingConnectionAllowed property.PooledObject<DelegatingPreparedStatement>
makeObject(PStmtKey key)
MyKeyedPooledObjectFactory
method for creatingPreparedStatement
s.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.(package private) void
notifyListeners()
Sends a connectionClosed event.void
passivateObject(PStmtKey key, PooledObject<DelegatingPreparedStatement> pooledObject)
MyKeyedPooledObjectFactory
method for passivatingPreparedStatement
s.(package private) java.sql.CallableStatement
prepareCall(java.lang.String sql)
Creates or obtains aCallableStatement
from my pool.(package private) java.sql.CallableStatement
prepareCall(java.lang.String sql, int resultSetType, int resultSetConcurrency)
Creates or obtains aCallableStatement
from my pool.(package private) java.sql.CallableStatement
prepareCall(java.lang.String sql, int resultSetType, int resultSetConcurrency, int resultSetHoldability)
Creates or obtains aCallableStatement
from my pool.(package private) java.sql.PreparedStatement
prepareStatement(java.lang.String sql)
Creates or obtains aPreparedStatement
from my pool.(package private) java.sql.PreparedStatement
prepareStatement(java.lang.String sql, int autoGeneratedKeys)
Creates or obtains aPreparedStatement
from my pool.(package private) java.sql.PreparedStatement
prepareStatement(java.lang.String sql, int[] columnIndexes)
(package private) java.sql.PreparedStatement
prepareStatement(java.lang.String sql, int resultSetType, int resultSetConcurrency)
Creates or obtains aPreparedStatement
from my pool.(package private) java.sql.PreparedStatement
prepareStatement(java.lang.String sql, int resultSetType, int resultSetConcurrency, int resultSetHoldability)
(package private) java.sql.PreparedStatement
prepareStatement(java.lang.String sql, java.lang.String[] columnNames)
void
removeConnectionEventListener(javax.sql.ConnectionEventListener listener)
void
removeStatementEventListener(javax.sql.StatementEventListener listener)
void
setAccessToUnderlyingConnectionAllowed(boolean allow)
Sets the value of the accessToUnderlyingConnectionAllowed property.void
setStatementPool(KeyedObjectPool<PStmtKey,DelegatingPreparedStatement> statementPool)
java.lang.String
toString()
boolean
validateObject(PStmtKey key, PooledObject<DelegatingPreparedStatement> pooledObject)
MyKeyedPooledObjectFactory
method for validatingPreparedStatement
s.
-
-
-
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 ofPreparedStatement
s.
-
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
MyKeyedPooledObjectFactory
method for activatingPreparedStatement
s.- Specified by:
activateObject
in 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:
addConnectionEventListener
in interfacejavax.sql.PooledConnection
-
addStatementEventListener
public void addStatementEventListener(javax.sql.StatementEventListener listener)
- Specified by:
addStatementEventListener
in interfacejavax.sql.PooledConnection
-
assertOpen
private void assertOpen() throws java.sql.SQLException
Throws an SQLException, if isClosed is true- Throws:
java.sql.SQLException
-
close
public void close() throws java.sql.SQLException
Closes the physical connection and marks thisPooledConnection
so that it may not be used to generate any more logicalConnection
s.- Specified by:
close
in 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 aPStmtKey
for the given arguments.- Parameters:
sql
- The SQL statement.- Returns:
- a
PStmtKey
for the given arguments.
-
createKey
protected PStmtKey createKey(java.lang.String sql, int autoGeneratedKeys)
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
protected PStmtKey createKey(java.lang.String sql, int[] columnIndexes)
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
protected PStmtKey createKey(java.lang.String sql, int resultSetType, int resultSetConcurrency)
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(java.lang.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(java.lang.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(java.lang.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
protected PStmtKey createKey(java.lang.String sql, PoolingConnection.StatementType statementType)
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
protected PStmtKey createKey(java.lang.String sql, java.lang.String[] columnNames)
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 java.lang.Exception
MyKeyedPooledObjectFactory
method for destroyingPreparedStatement
s.- Specified by:
destroyObject
in interfaceKeyedPooledObjectFactory<PStmtKey,DelegatingPreparedStatement>
- Parameters:
key
- ignoredpooledObject
- the wrappedPreparedStatement
to 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.Throwable
Closes the physical connection and checks that the logical connection was closed as well.- Overrides:
finalize
in 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.SQLException
Returns a JDBC connection.- Specified by:
getConnection
in 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
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:
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
MyKeyedPooledObjectFactory
method for passivatingPreparedStatement
s. Currently invokesPreparedStatement.clearParameters()
.- Specified by:
passivateObject
in 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.SQLException
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:
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.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:
java.sql.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
java.sql.CallableStatement prepareCall(java.lang.String sql, int resultSetType, int resultSetConcurrency, int resultSetHoldability) throws java.sql.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:
java.sql.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
java.sql.PreparedStatement prepareStatement(java.lang.String sql) throws java.sql.SQLException
Creates or obtains aPreparedStatement
from 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.SQLException
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:
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.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:
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:
removeConnectionEventListener
in interfacejavax.sql.PooledConnection
-
removeStatementEventListener
public void removeStatementEventListener(javax.sql.StatementEventListener listener)
- Specified by:
removeStatementEventListener
in 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)
MyKeyedPooledObjectFactory
method for validatingPreparedStatement
s.- Specified by:
validateObject
in interfaceKeyedPooledObjectFactory<PStmtKey,DelegatingPreparedStatement>
- Parameters:
key
- Ignored.pooledObject
- Ignored.- Returns:
true
-
toString
public java.lang.String toString()
- Overrides:
toString
in classjava.lang.Object
- Since:
- 2.6.0
-
-