org.apache.commons.dbcp2
Class PoolingConnection
- java.lang.Object
-
- org.apache.commons.dbcp2.AbandonedTrace
-
- org.apache.commons.dbcp2.DelegatingConnection<Connection>
-
- org.apache.commons.dbcp2.PoolingConnection
-
- All Implemented Interfaces:
- AutoCloseable, Connection, Wrapper, KeyedPooledObjectFactory<PStmtKey,DelegatingPreparedStatement>, TrackedUse
public class PoolingConnection extends DelegatingConnection<Connection> implements KeyedPooledObjectFactory<PStmtKey,DelegatingPreparedStatement>
ADelegatingConnection
that poolsPreparedStatement
s.The
prepareStatement(java.lang.String)
andprepareCall(java.lang.String)
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 and Description static class
PoolingConnection.StatementType
Statement types.
-
Field Summary
-
Fields inherited from interface java.sql.Connection
TRANSACTION_NONE, TRANSACTION_READ_COMMITTED, TRANSACTION_READ_UNCOMMITTED, TRANSACTION_REPEATABLE_READ, TRANSACTION_SERIALIZABLE
-
-
Constructor Summary
Constructors Constructor and Description PoolingConnection(Connection connection)
Constructor.
-
Method Summary
All Methods Instance Methods Concrete Methods Modifier and Type Method and 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.protected PStmtKey
createKey(String sql)
Creates a PStmtKey for the given arguments.protected PStmtKey
createKey(String sql, int autoGeneratedKeys)
protected PStmtKey
createKey(String sql, int[] columnIndexes)
Creates a PStmtKey for the given arguments.protected PStmtKey
createKey(String sql, int resultSetType, int resultSetConcurrency)
Creates a PStmtKey for the given arguments.protected PStmtKey
createKey(String sql, int resultSetType, int resultSetConcurrency, int resultSetHoldability)
Creates a PStmtKey for the given arguments.protected PStmtKey
createKey(String sql, int resultSetType, int resultSetConcurrency, int resultSetHoldability, PoolingConnection.StatementType stmtType)
Creates a PStmtKey for the given arguments.protected PStmtKey
createKey(String sql, int resultSetType, int resultSetConcurrency, PoolingConnection.StatementType stmtType)
Creates a PStmtKey for the given arguments.protected PStmtKey
createKey(String sql, PoolingConnection.StatementType stmtType)
Creates a PStmtKey for the given arguments.protected PStmtKey
createKey(String sql, String[] columnNames)
Creates a PStmtKey for the given arguments.void
destroyObject(PStmtKey key, PooledObject<DelegatingPreparedStatement> pooledObject)
KeyedPooledObjectFactory
method for destroying PoolablePreparedStatements and PoolableCallableStatements.PooledObject<DelegatingPreparedStatement>
makeObject(PStmtKey key)
KeyedPooledObjectFactory
method for creatingPoolablePreparedStatement
s orPoolableCallableStatement
s.protected String
normalizeSQL(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)
CallableStatement
prepareCall(String sql)
Creates or obtains aCallableStatement
from the pool.CallableStatement
prepareCall(String sql, int resultSetType, int resultSetConcurrency)
Creates or obtains aCallableStatement
from the pool.CallableStatement
prepareCall(String sql, int resultSetType, int resultSetConcurrency, int resultSetHoldability)
Creates or obtains aCallableStatement
from the pool.PreparedStatement
prepareStatement(String sql)
Creates or obtains aPreparedStatement
from the pool.PreparedStatement
prepareStatement(String sql, int autoGeneratedKeys)
PreparedStatement
prepareStatement(String sql, int[] columnIndexes)
Creates or obtains aPreparedStatement
from the pool.PreparedStatement
prepareStatement(String sql, int resultSetType, int resultSetConcurrency)
Creates or obtains aPreparedStatement
from the pool.PreparedStatement
prepareStatement(String sql, int resultSetType, int resultSetConcurrency, int resultSetHoldability)
Creates or obtains aPreparedStatement
from the pool.PreparedStatement
prepareStatement(String sql, String[] columnNames)
Creates or obtains aPreparedStatement
from the pool.void
setStatementPool(KeyedObjectPool<PStmtKey,DelegatingPreparedStatement> pool)
Sets the prepared statement pool.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.commons.dbcp2.DelegatingConnection
abort, activate, checkOpen, clearCachedState, clearWarnings, closeInternal, commit, createArrayOf, createBlob, createClob, createNClob, createSQLXML, createStatement, createStatement, createStatement, createStruct, getAutoCommit, getCacheState, getCatalog, getClientInfo, getClientInfo, getDefaultQueryTimeout, getDelegate, getDelegateInternal, getHoldability, getInnermostDelegate, getInnermostDelegateInternal, getMetaData, getNetworkTimeout, getSchema, getTransactionIsolation, getTypeMap, getWarnings, handleException, innermostDelegateEquals, isClosed, isClosedInternal, isReadOnly, isValid, isWrapperFor, nativeSQL, passivate, releaseSavepoint, rollback, rollback, setAutoCommit, setCacheState, setCatalog, setClientInfo, setClientInfo, setClosedInternal, setDefaultQueryTimeout, setDelegate, setHoldability, setNetworkTimeout, setReadOnly, setSavepoint, setSavepoint, setSchema, setTransactionIsolation, setTypeMap, unwrap
-
Methods inherited from class org.apache.commons.dbcp2.AbandonedTrace
addTrace, clearTrace, getLastUsed, getTrace, removeTrace, setLastUsed, setLastUsed
-
-
-
-
Constructor Detail
-
PoolingConnection
public PoolingConnection(Connection connection)
Constructor.- Parameters:
connection
- the underlyingConnection
.
-
-
Method Detail
-
activateObject
public void activateObject(PStmtKey key, PooledObject<DelegatingPreparedStatement> pooledObject) throws Exception
KeyedPooledObjectFactory
method for activating pooled statements.- Specified by:
activateObject
in interfaceKeyedPooledObjectFactory<PStmtKey,DelegatingPreparedStatement>
- Parameters:
key
- ignoredpooledObject
- wrapped pooled statement to be activated- Throws:
Exception
-
close
public void close() throws SQLException
Closes and frees allPreparedStatement
s orCallableStatement
s from the pool, and close the underlying connection.- Specified by:
close
in interfaceAutoCloseable
- Specified by:
close
in interfaceConnection
- Overrides:
close
in classDelegatingConnection<Connection>
- Throws:
SQLException
-
createKey
protected PStmtKey createKey(String sql)
Creates a PStmtKey for the given arguments.- Parameters:
sql
- the SQL string used to define the statement
-
createKey
protected PStmtKey createKey(String sql, int[] columnIndexes)
Creates a PStmtKey for the given arguments.- Parameters:
sql
- the SQL string used to define the statementcolumnIndexes
- column indexes
-
createKey
protected PStmtKey createKey(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
-
createKey
protected PStmtKey createKey(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
-
createKey
protected PStmtKey createKey(String sql, int resultSetType, int resultSetConcurrency, int resultSetHoldability, PoolingConnection.StatementType stmtType)
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 holdabilitystmtType
- statement type
-
createKey
protected PStmtKey createKey(String sql, int resultSetType, int resultSetConcurrency, PoolingConnection.StatementType stmtType)
Creates a PStmtKey for the given arguments.- Parameters:
sql
- the SQL string used to define the statementresultSetType
- result set typeresultSetConcurrency
- result set concurrencystmtType
- statement type
-
createKey
protected PStmtKey createKey(String sql, PoolingConnection.StatementType stmtType)
Creates a PStmtKey for the given arguments.- Parameters:
sql
- the SQL string used to define the statementstmtType
- statement type
-
createKey
protected PStmtKey createKey(String sql, String[] columnNames)
Creates a PStmtKey for the given arguments.- Parameters:
sql
- the SQL string used to define the statementcolumnNames
- column names
-
destroyObject
public void destroyObject(PStmtKey key, PooledObject<DelegatingPreparedStatement> pooledObject) throws 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:
Exception
-
makeObject
public PooledObject<DelegatingPreparedStatement> makeObject(PStmtKey key) throws 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- Throws:
Exception
- See Also:
createKey(String, int, int, StatementType)
-
normalizeSQL
protected String normalizeSQL(String sql)
Normalizes the given SQL statement, producing a canonical form that is semantically equivalent to the original.
-
passivateObject
public void passivateObject(PStmtKey key, PooledObject<DelegatingPreparedStatement> pooledObject) throws Exception
KeyedPooledObjectFactory
method for passivatingPreparedStatement
s orCallableStatement
s. InvokesPreparedStatement.clearParameters()
.- Specified by:
passivateObject
in interfaceKeyedPooledObjectFactory<PStmtKey,DelegatingPreparedStatement>
- Parameters:
key
- ignoredpooledObject
- a wrappedPreparedStatement
- Throws:
Exception
-
prepareCall
public CallableStatement prepareCall(String sql) throws SQLException
Creates or obtains aCallableStatement
from the pool.- Specified by:
prepareCall
in interfaceConnection
- Overrides:
prepareCall
in classDelegatingConnection<Connection>
- Parameters:
sql
- the SQL string used to define the CallableStatement- Returns:
- a
PoolableCallableStatement
- Throws:
SQLException
- Wraps an underlying exception.
-
prepareCall
public CallableStatement prepareCall(String sql, int resultSetType, int resultSetConcurrency) throws SQLException
Creates or obtains aCallableStatement
from the pool.- Specified by:
prepareCall
in interfaceConnection
- Overrides:
prepareCall
in classDelegatingConnection<Connection>
- Parameters:
sql
- the SQL string used to define the CallableStatementresultSetType
- result set typeresultSetConcurrency
- result set concurrency- Returns:
- a
PoolableCallableStatement
- Throws:
SQLException
- Wraps an underlying exception.
-
prepareCall
public CallableStatement prepareCall(String sql, int resultSetType, int resultSetConcurrency, int resultSetHoldability) throws SQLException
Creates or obtains aCallableStatement
from the pool.- Specified by:
prepareCall
in interfaceConnection
- Overrides:
prepareCall
in classDelegatingConnection<Connection>
- Parameters:
sql
- the SQL string used to define the CallableStatementresultSetType
- result set typeresultSetConcurrency
- result set concurrencyresultSetHoldability
- result set holdability- Returns:
- a
PoolableCallableStatement
- Throws:
SQLException
- Wraps an underlying exception.
-
prepareStatement
public PreparedStatement prepareStatement(String sql) throws SQLException
Creates or obtains aPreparedStatement
from the pool.- Specified by:
prepareStatement
in interfaceConnection
- Overrides:
prepareStatement
in classDelegatingConnection<Connection>
- Parameters:
sql
- the SQL string used to define the PreparedStatement- Returns:
- a
PoolablePreparedStatement
- Throws:
SQLException
-
prepareStatement
public PreparedStatement prepareStatement(String sql, int autoGeneratedKeys) throws SQLException
- Specified by:
prepareStatement
in interfaceConnection
- Overrides:
prepareStatement
in classDelegatingConnection<Connection>
- Throws:
SQLException
-
prepareStatement
public PreparedStatement prepareStatement(String sql, int[] columnIndexes) throws SQLException
Creates or obtains aPreparedStatement
from the pool.- Specified by:
prepareStatement
in interfaceConnection
- Overrides:
prepareStatement
in classDelegatingConnection<Connection>
- Parameters:
sql
- the SQL string used to define the PreparedStatementcolumnIndexes
- column indexes- Returns:
- a
PoolablePreparedStatement
- Throws:
SQLException
-
prepareStatement
public PreparedStatement prepareStatement(String sql, int resultSetType, int resultSetConcurrency) throws SQLException
Creates or obtains aPreparedStatement
from the pool.- Specified by:
prepareStatement
in interfaceConnection
- Overrides:
prepareStatement
in classDelegatingConnection<Connection>
- Parameters:
sql
- the SQL string used to define the PreparedStatementresultSetType
- result set typeresultSetConcurrency
- result set concurrency- Returns:
- a
PoolablePreparedStatement
- Throws:
SQLException
-
prepareStatement
public PreparedStatement prepareStatement(String sql, int resultSetType, int resultSetConcurrency, int resultSetHoldability) throws SQLException
Creates or obtains aPreparedStatement
from the pool.- Specified by:
prepareStatement
in interfaceConnection
- Overrides:
prepareStatement
in classDelegatingConnection<Connection>
- Parameters:
sql
- the SQL string used to define the PreparedStatementresultSetType
- result set typeresultSetConcurrency
- result set concurrencyresultSetHoldability
- result set holdability- Returns:
- a
PoolablePreparedStatement
- Throws:
SQLException
-
prepareStatement
public PreparedStatement prepareStatement(String sql, String[] columnNames) throws SQLException
Creates or obtains aPreparedStatement
from the pool.- Specified by:
prepareStatement
in interfaceConnection
- Overrides:
prepareStatement
in classDelegatingConnection<Connection>
- Parameters:
sql
- the SQL string used to define the PreparedStatementcolumnNames
- column names- Returns:
- a
PoolablePreparedStatement
- Throws:
SQLException
-
setStatementPool
public void setStatementPool(KeyedObjectPool<PStmtKey,DelegatingPreparedStatement> pool)
Sets the prepared statement pool.- Parameters:
pool
- the prepared statement pool.
-
toString
public String toString()
Description copied from class:DelegatingConnection
Returns a string representation of the metadata associated with the innermost delegate connection.- Overrides:
toString
in classDelegatingConnection<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
-
-