Class PoolableConnectionFactory
- java.lang.Object
- 
- org.apache.tomcat.dbcp.dbcp2.PoolableConnectionFactory
 
- 
- All Implemented Interfaces:
- PooledObjectFactory<PoolableConnection>
 - Direct Known Subclasses:
- PoolableManagedConnectionFactory
 
 public class PoolableConnectionFactory extends java.lang.Object implements PooledObjectFactory<PoolableConnection> APooledObjectFactorythat createsPoolableConnections.- Since:
- 2.0
 
- 
- 
Constructor SummaryConstructors Constructor Description PoolableConnectionFactory(ConnectionFactory connFactory, javax.management.ObjectName dataSourceJmxObjectName)Creates a newPoolableConnectionFactory.
 - 
Method SummaryAll Methods Instance Methods Concrete Methods Deprecated Methods Modifier and Type Method Description voidactivateObject(PooledObject<PoolableConnection> p)Reinitializes an instance to be returned by the pool.voiddestroyObject(PooledObject<PoolableConnection> p)Destroys an instance no longer needed by the pool, using the default (NORMAL) DestroyMode.voiddestroyObject(PooledObject<PoolableConnection> p, DestroyMode mode)Destroys an instance no longer needed by the pool, using the provided DestroyMode.booleangetCacheState()Gets the cache state.ConnectionFactorygetConnectionFactory()Gets the connection factory.protected java.util.concurrent.atomic.AtomicLonggetConnectionIndex()java.util.Collection<java.lang.String>getConnectionInitSqls()javax.management.ObjectNamegetDataSourceJmxName()javax.management.ObjectNamegetDataSourceJmxObjectName()java.lang.BooleangetDefaultAutoCommit()java.lang.StringgetDefaultCatalog()java.lang.IntegergetDefaultQueryTimeout()Deprecated.java.time.DurationgetDefaultQueryTimeoutDuration()Gets the default query timeout Duration.java.lang.IntegergetDefaultQueryTimeoutSeconds()Deprecated.java.lang.BooleangetDefaultReadOnly()java.lang.StringgetDefaultSchema()intgetDefaultTransactionIsolation()java.util.Collection<java.lang.String>getDisconnectionSqlCodes()SQL_STATE codes considered to signal fatal conditions.java.time.DurationgetMaxConnDuration()Gets the Maximum connection duration.longgetMaxConnLifetimeMillis()Gets the Maximum connection lifetime in milliseconds.protected intgetMaxOpenPreparedStatements()ObjectPool<PoolableConnection>getPool()Returns theObjectPoolin whichConnections are pooled.booleangetPoolStatements()java.lang.StringgetValidationQuery()java.time.DurationgetValidationQueryTimeoutDuration()Gets the query timeout in seconds.intgetValidationQueryTimeoutSeconds()Deprecated.protected voidinitializeConnection(java.sql.Connection conn)booleanisAutoCommitOnReturn()booleanisEnableAutoCommitOnReturn()Deprecated.booleanisFastFailValidation()True means that validation will fail immediately for connections that have previously thrown SQLExceptions with SQL_STATE indicating fatal disconnection errors.booleanisRollbackOnReturn()PooledObject<PoolableConnection>makeObject()Creates an instance that can be served by the pool and wrap it in aPooledObjectto be managed by the pool.voidpassivateObject(PooledObject<PoolableConnection> p)Uninitializes an instance to be returned to the idle object pool.voidsetAutoCommitOnReturn(boolean autoCommitOnReturn)voidsetCacheState(boolean cacheState)voidsetClearStatementPoolOnReturn(boolean clearStatementPoolOnReturn)Sets whether the pool of statements (which was enabled withsetPoolStatements(boolean)) should be cleared when the connection is returned to its pool.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 defaultQueryTimeoutSeconds)Deprecated.voidsetDefaultQueryTimeout(java.time.Duration defaultQueryTimeoutDuration)Sets the query timeout Duration.voidsetDefaultReadOnly(java.lang.Boolean defaultReadOnly)Sets the default "read only" setting for borrowedConnectionsvoidsetDefaultSchema(java.lang.String defaultSchema)Sets the default "schema" setting for borrowedConnectionsvoidsetDefaultTransactionIsolation(int defaultTransactionIsolation)Sets the default "Transaction Isolation" setting for borrowedConnectionsvoidsetDisconnectionSqlCodes(java.util.Collection<java.lang.String> disconnectionSqlCodes)voidsetEnableAutoCommitOnReturn(boolean autoCommitOnReturn)Deprecated.voidsetFastFailValidation(boolean fastFailValidation)voidsetMaxConn(java.time.Duration maxConnDuration)Sets the maximum lifetime in milliseconds of a connection after which the connection will always fail activation, passivation and validation.voidsetMaxConnLifetimeMillis(long maxConnLifetimeMillis)Deprecated.UsesetMaxConn(Duration).voidsetMaxOpenPreparedStatements(int maxOpenPreparedStatements)Sets the maximum number of open prepared statements.voidsetMaxOpenPrepatedStatements(int maxOpenPreparedStatements)Deprecated.voidsetPool(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 validationQueryTimeoutSeconds)Deprecated.voidsetValidationQueryTimeout(java.time.Duration validationQueryTimeoutDuration)Sets the validation query timeout, the amount of time, that connection validation will wait for a response from the database when executing a validation query.voidvalidateConnection(PoolableConnection conn)Validates the given connection if it is open.booleanvalidateObject(PooledObject<PoolableConnection> p)Ensures that the instance is safe to be returned by the pool.
 
