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> ADelegatingConnectionthat poolsPreparedStatements.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 SummaryNested Classes Modifier and Type Class Description static classPoolingConnection.StatementTypeStatement types.
 - 
Constructor SummaryConstructors Constructor Description PoolingConnection(java.sql.Connection connection)Constructor.
 - 
Method SummaryAll Methods Instance Methods Concrete Methods Modifier and Type Method Description voidactivateObject(PStmtKey key, PooledObject<DelegatingPreparedStatement> pooledObject)KeyedPooledObjectFactorymethod for activating pooled statements.voidclose()Closes and frees allPreparedStatements orCallableStatements from the pool, and close the underlying connection.voidconnectionReturnedToPool()Notification fromPoolableConnectionthat we returned to the pool.protected PStmtKeycreateKey(java.lang.String sql)Creates a PStmtKey for the given arguments.protected PStmtKeycreateKey(java.lang.String sql, int autoGeneratedKeys)Creates a PStmtKey for the given arguments.protected PStmtKeycreateKey(java.lang.String sql, int[] columnIndexes)Creates a PStmtKey for the given arguments.protected PStmtKeycreateKey(java.lang.String sql, int resultSetType, int resultSetConcurrency)Creates a PStmtKey for the given arguments.protected PStmtKeycreateKey(java.lang.String sql, int resultSetType, int resultSetConcurrency, int resultSetHoldability)Creates a PStmtKey for the given arguments.protected PStmtKeycreateKey(java.lang.String sql, int resultSetType, int resultSetConcurrency, int resultSetHoldability, PoolingConnection.StatementType statementType)Creates a PStmtKey for the given arguments.protected PStmtKeycreateKey(java.lang.String sql, int resultSetType, int resultSetConcurrency, PoolingConnection.StatementType statementType)Creates a PStmtKey for the given arguments.protected PStmtKeycreateKey(java.lang.String sql, java.lang.String[] columnNames)Creates a PStmtKey for the given arguments.protected PStmtKeycreateKey(java.lang.String sql, PoolingConnection.StatementType statementType)Creates a PStmtKey for the given arguments.voiddestroyObject(PStmtKey key, PooledObject<DelegatingPreparedStatement> pooledObject)KeyedPooledObjectFactorymethod for destroying PoolablePreparedStatements and PoolableCallableStatements.KeyedObjectPool<PStmtKey,DelegatingPreparedStatement>getStatementPool()Gets the prepared statement pool.PooledObject<DelegatingPreparedStatement>makeObject(PStmtKey key)KeyedPooledObjectFactorymethod for creatingPoolablePreparedStatements orPoolableCallableStatements.protected java.lang.StringnormalizeSQL(java.lang.String sql)Normalizes the given SQL statement, producing a canonical form that is semantically equivalent to the original.voidpassivateObject(PStmtKey key, PooledObject<DelegatingPreparedStatement> pooledObject)java.sql.CallableStatementprepareCall(java.lang.String sql)Creates or obtains aCallableStatementfrom the pool.java.sql.CallableStatementprepareCall(java.lang.String sql, int resultSetType, int resultSetConcurrency)Creates or obtains aCallableStatementfrom the pool.java.sql.CallableStatementprepareCall(java.lang.String sql, int resultSetType, int resultSetConcurrency, int resultSetHoldability)Creates or obtains aCallableStatementfrom the pool.java.sql.PreparedStatementprepareStatement(java.lang.String sql)Creates or obtains aPreparedStatementfrom the pool.java.sql.PreparedStatementprepareStatement(java.lang.String sql, int autoGeneratedKeys)java.sql.PreparedStatementprepareStatement(java.lang.String sql, int[] columnIndexes)Creates or obtains aPreparedStatementfrom the pool.java.sql.PreparedStatementprepareStatement(java.lang.String sql, int resultSetType, int resultSetConcurrency)Creates or obtains aPreparedStatementfrom the pool.java.sql.PreparedStatementprepareStatement(java.lang.String sql, int resultSetType, int resultSetConcurrency, int resultSetHoldability)Creates or obtains aPreparedStatementfrom the pool.java.sql.PreparedStatementprepareStatement(java.lang.String sql, java.lang.String[] columnNames)Creates or obtains aPreparedStatementfrom the pool.voidsetClearStatementPoolOnReturn(boolean clearStatementPoolOnReturn)Sets whether the pool of statements should be cleared when the connection is returned to its pool.voidsetStatementPool(KeyedObjectPool<PStmtKey,DelegatingPreparedStatement> pool)Sets the prepared statement pool.java.lang.StringtoString()Returns a string representation of the metadata associated with the innermost delegate connection.booleanvalidateObject(PStmtKey key, PooledObject<DelegatingPreparedStatement> pooledObject)KeyedPooledObjectFactorymethod for validating pooled statements.- 
Methods inherited from class org.apache.tomcat.dbcp.dbcp2.DelegatingConnectionabort, 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.AbandonedTraceaddTrace, clearTrace, close, getLastUsed, getLastUsedInstant, getTrace, removeThisTrace, removeTrace, setLastUsed, setLastUsed, setLastUsed
 - 
