Package org.apache.commons.dbcp2
Class PoolableConnectionFactory
- java.lang.Object
-
- org.apache.commons.dbcp2.PoolableConnectionFactory
-
- All Implemented Interfaces:
org.apache.commons.pool2.PooledObjectFactory<PoolableConnection>
- Direct Known Subclasses:
PoolableManagedConnectionFactory
public class PoolableConnectionFactory extends java.lang.Object implements org.apache.commons.pool2.PooledObjectFactory<PoolableConnection>
APooledObjectFactorythat createsPoolableConnections.- Since:
- 2.0
- Version:
- $Id: PoolableConnectionFactory.java 1659452 2015-02-13 04:00:39Z psteitz $
- Author:
- Rodney Waldhoff, Glenn L. Nielsen, James House, Dirk Verbeeck
-
-
Constructor Summary
Constructors Constructor Description PoolableConnectionFactory(ConnectionFactory connFactory, javax.management.ObjectName dataSourceJmxName)Create a newPoolableConnectionFactory.
-
Method Summary
All Methods Instance Methods Concrete Methods Modifier and Type Method Description voidactivateObject(org.apache.commons.pool2.PooledObject<PoolableConnection> p)voiddestroyObject(org.apache.commons.pool2.PooledObject<PoolableConnection> p)java.lang.IntegergetDefaultQueryTimeout()java.util.Collection<java.lang.String>getDisconnectionSqlCodes()SQL_STATE codes considered to signal fatal conditions.org.apache.commons.pool2.ObjectPool<PoolableConnection>getPool()Returns theObjectPoolin whichConnections are pooled.booleanisEnableAutoCommitOnReturn()booleanisFastFailValidation()True means that validation will fail immediately for connections that have previously thrown SQLExceptions with SQL_STATE indicating fatal disconnection errors.booleanisRollbackOnReturn()org.apache.commons.pool2.PooledObject<PoolableConnection>makeObject()voidpassivateObject(org.apache.commons.pool2.PooledObject<PoolableConnection> p)voidsetCacheState(boolean cacheState)voidsetConnectionInitSql(java.util.Collection<java.lang.String> connectionInitSqls)Sets the SQL statements I use to initialize newly createdConnections.voidsetDefaultAutoCommit(java.lang.Boolean defaultAutoCommit)Sets the default "auto commit" setting for borrowedConnectionsvoidsetDefaultCatalog(java.lang.String defaultCatalog)Sets the default "catalog" setting for borrowedConnectionsvoidsetDefaultQueryTimeout(java.lang.Integer defaultQueryTimeout)voidsetDefaultReadOnly(java.lang.Boolean defaultReadOnly)Sets the default "read only" setting for borrowedConnectionsvoidsetDefaultTransactionIsolation(int defaultTransactionIsolation)Sets the default "Transaction Isolation" setting for borrowedConnectionsvoidsetDisconnectionSqlCodes(java.util.Collection<java.lang.String> disconnectionSqlCodes)voidsetEnableAutoCommitOnReturn(boolean enableAutoCommitOnReturn)voidsetFastFailValidation(boolean fastFailValidation)voidsetMaxConnLifetimeMillis(long maxConnLifetimeMillis)Sets the maximum lifetime in milliseconds of a connection after which the connection will always fail activation, passivation and validation.voidsetMaxOpenPrepatedStatements(int maxOpenPreparedStatements)voidsetPool(org.apache.commons.pool2.ObjectPool<PoolableConnection> pool)Sets theObjectPoolin which to poolConnections.voidsetPoolStatements(boolean poolStatements)voidsetRollbackOnReturn(boolean rollbackOnReturn)voidsetValidationQuery(java.lang.String validationQuery)Sets the query I use tovalidateConnections.voidsetValidationQueryTimeout(int timeout)Sets the validation query timeout, the amount of time, in seconds, that connection validation will wait for a response from the database when executing a validation query.voidvalidateConnection(PoolableConnection conn)booleanvalidateObject(org.apache.commons.pool2.PooledObject<PoolableConnection> p)
-
-
-
Constructor Detail
-
PoolableConnectionFactory
public PoolableConnectionFactory(ConnectionFactory connFactory, javax.management.ObjectName dataSourceJmxName)
Create a newPoolableConnectionFactory.- Parameters:
connFactory- theConnectionFactoryfrom which to obtain baseConnections
-
-
Method Detail
-
setValidationQuery
public void setValidationQuery(java.lang.String validationQuery)
Sets the query I use tovalidateConnections. Should return at least one row. If not specified,Connection.isValid(int)will be used to validate connections.- Parameters:
validationQuery- a query to use tovalidateConnections.
-
setValidationQueryTimeout
public void setValidationQueryTimeout(int timeout)
Sets the validation query timeout, the amount of time, in seconds, that connection validation will wait for a response from the database when executing a validation query. Use a value less than or equal to 0 for no timeout.- Parameters:
timeout- new validation query timeout value in seconds
-
setConnectionInitSql
public void setConnectionInitSql(java.util.Collection<java.lang.String> connectionInitSqls)
Sets the SQL statements I use to initialize newly createdConnections. Usingnullturns off connection initialization.- Parameters:
connectionInitSqls- SQL statement to initializeConnections.
-
setPool
public void setPool(org.apache.commons.pool2.ObjectPool<PoolableConnection> pool)
Sets theObjectPoolin which to poolConnections.- Parameters:
pool- theObjectPoolin which to pool thoseConnections
-
getPool
public org.apache.commons.pool2.ObjectPool<PoolableConnection> getPool()
Returns theObjectPoolin whichConnections are pooled.- Returns:
- the connection pool
-
setDefaultReadOnly
public void setDefaultReadOnly(java.lang.Boolean defaultReadOnly)
Sets the default "read only" setting for borrowedConnections- Parameters:
defaultReadOnly- the default "read only" setting for borrowedConnections
-
setDefaultAutoCommit
public void setDefaultAutoCommit(java.lang.Boolean defaultAutoCommit)
Sets the default "auto commit" setting for borrowedConnections- Parameters:
defaultAutoCommit- the default "auto commit" setting for borrowedConnections
-
setDefaultTransactionIsolation
public void setDefaultTransactionIsolation(int defaultTransactionIsolation)
Sets the default "Transaction Isolation" setting for borrowedConnections- Parameters:
defaultTransactionIsolation- the default "Transaction Isolation" setting for returnedConnections
-
setDefaultCatalog
public void setDefaultCatalog(java.lang.String defaultCatalog)
Sets the default "catalog" setting for borrowedConnections- Parameters:
defaultCatalog- the default "catalog" setting for borrowedConnections
-
setCacheState
public void setCacheState(boolean cacheState)
-
setPoolStatements
public void setPoolStatements(boolean poolStatements)
-
setMaxOpenPrepatedStatements
public void setMaxOpenPrepatedStatements(int maxOpenPreparedStatements)
-
setMaxConnLifetimeMillis
public void setMaxConnLifetimeMillis(long maxConnLifetimeMillis)
Sets the maximum lifetime in milliseconds of a connection after which the connection will always fail activation, passivation and validation. A value of zero or less indicates an infinite lifetime. The default value is -1.
-
isEnableAutoCommitOnReturn
public boolean isEnableAutoCommitOnReturn()
-
setEnableAutoCommitOnReturn
public void setEnableAutoCommitOnReturn(boolean enableAutoCommitOnReturn)
-
isRollbackOnReturn
public boolean isRollbackOnReturn()
-
setRollbackOnReturn
public void setRollbackOnReturn(boolean rollbackOnReturn)
-
getDefaultQueryTimeout
public java.lang.Integer getDefaultQueryTimeout()
-
setDefaultQueryTimeout
public void setDefaultQueryTimeout(java.lang.Integer defaultQueryTimeout)
-
getDisconnectionSqlCodes
public java.util.Collection<java.lang.String> getDisconnectionSqlCodes()
SQL_STATE codes considered to signal fatal conditions.Overrides the defaults in
Utils.DISCONNECTION_SQL_CODES(plus anything starting withUtils.DISCONNECTION_SQL_CODE_PREFIX). If this property is non-null andisFastFailValidation()istrue, whenever connections created by this factory generate exceptions with SQL_STATE codes in this list, they will be marked as "fatally disconnected" and subsequent validations will fail fast (no attempt at isValid or validation query).If
isFastFailValidation()isfalsesetting this property has no effect.- Returns:
- SQL_STATE codes overriding defaults
- Since:
- 2.1
-
setDisconnectionSqlCodes
public void setDisconnectionSqlCodes(java.util.Collection<java.lang.String> disconnectionSqlCodes)
- Parameters:
disconnectionSqlCodes-- Since:
- 2.1
- See Also:
getDisconnectionSqlCodes()
-
isFastFailValidation
public boolean isFastFailValidation()
True means that validation will fail immediately for connections that have previously thrown SQLExceptions with SQL_STATE indicating fatal disconnection errors.- Returns:
- true if connections created by this factory will fast fail validation.
- Since:
- 2.1
- See Also:
setDisconnectionSqlCodes(Collection)
-
setFastFailValidation
public void setFastFailValidation(boolean fastFailValidation)
- Parameters:
fastFailValidation- true means connections created by this factory will fast fail validation- Since:
- 2.1
- See Also:
isFastFailValidation()
-
makeObject
public org.apache.commons.pool2.PooledObject<PoolableConnection> makeObject() throws java.lang.Exception
- Specified by:
makeObjectin interfaceorg.apache.commons.pool2.PooledObjectFactory<PoolableConnection>- Throws:
java.lang.Exception
-
destroyObject
public void destroyObject(org.apache.commons.pool2.PooledObject<PoolableConnection> p) throws java.lang.Exception
- Specified by:
destroyObjectin interfaceorg.apache.commons.pool2.PooledObjectFactory<PoolableConnection>- Throws:
java.lang.Exception
-
validateObject
public boolean validateObject(org.apache.commons.pool2.PooledObject<PoolableConnection> p)
- Specified by:
validateObjectin interfaceorg.apache.commons.pool2.PooledObjectFactory<PoolableConnection>
-
validateConnection
public void validateConnection(PoolableConnection conn) throws java.sql.SQLException
- Throws:
java.sql.SQLException
-
passivateObject
public void passivateObject(org.apache.commons.pool2.PooledObject<PoolableConnection> p) throws java.lang.Exception
- Specified by:
passivateObjectin interfaceorg.apache.commons.pool2.PooledObjectFactory<PoolableConnection>- Throws:
java.lang.Exception
-
activateObject
public void activateObject(org.apache.commons.pool2.PooledObject<PoolableConnection> p) throws java.lang.Exception
- Specified by:
activateObjectin interfaceorg.apache.commons.pool2.PooledObjectFactory<PoolableConnection>- Throws:
java.lang.Exception
-
-