Package org.apache.commons.dbcp2
Class PoolingConnection
- java.lang.Object
-
- org.apache.commons.dbcp2.AbandonedTrace
-
- org.apache.commons.dbcp2.DelegatingConnection<java.sql.Connection>
-
- org.apache.commons.dbcp2.PoolingConnection
-
- All Implemented Interfaces:
java.lang.AutoCloseable
,java.sql.Connection
,java.sql.Wrapper
,org.apache.commons.pool2.KeyedPooledObjectFactory<PStmtKey,DelegatingPreparedStatement>
,org.apache.commons.pool2.TrackedUse
public class PoolingConnection extends DelegatingConnection<java.sql.Connection> implements org.apache.commons.pool2.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
- Version:
- $Id: PoolingConnection.java 1658644 2015-02-10 08:59:07Z tn $
- Author:
- Rodney Waldhoff, Dirk Verbeeck
- See Also:
PoolablePreparedStatement
-
-
Constructor Summary
Constructors Constructor Description PoolingConnection(java.sql.Connection c)
Constructor.
-
Method Summary
All Methods Instance Methods Concrete Methods Modifier and Type Method Description void
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)
KeyedPooledObjectFactory
method for passivatingPreparedStatement
s orCallableStatement
s.java.sql.CallableStatement
prepareCall(java.lang.String sql)
Create or obtain aCallableStatement
from the pool.java.sql.CallableStatement
prepareCall(java.lang.String sql, int resultSetType, int resultSetConcurrency)
Create or obtain aCallableStatement
from the pool.java.sql.PreparedStatement
prepareStatement(java.lang.String sql)
Create or obtain aPreparedStatement
from the pool.java.sql.PreparedStatement
prepareStatement(java.lang.String sql, int autoGeneratedKeys)
java.sql.PreparedStatement
prepareStatement(java.lang.String sql, int resultSetType, int resultSetConcurrency)
Create or obtain aPreparedStatement
from the pool.void
setStatementPool(org.apache.commons.pool2.KeyedObjectPool<PStmtKey,DelegatingPreparedStatement> pool)
java.lang.String
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
-
-
-
-
Method Detail
-
setStatementPool
public void setStatementPool(org.apache.commons.pool2.KeyedObjectPool<PStmtKey,DelegatingPreparedStatement> pool)
-
close
public void close() throws java.sql.SQLException
Close and free 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
-
prepareStatement
public java.sql.PreparedStatement prepareStatement(java.lang.String sql) throws java.sql.SQLException
Create or obtain 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
-
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
Create or obtain 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
-
prepareCall
public java.sql.CallableStatement prepareCall(java.lang.String sql) throws java.sql.SQLException
Create or obtain 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
-
prepareCall
public java.sql.CallableStatement prepareCall(java.lang.String sql, int resultSetType, int resultSetConcurrency) throws java.sql.SQLException
Create or obtain 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
-
makeObject
public org.apache.commons.pool2.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 interfaceorg.apache.commons.pool2.KeyedPooledObjectFactory<PStmtKey,DelegatingPreparedStatement>
- Parameters:
key
- the key for thePreparedStatement
to be created- Throws:
java.lang.Exception
- See Also:
createKey(String, int, int, StatementType)
-
destroyObject
public void destroyObject(PStmtKey key, org.apache.commons.pool2.PooledObject<DelegatingPreparedStatement> p) throws java.lang.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:
java.lang.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 java.lang.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:
java.lang.Exception
-
passivateObject
public void passivateObject(PStmtKey key, org.apache.commons.pool2.PooledObject<DelegatingPreparedStatement> p) throws java.lang.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:
java.lang.Exception
-
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>
-
-