Methods inherited from class java.lang.Objectclone, equals, finalize, getClass, hashCode, notify, notifyAll, wait, wait, wait
 - 
Methods inherited from interface java.sql.ConnectionbeginRequest, endRequest, setShardingKey, setShardingKey, setShardingKeyIfValid, setShardingKeyIfValid
 - 
Methods inherited from interface org.apache.tomcat.dbcp.pool2.KeyedPooledObjectFactorydestroyObject
 
- 
 
- 
- 
- 
Method Detail- 
activateObjectpublic void activateObject(PStmtKey key, PooledObject<DelegatingPreparedStatement> pooledObject) throws java.sql.SQLException KeyedPooledObjectFactorymethod for activating pooled statements.- Specified by:
- activateObjectin interface- KeyedPooledObjectFactory<PStmtKey,DelegatingPreparedStatement>
- Parameters:
- key- ignored
- pooledObject- wrapped pooled statement to be activated
- Throws:
- java.sql.SQLException
- See Also:
- KeyedPooledObjectFactory.destroyObject(K, org.apache.tomcat.dbcp.pool2.PooledObject<V>)
 
 - 
closepublic void close() throws java.sql.SQLExceptionCloses and frees allPreparedStatements orCallableStatements from the pool, and close the underlying connection.- Specified by:
- closein interface- java.lang.AutoCloseable
- Specified by:
- closein interface- java.sql.Connection
- Overrides:
- closein class- DelegatingConnection<java.sql.Connection>
- Throws:
- java.sql.SQLException- Ignored here, for subclasses.
 
 - 
connectionReturnedToPoolpublic void connectionReturnedToPool() throws java.sql.SQLExceptionNotification fromPoolableConnectionthat we returned to the pool.- Throws:
- java.sql.SQLException- when- clearStatementPoolOnReturnis true and the statement pool could not be cleared
- Since:
- 2.8.0
 
 - 
createKeyprotected 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.
 
 - 
createKeyprotected PStmtKey createKey(java.lang.String sql, int autoGeneratedKeys) Creates a PStmtKey for the given arguments.- Parameters:
- sql- the SQL string used to define the statement
- autoGeneratedKeys- A flag indicating whether auto-generated keys should be returned; one of- Statement.RETURN_GENERATED_KEYSor- Statement.NO_GENERATED_KEYS.
- Returns:
- the PStmtKey created for the given arguments.
 
 - 
createKeyprotected 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 statement
- resultSetType- result set type
- resultSetConcurrency- result set concurrency
- Returns:
- the PStmtKey created for the given arguments.
 
 - 
createKeyprotected 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 statement
- resultSetType- result set type
- resultSetConcurrency- result set concurrency
- resultSetHoldability- result set holdability
- Returns:
- the PStmtKey created for the given arguments.
 
 - 
createKeyprotected 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 statement
- resultSetType- result set type
- resultSetConcurrency- result set concurrency
- resultSetHoldability- result set holdability
- statementType- statement type
- Returns:
- the PStmtKey created for the given arguments.
 
 - 
createKeyprotected 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 statement
- resultSetType- result set type
- resultSetConcurrency- result set concurrency
- statementType- statement type
- Returns:
- the PStmtKey created for the given arguments.
 
 - 
createKeyprotected PStmtKey createKey(java.lang.String sql, int[] columnIndexes) Creates a PStmtKey for the given arguments.- Parameters:
- sql- the SQL string used to define the statement
- columnIndexes- 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.
 
 - 
createKeyprotected 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 statement
- statementType- statement type
- Returns:
- the PStmtKey created for the given arguments.
 
 - 
createKeyprotected 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 statement
- columnNames- column names
- Returns:
- the PStmtKey created for the given arguments.
 
 - 
