Package 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
,org.apache.commons.pool2.KeyedPooledObjectFactory<PStmtKey,
,DelegatingPreparedStatement> org.apache.commons.pool2.TrackedUse
public class PoolingConnection
extends DelegatingConnection<Connection>
implements org.apache.commons.pool2.KeyedPooledObjectFactory<PStmtKey,DelegatingPreparedStatement>
A
DelegatingConnection
that pools PreparedStatement
s.
The prepareStatement(java.lang.String)
and prepareCall(java.lang.String)
methods, rather than
creating a new PreparedStatement each time, may actually pull the statement
from a pool of unused statements.
The Statement.close()
method of the returned statement doesn't
actually close the statement, but rather returns it to the pool.
(See PoolablePreparedStatement
, PoolableCallableStatement
.)
- Since:
- 2.0
- Version:
- $Id: PoolingConnection.java 1658644 2015-02-10 08:59:07Z tn $
- Author:
- Rodney Waldhoff, Dirk Verbeeck
- See Also:
-
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 -
Method Summary
Modifier and TypeMethodDescriptionvoid
activateObject
(PStmtKey key, org.apache.commons.pool2.PooledObject<DelegatingPreparedStatement> p) KeyedPooledObjectFactory
method for activating pooled statements.void
close()
Close and free allPreparedStatement
s orCallableStatement
s from the pool, and close the underlying connection.void
destroyObject
(PStmtKey key, org.apache.commons.pool2.PooledObject<DelegatingPreparedStatement> p) KeyedPooledObjectFactory
method for destroying PoolablePreparedStatements and PoolableCallableStatements.org.apache.commons.pool2.PooledObject
<DelegatingPreparedStatement> makeObject
(PStmtKey key) KeyedPooledObjectFactory
method for creatingPoolablePreparedStatement
s orPoolableCallableStatement
s.void
passivateObject
(PStmtKey key, org.apache.commons.pool2.PooledObject<DelegatingPreparedStatement> p) prepareCall
(String sql) Create or obtain aCallableStatement
from the pool.prepareCall
(String sql, int resultSetType, int resultSetConcurrency) Create or obtain aCallableStatement
from the pool.prepareStatement
(String sql) Create or obtain aPreparedStatement
from the pool.prepareStatement
(String sql, int autoGeneratedKeys) prepareStatement
(String sql, int resultSetType, int resultSetConcurrency) Create or obtain aPreparedStatement
from the pool.void
setStatementPool
(org.apache.commons.pool2.KeyedObjectPool<PStmtKey, DelegatingPreparedStatement> pool) toString()
Returns a string representation of the metadata associated with the innermost delegate connection.boolean
validateObject
(PStmtKey key, org.apache.commons.pool2.PooledObject<DelegatingPreparedStatement> p) KeyedPooledObjectFactory
method for validating pooled statements.Methods inherited from class org.apache.commons.dbcp2.DelegatingConnection
abort, clearCachedState, clearWarnings, commit, createArrayOf, createBlob, createClob, createNClob, createSQLXML, createStatement, createStatement, createStatement, createStruct, getAutoCommit, getCacheState, getCatalog, getClientInfo, getClientInfo, getDefaultQueryTimeout, getDelegate, getHoldability, getInnermostDelegate, getInnermostDelegateInternal, getMetaData, getNetworkTimeout, getSchema, getTransactionIsolation, getTypeMap, getWarnings, innermostDelegateEquals, isClosed, isReadOnly, isValid, isWrapperFor, nativeSQL, prepareCall, prepareStatement, prepareStatement, prepareStatement, releaseSavepoint, rollback, rollback, setAutoCommit, setCacheState, setCatalog, setClientInfo, setClientInfo, setDefaultQueryTimeout, setDelegate, setHoldability, setNetworkTimeout, setReadOnly, setSavepoint, setSavepoint, setSchema, setTransactionIsolation, setTypeMap, unwrap
Methods inherited from class org.apache.commons.dbcp2.AbandonedTrace
getLastUsed
Methods inherited from class java.lang.Object
equals, getClass, hashCode, notify, notifyAll, wait, wait, wait
Methods inherited from interface java.sql.Connection
beginRequest, endRequest, setShardingKey, setShardingKey, setShardingKeyIfValid, setShardingKeyIfValid
-
Constructor Details
-
PoolingConnection
Constructor.- Parameters:
c
- the underlyingConnection
.
-
-
Method Details
-
setStatementPool
public void setStatementPool(org.apache.commons.pool2.KeyedObjectPool<PStmtKey, DelegatingPreparedStatement> pool) -
close
Close and free 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
-
prepareStatement
Create or obtain 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
- Specified by:
prepareStatement
in interfaceConnection
- Overrides:
prepareStatement
in classDelegatingConnection<Connection>
- Throws:
SQLException
-
prepareStatement
public PreparedStatement prepareStatement(String sql, int resultSetType, int resultSetConcurrency) throws SQLException Create or obtain 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
-
prepareCall
Create or obtain 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
-
prepareCall
public CallableStatement prepareCall(String sql, int resultSetType, int resultSetConcurrency) throws SQLException Create or obtain 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
-
makeObject
public org.apache.commons.pool2.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 interfaceorg.apache.commons.pool2.KeyedPooledObjectFactory<PStmtKey,
DelegatingPreparedStatement> - Parameters:
key
- the key for thePreparedStatement
to be created- Throws:
Exception
- See Also:
-
destroyObject
public void destroyObject(PStmtKey key, org.apache.commons.pool2.PooledObject<DelegatingPreparedStatement> p) throws Exception KeyedPooledObjectFactory
method for destroying PoolablePreparedStatements and PoolableCallableStatements. Closes the underlying statement.- Specified by:
destroyObject
in interfaceorg.apache.commons.pool2.KeyedPooledObjectFactory<PStmtKey,
DelegatingPreparedStatement> - Parameters:
key
- ignoredp
- the wrapped pooled statement to be destroyed.- Throws:
Exception
-
validateObject
public boolean validateObject(PStmtKey key, org.apache.commons.pool2.PooledObject<DelegatingPreparedStatement> p) KeyedPooledObjectFactory
method for validating pooled statements. Currently always returns true.- Specified by:
validateObject
in interfaceorg.apache.commons.pool2.KeyedPooledObjectFactory<PStmtKey,
DelegatingPreparedStatement> - Parameters:
key
- ignoredp
- ignored- Returns:
true
-
activateObject
public void activateObject(PStmtKey key, org.apache.commons.pool2.PooledObject<DelegatingPreparedStatement> p) throws Exception KeyedPooledObjectFactory
method for activating pooled statements.- Specified by:
activateObject
in interfaceorg.apache.commons.pool2.KeyedPooledObjectFactory<PStmtKey,
DelegatingPreparedStatement> - Parameters:
key
- ignoredp
- wrapped pooled statement to be activated- Throws:
Exception
-
passivateObject
public void passivateObject(PStmtKey key, org.apache.commons.pool2.PooledObject<DelegatingPreparedStatement> p) throws Exception KeyedPooledObjectFactory
method for passivatingPreparedStatement
s orCallableStatement
s. InvokesPreparedStatement.clearParameters()
.- Specified by:
passivateObject
in interfaceorg.apache.commons.pool2.KeyedPooledObjectFactory<PStmtKey,
DelegatingPreparedStatement> - Parameters:
key
- ignoredp
- a wrappedPreparedStatement
- Throws:
Exception
-
toString
Description copied from class:DelegatingConnection
Returns a string representation of the metadata associated with the innermost delegate connection.- Overrides:
toString
in classDelegatingConnection<Connection>
-