Package org.apache.tomcat.dbcp.dbcp2
Class PoolingConnection
- java.lang.Object
-
- org.apache.tomcat.dbcp.dbcp2.AbandonedTrace
-
- org.apache.tomcat.dbcp.dbcp2.DelegatingConnection<java.sql.Connection>
-
- org.apache.tomcat.dbcp.dbcp2.PoolingConnection
-
- All Implemented Interfaces:
java.lang.AutoCloseable
,java.sql.Connection
,java.sql.Wrapper
,KeyedPooledObjectFactory<PStmtKey,DelegatingPreparedStatement>
,TrackedUse
public class PoolingConnection extends DelegatingConnection<java.sql.Connection> implements KeyedPooledObjectFactory<PStmtKey,DelegatingPreparedStatement>
ADelegatingConnection
that poolsPreparedStatement
s.The
prepareStatement(org.apache.tomcat.dbcp.dbcp2.PStmtKey)
andprepareCall(org.apache.tomcat.dbcp.dbcp2.PStmtKey)
methods, rather than creating a new PreparedStatement each time, may actually pull the statement from a pool of unused statements. TheStatement.close()
method of the returned statement doesn't actually close the statement, but rather returns it to the pool. (SeePoolablePreparedStatement
,PoolableCallableStatement
.)- Since:
- 2.0
- See Also:
PoolablePreparedStatement
-
-
Nested Class Summary
Nested Classes Modifier and Type Class Description static class
PoolingConnection.StatementType
Statement types.
-
Constructor Summary
Constructors Constructor Description PoolingConnection(java.sql.Connection connection)
Constructor.
-
Method Summary
All Methods Instance Methods Concrete Methods Modifier and Type Method Description void
activateObject(PStmtKey key, PooledObject<DelegatingPreparedStatement> pooledObject)
KeyedPooledObjectFactory
method for activating pooled statements.void
close()
Closes and frees allPreparedStatement
s orCallableStatement
s from the pool, and close the underlying connection.void
connectionReturnedToPool()
Notification fromPoolableConnection
that we returned to the pool.protected PStmtKey
createKey(java.lang.String sql)
Creates a PStmtKey for the given arguments.protected PStmtKey
createKey(java.lang.String sql, int autoGeneratedKeys)
Creates a PStmtKey for the given arguments.protected PStmtKey
createKey(java.lang.String sql, int[] columnIndexes)
Creates a PStmtKey for the given arguments.protected PStmtKey
createKey(java.lang.String sql, int resultSetType, int resultSetConcurrency)
Creates a PStmtKey for the given arguments.protected PStmtKey
createKey(java.lang.String sql, int resultSetType, int resultSetConcurrency, int resultSetHoldability)
Creates a PStmtKey for the given arguments.protected PStmtKey
createKey(java.lang.String sql, int resultSetType, int resultSetConcurrency, int resultSetHoldability, PoolingConnection.StatementType statementType)
Creates a PStmtKey for the given arguments.protected PStmtKey
createKey(java.lang.String sql, int resultSetType, int resultSetConcurrency, PoolingConnection.StatementType statementType)
Creates a PStmtKey for the given arguments.protected PStmtKey
createKey(java.lang.String sql, java.lang.String[] columnNames)
Creates a PStmtKey for the given arguments.protected PStmtKey
createKey(java.lang.String sql, PoolingConnection.StatementType statementType)
Creates a PStmtKey for the given arguments.void
destroyObject(PStmtKey key, PooledObject<DelegatingPreparedStatement> pooledObject)
KeyedPooledObjectFactory
method for destroying PoolablePreparedStatements and PoolableCallableStatements.KeyedObjectPool<PStmtKey,DelegatingPreparedStatement>
getStatementPool()
Returns the prepared statement pool we're using.PooledObject<DelegatingPreparedStatement>
makeObject(PStmtKey key)
KeyedPooledObjectFactory
method for creatingPoolablePreparedStatement
s orPoolableCallableStatement
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.void
passivateObject(PStmtKey key, PooledObject<DelegatingPreparedStatement> pooledObject)
java.sql.CallableStatement
prepareCall(java.lang.String sql)
Creates or obtains aCallableStatement
from the pool.java.sql.CallableStatement
prepareCall(java.lang.String sql, int resultSetType, int resultSetConcurrency)
Creates or obtains aCallableStatement
from the pool.java.sql.CallableStatement
prepareCall(java.lang.String sql, int resultSetType, int resultSetConcurrency, int resultSetHoldability)
Creates or obtains aCallableStatement
from the pool.java.sql.PreparedStatement
prepareStatement(java.lang.String sql)
Creates or obtains aPreparedStatement
from the pool.java.sql.PreparedStatement
prepareStatement(java.lang.String sql, int autoGeneratedKeys)
java.sql.PreparedStatement
prepareStatement(java.lang.String sql, int[] columnIndexes)
Creates or obtains aPreparedStatement
from the pool.java.sql.PreparedStatement
prepareStatement(java.lang.String sql, int resultSetType, int resultSetConcurrency)
Creates or obtains aPreparedStatement
from the pool.java.sql.PreparedStatement
prepareStatement(java.lang.String sql, int resultSetType, int resultSetConcurrency, int resultSetHoldability)
Creates or obtains aPreparedStatement
from the pool.java.sql.PreparedStatement
prepareStatement(java.lang.String sql, java.lang.String[] columnNames)
Creates or obtains aPreparedStatement
from the pool.void
setClearStatementPoolOnReturn(boolean clearStatementPoolOnReturn)
Sets whether the pool of statements should be cleared when the connection is returned to its pool.void
setStatementPool(KeyedObjectPool<PStmtKey,DelegatingPreparedStatement> pool)
Sets the prepared statement pool.java.lang.String
toString()
Returns a string representation of the metadata associated with the innermost delegate connection.boolean
validateObject(PStmtKey key, PooledObject<DelegatingPreparedStatement> pooledObject)
KeyedPooledObjectFactory
method for validating pooled statements.-
Methods inherited from class org.apache.tomcat.dbcp.dbcp2.DelegatingConnection
abort, activate, checkOpen, clearCachedState, clearWarnings, closeInternal, commit, createArrayOf, createBlob, createClob, createNClob, createSQLXML, createStatement, createStatement, createStatement, createStruct, getAutoCommit, getCacheState, getCatalog, getClientInfo, getClientInfo, getDefaultQueryTimeout, getDefaultQueryTimeoutDuration, getDelegate, getDelegateInternal, getHoldability, getInnermostDelegate, getInnermostDelegateInternal, getMetaData, getNetworkTimeout, getSchema, getTransactionIsolation, getTypeMap, getWarnings, handleException, handleExceptionNoThrow, innermostDelegateEquals, isClosed, isClosedInternal, isReadOnly, isValid, isValid, isWrapperFor, nativeSQL, passivate, releaseSavepoint, rollback, rollback, setAutoCommit, setCacheState, setCatalog, setClientInfo, setClientInfo, setClosedInternal, setDefaultQueryTimeout, setDefaultQueryTimeout, setDelegate, setHoldability, setNetworkTimeout, setReadOnly, setSavepoint, setSavepoint, setSchema, setTransactionIsolation, setTypeMap, unwrap
-
Methods inherited from class org.apache.tomcat.dbcp.dbcp2.AbandonedTrace
addTrace, clearTrace, getLastUsed, getLastUsedInstant, getTrace, removeThisTrace, removeTrace, setLastUsed, setLastUsed, setLastUsed
-
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, wait, wait, wait
-
Methods inherited from interface java.sql.Connection
beginRequest, endRequest, setShardingKey, setShardingKey, setShardingKeyIfValid, setShardingKeyIfValid
-
Methods inherited from interface org.apache.tomcat.dbcp.pool2.KeyedPooledObjectFactory
destroyObject
-
-
-
-
Method Detail
-
activateObject
public void activateObject(PStmtKey key, PooledObject<DelegatingPreparedStatement> pooledObject) throws java.lang.Exception
KeyedPooledObjectFactory
method for activating pooled statements.- Specified by:
activateObject
in interfaceKeyedPooledObjectFactory<PStmtKey,DelegatingPreparedStatement>
- Parameters:
key
- ignoredpooledObject
- wrapped pooled statement to be activated- Throws:
java.lang.Exception
- if there is a problem activatingobj
, this exception may be swallowed by the pool.- See Also:
KeyedPooledObjectFactory.destroyObject(K, org.apache.tomcat.dbcp.pool2.PooledObject<V>)
-
close
public void close() throws java.sql.SQLException
Closes and frees allPreparedStatement
s orCallableStatement
s from the pool, and close the underlying connection.- Specified by:
close
in interfacejava.lang.AutoCloseable
- Specified by:
close
in interfacejava.sql.Connection
- Overrides:
close
in classDelegatingConnection<java.sql.Connection>
- Throws:
java.sql.SQLException
-
connectionReturnedToPool
public void connectionReturnedToPool() throws java.sql.SQLException
Notification fromPoolableConnection
that we returned to the pool.- Throws:
java.sql.SQLException
- whenclearStatementPoolOnReturn
is true and the statement pool could not be cleared- Since:
- 2.8.0
-
createKey
protected PStmtKey createKey(java.lang.String sql)
Creates a PStmtKey for the given arguments.- Parameters:
sql
- the SQL string used to define the statement- Returns:
- the PStmtKey created for the given arguments.
-
createKey
protected PStmtKey createKey(java.lang.String sql, int autoGeneratedKeys)
Creates a PStmtKey for the given arguments.- Parameters:
sql
- the SQL string used to define the statementautoGeneratedKeys
- A flag indicating whether auto-generated keys should be returned; one ofStatement.RETURN_GENERATED_KEYS
orStatement.NO_GENERATED_KEYS
.- Returns:
- the PStmtKey created for the given arguments.
-
createKey
protected PStmtKey createKey(java.lang.String sql, int resultSetType, int resultSetConcurrency)
Creates a PStmtKey for the given arguments.- Parameters:
sql
- the SQL string used to define the statementresultSetType
- result set typeresultSetConcurrency
- result set concurrency- Returns:
- the PStmtKey created for the given arguments.
-
createKey
protected PStmtKey createKey(java.lang.String sql, int resultSetType, int resultSetConcurrency, int resultSetHoldability)
Creates a PStmtKey for the given arguments.- Parameters:
sql
- the SQL string used to define the statementresultSetType
- result set typeresultSetConcurrency
- result set concurrencyresultSetHoldability
- result set holdability- Returns:
- the PStmtKey created for the given arguments.
-
createKey
protected PStmtKey createKey(java.lang.String sql, int resultSetType, int resultSetConcurrency, int resultSetHoldability, PoolingConnection.StatementType statementType)
Creates a PStmtKey for the given arguments.- Parameters:
sql
- the SQL string used to define the statementresultSetType
- result set typeresultSetConcurrency
- result set concurrencyresultSetHoldability
- result set holdabilitystatementType
- statement type- Returns:
- the PStmtKey created for the given arguments.
-
createKey
protected PStmtKey createKey(java.lang.String sql, int resultSetType, int resultSetConcurrency, PoolingConnection.StatementType statementType)
Creates a PStmtKey for the given arguments.- Parameters:
sql
- the SQL string used to define the statementresultSetType
- result set typeresultSetConcurrency
- result set concurrencystatementType
- statement type- Returns:
- the PStmtKey created for the given arguments.
-
createKey
protected PStmtKey createKey(java.lang.String sql, int[] columnIndexes)
Creates a PStmtKey for the given arguments.- Parameters:
sql
- the SQL string used to define the statementcolumnIndexes
- An array of column indexes indicating the columns that should be returned from the inserted row or rows.- Returns:
- the PStmtKey created for the given arguments.
-
createKey
protected PStmtKey createKey(java.lang.String sql, PoolingConnection.StatementType statementType)
Creates a PStmtKey for the given arguments.- Parameters:
sql
- the SQL string used to define the statementstatementType
- statement type- Returns:
- the PStmtKey created for the given arguments.
-
createKey
protected PStmtKey createKey(java.lang.String sql, java.lang.String[] columnNames)
Creates a PStmtKey for the given arguments.- Parameters:
sql
- the SQL string used to define the statementcolumnNames
- column names- Returns:
- the PStmtKey created for the given arguments.
-
destroyObject
public void destroyObject(PStmtKey key, PooledObject<DelegatingPreparedStatement> pooledObject) throws java.lang.Exception
KeyedPooledObjectFactory
method for destroying PoolablePreparedStatements and PoolableCallableStatements. Closes the underlying statement.- Specified by:
destroyObject
in interfaceKeyedPooledObjectFactory<PStmtKey,DelegatingPreparedStatement>
- Parameters:
key
- ignoredpooledObject
- the wrapped pooled statement 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.apache.tomcat.dbcp.pool2.PooledObject<V>)
,KeyedObjectPool.invalidateObject(K, V)
-
getStatementPool
public KeyedObjectPool<PStmtKey,DelegatingPreparedStatement> getStatementPool()
Returns the prepared statement pool we're using.- Returns:
- statement pool
- Since:
- 2.8.0
-
makeObject
public PooledObject<DelegatingPreparedStatement> makeObject(PStmtKey key) throws java.lang.Exception
KeyedPooledObjectFactory
method for creatingPoolablePreparedStatement
s orPoolableCallableStatement
s. ThestmtType
field in the key determines whether a PoolablePreparedStatement or PoolableCallableStatement is created.- 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.- See Also:
createKey(String, int, int, StatementType)
-
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 statement to be normalized.- Returns:
- The canonical form of the supplied SQL statement.
-
passivateObject
public void passivateObject(PStmtKey key, PooledObject<DelegatingPreparedStatement> pooledObject) throws java.lang.Exception
KeyedPooledObjectFactory
method for passivatingPreparedStatement
s orCallableStatement
s. 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.apache.tomcat.dbcp.pool2.PooledObject<V>)
-
prepareCall
public java.sql.CallableStatement prepareCall(java.lang.String sql) throws java.sql.SQLException
Creates or obtains aCallableStatement
from the pool.- Specified by:
prepareCall
in interfacejava.sql.Connection
- Overrides:
prepareCall
in classDelegatingConnection<java.sql.Connection>
- Parameters:
sql
- the SQL string used to define the CallableStatement- Returns:
- a
PoolableCallableStatement
- Throws:
java.sql.SQLException
- Wraps an underlying exception.
-
prepareCall
public java.sql.CallableStatement prepareCall(java.lang.String sql, int resultSetType, int resultSetConcurrency) throws java.sql.SQLException
Creates or obtains aCallableStatement
from the pool.- Specified by:
prepareCall
in interfacejava.sql.Connection
- Overrides:
prepareCall
in classDelegatingConnection<java.sql.Connection>
- Parameters:
sql
- the SQL string used to define the CallableStatementresultSetType
- result set typeresultSetConcurrency
- result set concurrency- Returns:
- a
PoolableCallableStatement
- Throws:
java.sql.SQLException
- Wraps an underlying exception.
-
prepareCall
public java.sql.CallableStatement prepareCall(java.lang.String sql, int resultSetType, int resultSetConcurrency, int resultSetHoldability) throws java.sql.SQLException
Creates or obtains aCallableStatement
from the pool.- Specified by:
prepareCall
in interfacejava.sql.Connection
- Overrides:
prepareCall
in classDelegatingConnection<java.sql.Connection>
- Parameters:
sql
- the SQL string used to define the CallableStatementresultSetType
- result set typeresultSetConcurrency
- result set concurrencyresultSetHoldability
- result set holdability- Returns:
- a
PoolableCallableStatement
- Throws:
java.sql.SQLException
- Wraps an underlying exception.
-
prepareStatement
public java.sql.PreparedStatement prepareStatement(java.lang.String sql) throws java.sql.SQLException
Creates or obtains aPreparedStatement
from the pool.- Specified by:
prepareStatement
in interfacejava.sql.Connection
- Overrides:
prepareStatement
in classDelegatingConnection<java.sql.Connection>
- Parameters:
sql
- the SQL string used to define the PreparedStatement- Returns:
- a
PoolablePreparedStatement
- Throws:
java.sql.SQLException
- Wraps an underlying exception.
-
prepareStatement
public java.sql.PreparedStatement prepareStatement(java.lang.String sql, int autoGeneratedKeys) throws java.sql.SQLException
- Specified by:
prepareStatement
in interfacejava.sql.Connection
- Overrides:
prepareStatement
in classDelegatingConnection<java.sql.Connection>
- Throws:
java.sql.SQLException
-
prepareStatement
public java.sql.PreparedStatement prepareStatement(java.lang.String sql, int resultSetType, int resultSetConcurrency) throws java.sql.SQLException
Creates or obtains aPreparedStatement
from the pool.- Specified by:
prepareStatement
in interfacejava.sql.Connection
- Overrides:
prepareStatement
in classDelegatingConnection<java.sql.Connection>
- Parameters:
sql
- the SQL string used to define the PreparedStatementresultSetType
- result set typeresultSetConcurrency
- result set concurrency- Returns:
- a
PoolablePreparedStatement
- Throws:
java.sql.SQLException
- Wraps an underlying exception.
-
prepareStatement
public java.sql.PreparedStatement prepareStatement(java.lang.String sql, int resultSetType, int resultSetConcurrency, int resultSetHoldability) throws java.sql.SQLException
Creates or obtains aPreparedStatement
from the pool.- Specified by:
prepareStatement
in interfacejava.sql.Connection
- Overrides:
prepareStatement
in classDelegatingConnection<java.sql.Connection>
- Parameters:
sql
- the SQL string used to define the PreparedStatementresultSetType
- result set typeresultSetConcurrency
- result set concurrencyresultSetHoldability
- result set holdability- Returns:
- a
PoolablePreparedStatement
- Throws:
java.sql.SQLException
- Wraps an underlying exception.
-
prepareStatement
public java.sql.PreparedStatement prepareStatement(java.lang.String sql, int[] columnIndexes) throws java.sql.SQLException
Creates or obtains aPreparedStatement
from the pool.- Specified by:
prepareStatement
in interfacejava.sql.Connection
- Overrides:
prepareStatement
in classDelegatingConnection<java.sql.Connection>
- Parameters:
sql
- the SQL string used to define the PreparedStatementcolumnIndexes
- An array of column indexes indicating the columns that should be returned from the inserted row or rows.- Returns:
- a
PoolablePreparedStatement
- Throws:
java.sql.SQLException
- Wraps an underlying exception.
-
prepareStatement
public java.sql.PreparedStatement prepareStatement(java.lang.String sql, java.lang.String[] columnNames) throws java.sql.SQLException
Creates or obtains aPreparedStatement
from the pool.- Specified by:
prepareStatement
in interfacejava.sql.Connection
- Overrides:
prepareStatement
in classDelegatingConnection<java.sql.Connection>
- Parameters:
sql
- the SQL string used to define the PreparedStatementcolumnNames
- column names- Returns:
- a
PoolablePreparedStatement
- Throws:
java.sql.SQLException
- Wraps an underlying exception.
-
setClearStatementPoolOnReturn
public void setClearStatementPoolOnReturn(boolean clearStatementPoolOnReturn)
Sets whether the pool of statements should be cleared when the connection is returned to its pool. Default is false.- Parameters:
clearStatementPoolOnReturn
- clear or not- Since:
- 2.8.0
-
setStatementPool
public void setStatementPool(KeyedObjectPool<PStmtKey,DelegatingPreparedStatement> pool)
Sets the prepared statement pool.- Parameters:
pool
- the prepared statement pool.
-
toString
public java.lang.String toString()
Description copied from class:DelegatingConnection
Returns a string representation of the metadata associated with the innermost delegate connection.- Overrides:
toString
in classDelegatingConnection<java.sql.Connection>
-
validateObject
public boolean validateObject(PStmtKey key, PooledObject<DelegatingPreparedStatement> pooledObject)
KeyedPooledObjectFactory
method for validating pooled statements. Currently always returns true.- Specified by:
validateObject
in interfaceKeyedPooledObjectFactory<PStmtKey,DelegatingPreparedStatement>
- Parameters:
key
- ignoredpooledObject
- ignored- Returns:
true
-
-