- 
- 
- 
Constructor Detail- 
PoolableConnectionFactorypublic PoolableConnectionFactory(ConnectionFactory connFactory, javax.management.ObjectName dataSourceJmxObjectName) Creates a newPoolableConnectionFactory.- Parameters:
- connFactory- the- ConnectionFactoryfrom which to obtain base- Connections
- dataSourceJmxObjectName- The JMX object name, may be null.
 
 
- 
 - 
Method Detail- 
activateObjectpublic void activateObject(PooledObject<PoolableConnection> p) throws java.sql.SQLException Description copied from interface:PooledObjectFactoryReinitializes an instance to be returned by the pool.- Specified by:
- activateObjectin interface- PooledObjectFactory<PoolableConnection>
- Parameters:
- p- a- PooledObjectwrapping the instance to be activated
- Throws:
- java.sql.SQLException
- See Also:
- PooledObjectFactory.destroyObject(org.apache.tomcat.dbcp.pool2.PooledObject<T>)
 
 - 
destroyObjectpublic void destroyObject(PooledObject<PoolableConnection> p) throws java.sql.SQLException Description copied from interface:PooledObjectFactoryDestroys an instance no longer needed by the pool, using the default (NORMAL) DestroyMode.It is important for implementations of this method to be aware that there is no guarantee about what state objwill be in and the implementation should be prepared to handle unexpected errors.Also, an implementation must take in to consideration that instances lost to the garbage collector may never be destroyed. - Specified by:
- destroyObjectin interface- PooledObjectFactory<PoolableConnection>
- Parameters:
- p- a- PooledObjectwrapping the instance to be destroyed
- Throws:
- java.sql.SQLException
- See Also:
- PooledObjectFactory.validateObject(org.apache.tomcat.dbcp.pool2.PooledObject<T>),- ObjectPool.invalidateObject(T)
 
 - 
destroyObjectpublic void destroyObject(PooledObject<PoolableConnection> p, DestroyMode mode) throws java.sql.SQLException Description copied from interface:PooledObjectFactoryDestroys an instance no longer needed by the pool, using the provided DestroyMode.- Specified by:
- destroyObjectin interface- PooledObjectFactory<PoolableConnection>
- Parameters:
- p- a- PooledObjectwrapping the instance to be destroyed
- mode- DestroyMode providing context to the factory
- Throws:
- java.sql.SQLException
- Since:
- 2.9.0
- See Also:
- PooledObjectFactory.validateObject(org.apache.tomcat.dbcp.pool2.PooledObject<T>),- ObjectPool.invalidateObject(T),- PooledObjectFactory.destroyObject(PooledObject),- DestroyMode
 
 - 