destroyObjectpublic void destroyObject(PStmtKey key, PooledObject<DelegatingPreparedStatement> pooledObject) throws java.sql.SQLException KeyedPooledObjectFactorymethod for destroying PoolablePreparedStatements and PoolableCallableStatements. Closes the underlying statement.- Specified by:
- destroyObjectin interface- KeyedPooledObjectFactory<PStmtKey,DelegatingPreparedStatement>
- Parameters:
- key- ignored
- pooledObject- the wrapped pooled statement to be destroyed.
- Throws:
- java.sql.SQLException
- See Also:
- KeyedPooledObjectFactory.validateObject(K, org.apache.tomcat.dbcp.pool2.PooledObject<V>),- KeyedObjectPool.invalidateObject(K, V)
 
 - 
getStatementPoolpublic KeyedObjectPool<PStmtKey,DelegatingPreparedStatement> getStatementPool() Gets the prepared statement pool.- Returns:
- statement pool
- Since:
- 2.8.0
 
 - 
makeObjectpublic PooledObject<DelegatingPreparedStatement> makeObject(PStmtKey key) throws java.sql.SQLException KeyedPooledObjectFactorymethod for creatingPoolablePreparedStatements orPoolableCallableStatements. ThestmtTypefield in the key determines whether a PoolablePreparedStatement or PoolableCallableStatement is created.- Specified by:
- makeObjectin interface- KeyedPooledObjectFactory<PStmtKey,DelegatingPreparedStatement>
- Parameters:
- key- the key for the- PreparedStatementto be created
- Returns:
- a PooledObjectwrapping an instance that can be served by the pool.
- Throws:
- java.sql.SQLException
- See Also:
- createKey(String, int, int, StatementType)
 
 - 
normalizeSQLprotected 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.
 
 - 
passivateObjectpublic void passivateObject(PStmtKey key, PooledObject<DelegatingPreparedStatement> pooledObject) throws java.sql.SQLException KeyedPooledObjectFactorymethod for passivatingPreparedStatements orCallableStatements. InvokesPreparedStatement.clearParameters().- Specified by:
- passivateObjectin interface- KeyedPooledObjectFactory<PStmtKey,DelegatingPreparedStatement>
- Parameters:
- key- ignored
- pooledObject- a wrapped- PreparedStatement
- Throws:
- java.sql.SQLException
- See Also:
- KeyedPooledObjectFactory.destroyObject(K, org.apache.tomcat.dbcp.pool2.PooledObject<V>)
 
 - 
prepareCallpublic java.sql.CallableStatement prepareCall(java.lang.String sql) throws java.sql.SQLExceptionCreates or obtains aCallableStatementfrom the pool.- Specified by:
- prepareCallin interface- java.sql.Connection
- Overrides:
- prepareCallin class- DelegatingConnection<java.sql.Connection>
- Parameters:
- sql- the SQL string used to define the CallableStatement
- Returns:
- a PoolableCallableStatement
- Throws:
- java.sql.SQLException- Wraps an underlying exception.
 
 - 
prepareCallpublic java.sql.CallableStatement prepareCall(java.lang.String sql, int resultSetType, int resultSetConcurrency) throws java.sql.SQLExceptionCreates or obtains aCallableStatementfrom the pool.- Specified by:
- prepareCallin interface- java.sql.Connection
- Overrides:
- prepareCallin class- DelegatingConnection<java.sql.Connection>
- Parameters:
- sql- the SQL string used to define the CallableStatement
- resultSetType- result set type
- resultSetConcurrency- result set concurrency
- Returns:
- a PoolableCallableStatement
- Throws:
- java.sql.SQLException- Wraps an underlying exception.
 
 - 
prepareCallpublic java.sql.CallableStatement prepareCall(java.lang.String sql, int resultSetType, int resultSetConcurrency, int resultSetHoldability) throws java.sql.SQLExceptionCreates or obtains aCallableStatementfrom the pool.- Specified by:
- prepareCallin interface- java.sql.Connection
- Overrides:
- prepareCallin class- DelegatingConnection<java.sql.Connection>
- Parameters:
- sql- the SQL string used to define the CallableStatement
- resultSetType- result set type
- resultSetConcurrency- result set concurrency
- resultSetHoldability- result set holdability
- Returns:
- a PoolableCallableStatement
- Throws:
- java.sql.SQLException- Wraps an underlying exception.
 
 - 
