Package org.apache.commons.dbcp
Class PoolingConnection
- java.lang.Object
-
- org.apache.commons.dbcp.AbandonedTrace
-
- org.apache.commons.dbcp.DelegatingConnection
-
- org.apache.commons.dbcp.PoolingConnection
-
- All Implemented Interfaces:
AutoCloseable,Connection,Wrapper,org.apache.commons.pool.KeyedPoolableObjectFactory
public class PoolingConnection extends DelegatingConnection implements Connection, org.apache.commons.pool.KeyedPoolableObjectFactory
ADelegatingConnectionthat poolsPreparedStatements.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.)- Version:
- $Revision: 885261 $ $Date: 2009-11-29 15:07:02 -0500 (Sun, 29 Nov 2009) $
- Author:
- Rodney Waldhoff, Dirk Verbeeck
- See Also:
PoolablePreparedStatement
-
-
Field Summary
Fields Modifier and Type Field Description protected org.apache.commons.pool.KeyedObjectPool_pstmtPoolPool ofPreparedStatements.-
Fields inherited from class org.apache.commons.dbcp.DelegatingConnection
_closed, _conn
-
Fields inherited from interface java.sql.Connection
TRANSACTION_NONE, TRANSACTION_READ_COMMITTED, TRANSACTION_READ_UNCOMMITTED, TRANSACTION_REPEATABLE_READ, TRANSACTION_SERIALIZABLE
-
-
Constructor Summary
Constructors Constructor Description PoolingConnection(Connection c)Constructor.PoolingConnection(Connection c, org.apache.commons.pool.KeyedObjectPool pool)Constructor.
-
Method Summary
All Methods Instance Methods Concrete Methods Modifier and Type Method Description voidabort(Executor executor)voidactivateObject(Object key, Object obj)KeyedPoolableObjectFactorymethod for activating pooled statements.voidclose()Close and free allPreparedStatements orCallableStatementfrom the pool, and close the underlying connection.protected ObjectcreateKey(String sql)Create a PStmtKey for the given arguments.protected ObjectcreateKey(String sql, byte stmtType)Create a PStmtKey for the given arguments.protected ObjectcreateKey(String sql, int resultSetType, int resultSetConcurrency)Create a PStmtKey for the given arguments.protected ObjectcreateKey(String sql, int resultSetType, int resultSetConcurrency, byte stmtType)Create a PStmtKey for the given arguments.voiddestroyObject(Object key, Object obj)KeyedPoolableObjectFactorymethod for destroying PoolablePreparedStatements and PoolableCallableStatements.intgetNetworkTimeout()StringgetSchema()ObjectmakeObject(Object obj)KeyedPoolableObjectFactorymethod for creatingPoolablePreparedStatements orPoolableCallableStatements.protected StringnormalizeSQL(String sql)Normalize the given SQL statement, producing a cannonical form that is semantically equivalent to the original.voidpassivateObject(Object key, Object obj)CallableStatementprepareCall(String sql)Create or obtain aCallableStatementfrom the pool.CallableStatementprepareCall(String sql, int resultSetType, int resultSetConcurrency)Create or obtain aCallableStatementfrom the pool.PreparedStatementprepareStatement(String sql)Create or obtain aPreparedStatementfrom the pool.PreparedStatementprepareStatement(String sql, int resultSetType, int resultSetConcurrency)Create or obtain aPreparedStatementfrom the pool.voidsetNetworkTimeout(Executor executor, int milliseconds)voidsetSchema(String schema)StringtoString()Returns a string representation of the metadata associated with the innnermost delegate connection.booleanvalidateObject(Object key, Object obj)KeyedPoolableObjectFactorymethod for validating pooled statements.-
Methods inherited from class org.apache.commons.dbcp.DelegatingConnection
activate, checkOpen, clearWarnings, commit, createArrayOf, createBlob, createClob, createNClob, createSQLXML, createStatement, createStatement, createStatement, createStruct, equals, getAutoCommit, getCatalog, getClientInfo, getClientInfo, getDelegate, getDelegateInternal, getHoldability, getInnermostDelegate, getInnermostDelegateInternal, getMetaData, getTransactionIsolation, getTypeMap, getWarnings, handleException, hashCode, innermostDelegateEquals, isClosed, isReadOnly, isValid, isWrapperFor, nativeSQL, passivate, prepareCall, prepareStatement, prepareStatement, prepareStatement, prepareStatement, releaseSavepoint, rollback, rollback, setAutoCommit, setCatalog, setClientInfo, setClientInfo, setDelegate, setHoldability, setReadOnly, setSavepoint, setSavepoint, setTransactionIsolation, setTypeMap, unwrap
-
Methods inherited from class org.apache.commons.dbcp.AbandonedTrace
addTrace, clearTrace, getConfig, getLastUsed, getTrace, printStackTrace, removeTrace, setLastUsed, setLastUsed, setStackTrace
-
Methods inherited from class java.lang.Object
clone, finalize, getClass, notify, notifyAll, wait, wait, wait
-
Methods inherited from interface java.sql.Connection
beginRequest, clearWarnings, commit, createArrayOf, createBlob, createClob, createNClob, createSQLXML, createStatement, createStatement, createStatement, createStruct, endRequest, getAutoCommit, getCatalog, getClientInfo, getClientInfo, getHoldability, getMetaData, getTransactionIsolation, getTypeMap, getWarnings, isClosed, isReadOnly, isValid, nativeSQL, prepareCall, prepareStatement, prepareStatement, prepareStatement, prepareStatement, releaseSavepoint, rollback, rollback, setAutoCommit, setCatalog, setClientInfo, setClientInfo, setHoldability, setReadOnly, setSavepoint, setSavepoint, setShardingKey, setShardingKey, setShardingKeyIfValid, setShardingKeyIfValid, setTransactionIsolation, setTypeMap
-
Methods inherited from interface java.sql.Wrapper
isWrapperFor, unwrap
-
-
-
-
Field Detail
-
_pstmtPool
protected org.apache.commons.pool.KeyedObjectPool _pstmtPool
Pool ofPreparedStatements. andCallableStatements
-
-
Constructor Detail
-
PoolingConnection
public PoolingConnection(Connection c)
Constructor.- Parameters:
c- the underlyingConnection.
-
PoolingConnection
public PoolingConnection(Connection c, org.apache.commons.pool.KeyedObjectPool pool)
Constructor.- Parameters:
c- the underlyingConnection.pool-KeyedObjectPoolofPreparedStatements andCallableStatements.
-
-
Method Detail
-
close
public void close() throws SQLExceptionClose and free allPreparedStatements orCallableStatementfrom the pool, and close the underlying connection.- Specified by:
closein interfaceAutoCloseable- Specified by:
closein interfaceConnection- Overrides:
closein classDelegatingConnection- Throws:
SQLException
-
prepareStatement
public PreparedStatement prepareStatement(String sql) throws SQLException
Create or obtain aPreparedStatementfrom the pool.- Specified by:
prepareStatementin interfaceConnection- Overrides:
prepareStatementin classDelegatingConnection- Parameters:
sql- the sql string used to define the PreparedStatement- Returns:
- a
PoolablePreparedStatement - Throws:
SQLException
-
prepareStatement
public PreparedStatement prepareStatement(String sql, int resultSetType, int resultSetConcurrency) throws SQLException
Create or obtain aPreparedStatementfrom the pool.- Specified by:
prepareStatementin interfaceConnection- Overrides:
prepareStatementin classDelegatingConnection- Parameters:
sql- the sql string used to define the PreparedStatementresultSetType- result set typeresultSetConcurrency- result set concurrency- Returns:
- a
PoolablePreparedStatement - Throws:
SQLException
-
prepareCall
public CallableStatement prepareCall(String sql) throws SQLException
Create or obtain aCallableStatementfrom the pool.- Specified by:
prepareCallin interfaceConnection- Overrides:
prepareCallin classDelegatingConnection- Parameters:
sql- the sql string used to define the CallableStatement- Returns:
- a
PoolableCallableStatement - Throws:
SQLException- Since:
- 1.3
-
prepareCall
public CallableStatement prepareCall(String sql, int resultSetType, int resultSetConcurrency) throws SQLException
Create or obtain aCallableStatementfrom the pool.- Specified by:
prepareCallin interfaceConnection- Overrides:
prepareCallin classDelegatingConnection- Parameters:
sql- the sql string used to define the CallableStatementresultSetType- result set typeresultSetConcurrency- result set concurrency- Returns:
- a
PoolableCallableStatement - Throws:
SQLException- Since:
- 1.3
-
createKey
protected Object createKey(String sql, int resultSetType, int resultSetConcurrency)
Create a PStmtKey for the given arguments.- Parameters:
sql- the sql string used to define the statementresultSetType- result set typeresultSetConcurrency- result set concurrency
-
createKey
protected Object createKey(String sql, int resultSetType, int resultSetConcurrency, byte stmtType)
Create a PStmtKey for the given arguments.- Parameters:
sql- the sql string used to define the statementresultSetType- result set typeresultSetConcurrency- result set concurrencystmtType- statement type - eitherSTATEMENT_CALLABLESTMTorSTATEMENT_PREPAREDSTMT
-
createKey
protected Object createKey(String sql)
Create a PStmtKey for the given arguments.- Parameters:
sql- the sql string used to define the statement
-
createKey
protected Object createKey(String sql, byte stmtType)
Create a PStmtKey for the given arguments.- Parameters:
sql- the sql string used to define the statementstmtType- statement type - eitherSTATEMENT_CALLABLESTMTorSTATEMENT_PREPAREDSTMT
-
normalizeSQL
protected String normalizeSQL(String sql)
Normalize the given SQL statement, producing a cannonical form that is semantically equivalent to the original.
-
makeObject
public Object makeObject(Object obj) throws Exception
KeyedPoolableObjectFactorymethod for creatingPoolablePreparedStatements orPoolableCallableStatements. ThestmtTypefield in the key determines whether a PoolablePreparedStatement or PoolableCallableStatement is created.- Specified by:
makeObjectin interfaceorg.apache.commons.pool.KeyedPoolableObjectFactory- Parameters:
obj- the key for thePreparedStatementto be created- Throws:
Exception- See Also:
createKey(String, int, int, byte)
-
destroyObject
public void destroyObject(Object key, Object obj) throws Exception
KeyedPoolableObjectFactorymethod for destroying PoolablePreparedStatements and PoolableCallableStatements. Closes the underlying statement.- Specified by:
destroyObjectin interfaceorg.apache.commons.pool.KeyedPoolableObjectFactory- Parameters:
key- ignoredobj- the pooled statement to be destroyed.- Throws:
Exception
-
validateObject
public boolean validateObject(Object key, Object obj)
KeyedPoolableObjectFactorymethod for validating pooled statements. Currently always returns true.- Specified by:
validateObjectin interfaceorg.apache.commons.pool.KeyedPoolableObjectFactory- Parameters:
key- ignoredobj- ignored- Returns:
- true
-
activateObject
public void activateObject(Object key, Object obj) throws Exception
KeyedPoolableObjectFactorymethod for activating pooled statements.- Specified by:
activateObjectin interfaceorg.apache.commons.pool.KeyedPoolableObjectFactory- Parameters:
key- ignoredobj- pooled statement to be activated- Throws:
Exception
-
passivateObject
public void passivateObject(Object key, Object obj) throws Exception
KeyedPoolableObjectFactorymethod for passivatingPreparedStatements orCallableStatements. InvokesPreparedStatement.clearParameters().- Specified by:
passivateObjectin interfaceorg.apache.commons.pool.KeyedPoolableObjectFactory- Parameters:
key- ignoredobj- aPreparedStatement- Throws:
Exception
-
toString
public String toString()
Description copied from class:DelegatingConnectionReturns a string representation of the metadata associated with the innnermost delegate connection.- Overrides:
toStringin classDelegatingConnection
-
getNetworkTimeout
public int getNetworkTimeout() throws SQLException- Specified by:
getNetworkTimeoutin interfaceConnection- Overrides:
getNetworkTimeoutin classDelegatingConnection- Throws:
SQLException
-
setNetworkTimeout
public void setNetworkTimeout(Executor executor, int milliseconds) throws SQLException
- Specified by:
setNetworkTimeoutin interfaceConnection- Overrides:
setNetworkTimeoutin classDelegatingConnection- Throws:
SQLException
-
abort
public void abort(Executor executor) throws SQLException
- Specified by:
abortin interfaceConnection- Overrides:
abortin classDelegatingConnection- Throws:
SQLException
-
getSchema
public String getSchema() throws SQLException
- Specified by:
getSchemain interfaceConnection- Overrides:
getSchemain classDelegatingConnection- Throws:
SQLException
-
setSchema
public void setSchema(String schema) throws SQLException
- Specified by:
setSchemain interfaceConnection- Overrides:
setSchemain classDelegatingConnection- Throws:
SQLException
-
-