getCacheStatepublic boolean getCacheState() Gets the cache state.- Returns:
- The cache state.
- Since:
- 2.6.0.
 
 - 
getConnectionFactorypublic ConnectionFactory getConnectionFactory() Gets the connection factory.- Returns:
- The connection factory.
- Since:
- 2.6.0.
 
 - 
getConnectionIndexprotected java.util.concurrent.atomic.AtomicLong getConnectionIndex() 
 - 
getConnectionInitSqlspublic java.util.Collection<java.lang.String> getConnectionInitSqls() - Returns:
- The collection of initialization SQL statements.
- Since:
- 2.6.0
 
 - 
getDataSourceJmxNamepublic javax.management.ObjectName getDataSourceJmxName() - Returns:
- The data source JMX ObjectName
- Since:
- 2.6.0.
 
 - 
getDataSourceJmxObjectNamepublic javax.management.ObjectName getDataSourceJmxObjectName() - Returns:
- The data source JMS ObjectName.
- Since:
- 2.6.0
 
 - 
getDefaultAutoCommitpublic java.lang.Boolean getDefaultAutoCommit() - Returns:
- Default auto-commit value.
- Since:
- 2.6.0
 
 - 
getDefaultCatalogpublic java.lang.String getDefaultCatalog() - Returns:
- Default catalog.
- Since:
- 2.6.0
 
 - 
getDefaultQueryTimeout@Deprecated public java.lang.Integer getDefaultQueryTimeout() Deprecated.- Returns:
- Default query timeout in seconds.
 
 - 
getDefaultQueryTimeoutDurationpublic java.time.Duration getDefaultQueryTimeoutDuration() Gets the default query timeout Duration.- Returns:
- Default query timeout Duration.
- Since:
- 2.10.0
 
 - 
getDefaultQueryTimeoutSeconds@Deprecated public java.lang.Integer getDefaultQueryTimeoutSeconds() Deprecated.- Returns:
- Default query timeout in seconds.
- Since:
- 2.6.0
 
 - 
getDefaultReadOnlypublic java.lang.Boolean getDefaultReadOnly() - Returns:
- Default read-only-value.
- Since:
- 2.6.0
 
 - 
getDefaultSchemapublic java.lang.String getDefaultSchema() - Returns:
- Default schema.
- Since:
- 2.6.0
 
 - 
getDefaultTransactionIsolationpublic int getDefaultTransactionIsolation() - Returns:
- Default transaction isolation.
- Since:
- 2.6.0
 
 - 