prepareStatementpublic java.sql.PreparedStatement prepareStatement(java.lang.String sql) throws java.sql.SQLExceptionCreates or obtains aPreparedStatementfrom the pool.- Specified by:
- prepareStatementin interface- java.sql.Connection
- Overrides:
- prepareStatementin class- DelegatingConnection<java.sql.Connection>
- Parameters:
- sql- the SQL string used to define the PreparedStatement
- Returns:
- a PoolablePreparedStatement
- Throws:
- java.sql.SQLException- Wraps an underlying exception.
 
 - 
prepareStatementpublic java.sql.PreparedStatement prepareStatement(java.lang.String sql, int autoGeneratedKeys) throws java.sql.SQLException- Specified by:
- prepareStatementin interface- java.sql.Connection
- Overrides:
- prepareStatementin class- DelegatingConnection<java.sql.Connection>
- Throws:
- java.sql.SQLException
 
 - 
prepareStatementpublic java.sql.PreparedStatement prepareStatement(java.lang.String sql, int resultSetType, int resultSetConcurrency) throws java.sql.SQLExceptionCreates or obtains aPreparedStatementfrom the pool.- Specified by:
- prepareStatementin interface- java.sql.Connection
- Overrides:
- prepareStatementin class- DelegatingConnection<java.sql.Connection>
- Parameters:
- sql- the SQL string used to define the PreparedStatement
- resultSetType- result set type
- resultSetConcurrency- result set concurrency
- Returns:
- a PoolablePreparedStatement
- Throws:
- java.sql.SQLException- Wraps an underlying exception.
 
 - 
prepareStatementpublic java.sql.PreparedStatement prepareStatement(java.lang.String sql, int resultSetType, int resultSetConcurrency, int resultSetHoldability) throws java.sql.SQLExceptionCreates or obtains aPreparedStatementfrom the pool.- Specified by:
- prepareStatementin interface- java.sql.Connection
- Overrides:
- prepareStatementin class- DelegatingConnection<java.sql.Connection>
- Parameters:
- sql- the SQL string used to define the PreparedStatement
- resultSetType- result set type
- resultSetConcurrency- result set concurrency
- resultSetHoldability- result set holdability
- Returns:
- a PoolablePreparedStatement
- Throws:
- java.sql.SQLException- Wraps an underlying exception.
 
 - 
prepareStatementpublic java.sql.PreparedStatement prepareStatement(java.lang.String sql, int[] columnIndexes) throws java.sql.SQLExceptionCreates or obtains aPreparedStatementfrom the pool.- Specified by:
- prepareStatementin interface- java.sql.Connection
- Overrides:
- prepareStatementin class- DelegatingConnection<java.sql.Connection>
- Parameters:
- sql- the SQL string used to define the PreparedStatement
- columnIndexes- 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.
 
 - 
prepareStatementpublic java.sql.PreparedStatement prepareStatement(java.lang.String sql, java.lang.String[] columnNames) throws java.sql.SQLExceptionCreates or obtains aPreparedStatementfrom the pool.- Specified by:
- prepareStatementin interface- java.sql.Connection
- Overrides:
- prepareStatementin class- DelegatingConnection<java.sql.Connection>
- Parameters:
- sql- the SQL string used to define the PreparedStatement
- columnNames- column names
- Returns:
- a PoolablePreparedStatement
- Throws:
- java.sql.SQLException- Wraps an underlying exception.
 
 - 
setClearStatementPoolOnReturnpublic 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
 
 - 
setStatementPoolpublic void setStatementPool(KeyedObjectPool<PStmtKey,DelegatingPreparedStatement> pool) Sets the prepared statement pool.- Parameters:
- pool- the prepared statement pool.
 
 - 
toStringpublic java.lang.String toString() Description copied from class:DelegatingConnectionReturns a string representation of the metadata associated with the innermost delegate connection.- Overrides:
- toStringin class- DelegatingConnection<java.sql.Connection>
 
 - 
validateObjectpublic boolean validateObject(PStmtKey key, PooledObject<DelegatingPreparedStatement> pooledObject) KeyedPooledObjectFactorymethod for validating pooled statements. Currently always returns true.- Specified by:
- validateObjectin interface- KeyedPooledObjectFactory<PStmtKey,DelegatingPreparedStatement>
- Parameters:
- key- ignored
- pooledObject- ignored
- Returns:
- true
 
 
- 
 
-