getDisconnectionSqlCodespublic java.util.Collection<java.lang.String> getDisconnectionSqlCodes() SQL_STATE codes considered to signal fatal conditions.Overrides the defaults in Utils.getDisconnectionSqlCodes()(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
 
 - 
getMaxConnDurationpublic java.time.Duration getMaxConnDuration() Gets the Maximum connection duration.- Returns:
- Maximum connection duration.
- Since:
- 2.10.0
 
 - 
getMaxConnLifetimeMillispublic long getMaxConnLifetimeMillis() Gets the Maximum connection lifetime in milliseconds.- Returns:
- Maximum connection lifetime in milliseconds.
- Since:
- 2.6.0
 
 - 
getMaxOpenPreparedStatementsprotected int getMaxOpenPreparedStatements() 
 - 
getPoolpublic ObjectPool<PoolableConnection> getPool() Returns theObjectPoolin whichConnections are pooled.- Returns:
- the connection pool
 
 - 
getPoolStatementspublic boolean getPoolStatements() - Returns:
- Whether to pool statements.
- Since:
- 2.6.0.
 
 - 
getValidationQuerypublic java.lang.String getValidationQuery() - Returns:
- Validation query.
- Since:
- 2.6.0
 
 - 
getValidationQueryTimeoutDurationpublic java.time.Duration getValidationQueryTimeoutDuration() Gets the query timeout in seconds.- Returns:
- Validation query timeout in seconds.
- Since:
- 2.10.0
 
 - 
getValidationQueryTimeoutSeconds@Deprecated public int getValidationQueryTimeoutSeconds() Deprecated.Gets the query timeout in seconds.- Returns:
- Validation query timeout in seconds.
- Since:
- 2.6.0
 
 - 
initializeConnectionprotected void initializeConnection(java.sql.Connection conn) throws java.sql.SQLException- Throws:
- java.sql.SQLException
 
 - 
isAutoCommitOnReturnpublic boolean isAutoCommitOnReturn() - Returns:
- Whether to auto-commit on return.
- Since:
- 2.6.0
 
 - 
isEnableAutoCommitOnReturn@Deprecated public boolean isEnableAutoCommitOnReturn() Deprecated.- Returns:
- Whether to auto-commit on return.
 
 - 
isFastFailValidationpublic 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, 2.5.0 Defaults to true, previous versions defaulted to false.
- See Also:
- setDisconnectionSqlCodes(Collection)
 
 - 
isRollbackOnReturnpublic boolean isRollbackOnReturn() - Returns:
- Whether to rollback on return.
 
 - 
makeObjectpublic PooledObject<PoolableConnection> makeObject() throws java.sql.SQLException Description copied from interface:PooledObjectFactoryCreates an instance that can be served by the pool and wrap it in aPooledObjectto be managed by the pool.- Specified by:
- makeObjectin interface- PooledObjectFactory<PoolableConnection>
- Returns:
- a PooledObjectwrapping an instance that can be served by the pool
- Throws:
- java.sql.SQLException
 
 - 
passivateObjectpublic void passivateObject(PooledObject<PoolableConnection> p) throws java.sql.SQLException Description copied from interface:PooledObjectFactoryUninitializes an instance to be returned to the idle object pool.- Specified by:
- passivateObjectin interface- PooledObjectFactory<PoolableConnection>
- Parameters:
- p- a- PooledObjectwrapping the instance to be passivated
- Throws:
- java.sql.SQLException
- See Also:
- PooledObjectFactory.destroyObject(org.apache.tomcat.dbcp.pool2.PooledObject<T>)
 
 - 
setAutoCommitOnReturnpublic void setAutoCommitOnReturn(boolean autoCommitOnReturn) 
 - 
setCacheStatepublic void setCacheState(boolean cacheState) 
 - 
setClearStatementPoolOnReturnpublic void setClearStatementPoolOnReturn(boolean clearStatementPoolOnReturn) Sets whether the pool of statements (which was enabled withsetPoolStatements(boolean)) should be cleared when the connection is returned to its pool. Default is false.- Parameters:
- clearStatementPoolOnReturn- clear or not
- Since:
- 2.8.0
 
 - 
setConnectionInitSqlpublic 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 initialize- Connections.
 
 - 
setDefaultAutoCommitpublic void setDefaultAutoCommit(java.lang.Boolean defaultAutoCommit) Sets the default "auto commit" setting for borrowedConnections- Parameters:
- defaultAutoCommit- the default "auto commit" setting for borrowed- Connections
 
 - 
setDefaultCatalogpublic void setDefaultCatalog(java.lang.String defaultCatalog) Sets the default "catalog" setting for borrowedConnections- Parameters:
- defaultCatalog- the default "catalog" setting for borrowed- Connections
 
 - 
setDefaultQueryTimeoutpublic void setDefaultQueryTimeout(java.time.Duration defaultQueryTimeoutDuration) Sets the query timeout Duration.- Parameters:
- defaultQueryTimeoutDuration- the query timeout Duration.
- Since:
- 2.10.0
 
 - 
setDefaultQueryTimeout@Deprecated public void setDefaultQueryTimeout(java.lang.Integer defaultQueryTimeoutSeconds) Deprecated.Sets the query timeout in seconds.- Parameters:
- defaultQueryTimeoutSeconds- the query timeout in seconds.
 
 - 
setDefaultReadOnlypublic void setDefaultReadOnly(java.lang.Boolean defaultReadOnly) Sets the default "read only" setting for borrowedConnections- Parameters:
- defaultReadOnly- the default "read only" setting for borrowed- Connections
 
 - 
setDefaultSchemapublic void setDefaultSchema(java.lang.String defaultSchema) Sets the default "schema" setting for borrowedConnections- Parameters:
- defaultSchema- the default "schema" setting for borrowed- Connections
- Since:
- 2.5.0
 
 - 
setDefaultTransactionIsolationpublic void setDefaultTransactionIsolation(int defaultTransactionIsolation) Sets the default "Transaction Isolation" setting for borrowedConnections- Parameters:
- defaultTransactionIsolation- the default "Transaction Isolation" setting for returned- Connections
 
 - 
setDisconnectionSqlCodespublic void setDisconnectionSqlCodes(java.util.Collection<java.lang.String> disconnectionSqlCodes) - Parameters:
- disconnectionSqlCodes- The disconnection SQL codes.
- Since:
- 2.1
- See Also:
- getDisconnectionSqlCodes()
 
 - 
setEnableAutoCommitOnReturn@Deprecated public void setEnableAutoCommitOnReturn(boolean autoCommitOnReturn) Deprecated.- Parameters:
- autoCommitOnReturn- Whether to auto-commit on return.
 
 - 
setFastFailValidationpublic void setFastFailValidation(boolean fastFailValidation) - Parameters:
- fastFailValidation- true means connections created by this factory will fast fail validation
- Since:
- 2.1
- See Also:
- isFastFailValidation()
 
 - 
setMaxConnpublic void setMaxConn(java.time.Duration maxConnDuration) 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.- Parameters:
- maxConnDuration- The maximum lifetime in milliseconds.
- Since:
- 2.10.0
 
 - 
setMaxConnLifetimeMillis@Deprecated public void setMaxConnLifetimeMillis(long maxConnLifetimeMillis) Deprecated.UsesetMaxConn(Duration).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.- Parameters:
- maxConnLifetimeMillis- The maximum lifetime in milliseconds.
 
 - 
setMaxOpenPreparedStatementspublic void setMaxOpenPreparedStatements(int maxOpenPreparedStatements) Sets the maximum number of open prepared statements.- Parameters:
- maxOpenPreparedStatements- The maximum number of open prepared statements.
 
 - 
setMaxOpenPrepatedStatements@Deprecated public void setMaxOpenPrepatedStatements(int maxOpenPreparedStatements) Deprecated.Deprecated due to typo in method name.- Parameters:
- maxOpenPreparedStatements- The maximum number of open prepared statements.
 
 - 
setPoolpublic void setPool(ObjectPool<PoolableConnection> pool) Sets theObjectPoolin which to poolConnections.- Parameters:
- pool- the- ObjectPoolin which to pool those- Connections
 
 - 
setPoolStatementspublic void setPoolStatements(boolean poolStatements) 
 - 
setRollbackOnReturnpublic void setRollbackOnReturn(boolean rollbackOnReturn) 
 - 
setValidationQuerypublic 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 to- validate- Connections.
 
 - 
setValidationQueryTimeoutpublic void setValidationQueryTimeout(java.time.Duration validationQueryTimeoutDuration) Sets the validation query timeout, the amount of time, 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:
- validationQueryTimeoutDuration- new validation query timeout duration.
- Since:
- 2.10.0
 
 - 
setValidationQueryTimeout@Deprecated public void setValidationQueryTimeout(int validationQueryTimeoutSeconds) Deprecated.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:
- validationQueryTimeoutSeconds- new validation query timeout value in seconds
 
 - 
validateConnectionpublic void validateConnection(PoolableConnection conn) throws java.sql.SQLException Validates the given connection if it is open.- Parameters:
- conn- the connection to validate.
- Throws:
- java.sql.SQLException- if the connection is closed or validate fails.
 
 - 
validateObjectpublic boolean validateObject(PooledObject<PoolableConnection> p) Description copied from interface:PooledObjectFactoryEnsures that the instance is safe to be returned by the pool.- Specified by:
- validateObjectin interface- PooledObjectFactory<PoolableConnection>
- Parameters:
- p- a- PooledObjectwrapping the instance to be validated
- Returns:
- falseif- objis not valid and should be dropped from the pool,- trueotherwise.
 
 
- 
 
-