public class BasicDataSource extends java.lang.Object implements javax.sql.DataSource, BasicDataSourceMXBean, javax.management.MBeanRegistration, java.lang.AutoCloseable
Basic implementation of javax.sql.DataSource
that is configured via JavaBeans properties. This is not
the only way to combine the commons-dbcp2 and commons-pool2 packages, but provides a "one stop
shopping" solution for basic requirements.
Constructor and Description |
---|
BasicDataSource() |
Modifier and Type | Method and Description |
---|---|
void |
addConnectionProperty(java.lang.String name,
java.lang.String value)
Adds a custom connection property to the set that will be passed to our JDBC driver.
|
void |
close()
Closes and releases all idle connections that are currently stored in the connection pool associated with this
data source.
|
protected ConnectionFactory |
createConnectionFactory()
Creates a JDBC connection factory for this datasource.
|
protected void |
createConnectionPool(PoolableConnectionFactory factory)
Creates a connection pool for this datasource.
|
protected javax.sql.DataSource |
createDataSource()
Creates (if necessary) and return the internal data source we are using to manage our connections.
|
protected javax.sql.DataSource |
createDataSourceInstance()
Creates the actual data source instance.
|
protected GenericObjectPool<PoolableConnection> |
createObjectPool(PoolableConnectionFactory factory,
GenericObjectPoolConfig<PoolableConnection> poolConfig,
AbandonedConfig abandonedConfig)
Creates an object pool used to provide pooling support for
JDBC connections . |
protected PoolableConnectionFactory |
createPoolableConnectionFactory(ConnectionFactory driverConnectionFactory)
Creates the PoolableConnectionFactory and attaches it to the connection pool.
|
java.io.PrintWriter |
getAbandonedLogWriter()
Gets the print writer used by this configuration to log information on abandoned objects.
|
boolean |
getAbandonedUsageTracking()
If the connection pool implements
UsageTracking , should the
connection pool record a stack trace every time a method is called on a pooled connection and retain the most
recent stack trace to aid debugging of abandoned connections? |
boolean |
getCacheState()
Returns the state caching flag.
|
java.sql.Connection |
getConnection()
Creates (if necessary) and return a connection to the database.
|
java.sql.Connection |
getConnection(java.lang.String user,
java.lang.String pass)
BasicDataSource does NOT support this method.
|
java.util.List<java.lang.String> |
getConnectionInitSqls()
Returns the list of SQL statements executed when a physical connection is first created.
|
java.lang.String[] |
getConnectionInitSqlsAsArray()
Provides the same data as
getConnectionInitSqls() but in an array so it is accessible via JMX. |
protected GenericObjectPool<PoolableConnection> |
getConnectionPool() |
java.lang.Boolean |
getDefaultAutoCommit()
Returns the default auto-commit property.
|
java.lang.String |
getDefaultCatalog()
Returns the default catalog.
|
java.lang.Integer |
getDefaultQueryTimeout()
Gets the default query timeout that will be used for
Statement s created from this
connection. |
java.lang.Boolean |
getDefaultReadOnly()
Returns the default readOnly property.
|
int |
getDefaultTransactionIsolation()
Returns the default transaction isolation state of returned connections.
|
java.util.Set<java.lang.String> |
getDisconnectionSqlCodes()
Returns the set of SQL_STATE codes considered to signal fatal conditions.
|
java.lang.String[] |
getDisconnectionSqlCodesAsArray()
Provides the same data as
getDisconnectionSqlCodes() but in an array so it is accessible via JMX. |
java.sql.Driver |
getDriver()
Returns the JDBC Driver that has been configured for use by this pool.
|
java.lang.ClassLoader |
getDriverClassLoader()
Returns the class loader specified for loading the JDBC driver.
|
java.lang.String |
getDriverClassName()
Returns the JDBC driver class name.
|
boolean |
getEnableAutoCommitOnReturn()
Returns the value of the flag that controls whether or not connections being returned to the pool will be checked
and configured with
Connection.setAutoCommit(true) if the auto commit
setting is false when the connection is returned. |
java.lang.String |
getEvictionPolicyClassName()
Gets the EvictionPolicy implementation in use with this connection pool.
|
boolean |
getFastFailValidation()
True means that validation will fail immediately for connections that have previously thrown SQLExceptions with
SQL_STATE indicating fatal disconnection errors.
|
int |
getInitialSize()
Returns the initial size of the connection pool.
|
java.lang.String |
getJmxName()
Returns the JMX name that has been requested for this DataSource.
|
boolean |
getLifo()
Returns the LIFO property.
|
boolean |
getLogAbandoned()
Flag to log stack traces for application code which abandoned a Statement or Connection.
|
boolean |
getLogExpiredConnections()
When
getMaxConnLifetimeMillis() is set to limit connection lifetime, this property determines whether or
not log messages are generated when the pool closes connections due to maximum lifetime exceeded. |
int |
getLoginTimeout()
BasicDataSource does NOT support this method.
|
java.io.PrintWriter |
getLogWriter()
Returns the log writer being used by this data source.
|
long |
getMaxConnLifetimeMillis()
Returns the maximum permitted lifetime of a connection in milliseconds.
|
int |
getMaxIdle()
Returns the maximum number of connections that can remain idle in the pool.
|
int |
getMaxOpenPreparedStatements()
Gets the value of the
maxOpenPreparedStatements property. |
int |
getMaxTotal()
Returns the maximum number of active connections that can be allocated at the same time.
|
long |
getMaxWaitMillis()
Returns the maximum number of milliseconds that the pool will wait for a connection to be returned before
throwing an exception.
|
long |
getMinEvictableIdleTimeMillis()
Returns the
minEvictableIdleTimeMillis property. |
int |
getMinIdle()
Returns the minimum number of idle connections in the pool.
|
int |
getNumActive()
[Read Only] The current number of active connections that have been allocated from this data source.
|
int |
getNumIdle()
[Read Only] The current number of idle connections that are waiting to be allocated from this data source.
|
int |
getNumTestsPerEvictionRun()
Returns the value of the
numTestsPerEvictionRun property. |
java.util.logging.Logger |
getParentLogger() |
java.lang.String |
getPassword()
Returns the password passed to the JDBC driver to establish connections.
|
protected javax.management.ObjectName |
getRegisteredJmxName() |
boolean |
getRemoveAbandonedOnBorrow()
Flag to remove abandoned connections if they exceed the removeAbandonedTimeout when borrowObject is invoked.
|
boolean |
getRemoveAbandonedOnMaintenance()
Flag to remove abandoned connections if they exceed the removeAbandonedTimeout during pool maintenance.
|
int |
getRemoveAbandonedTimeout()
Timeout in seconds before an abandoned connection can be removed.
|
boolean |
getRollbackOnReturn()
Gets the current value of the flag that controls whether a connection will be rolled back when it is returned to
the pool if auto commit is not enabled and the connection is not read only.
|
long |
getSoftMinEvictableIdleTimeMillis()
Returns the minimum amount of time a connection may sit idle in the pool before it is eligible for eviction by
the idle object evictor, with the extra condition that at least "minIdle" connections remain in the pool.
|
boolean |
getTestOnBorrow()
Returns the
testOnBorrow property. |
boolean |
getTestOnCreate()
Returns the
testOnCreate property. |
boolean |
getTestOnReturn()
Returns the value of the
testOnReturn property. |
boolean |
getTestWhileIdle()
Returns the value of the
testWhileIdle property. |
long |
getTimeBetweenEvictionRunsMillis()
Returns the value of the
timeBetweenEvictionRunsMillis property. |
java.lang.String |
getUrl()
Returns the JDBC connection
url property. |
java.lang.String |
getUsername()
Returns the JDBC connection
userName property. |
java.lang.String |
getValidationQuery()
Returns the validation query used to validate connections before returning them.
|
int |
getValidationQueryTimeout()
Returns the validation query timeout.
|
void |
invalidateConnection(java.sql.Connection connection)
Manually invalidates a connection, effectively requesting the pool to try to close it, remove it from the pool
and reclaim pool capacity.
|
boolean |
isAccessToUnderlyingConnectionAllowed()
Returns the value of the accessToUnderlyingConnectionAllowed property.
|
boolean |
isClosed()
If true, this data source is closed and no more connections can be retrieved from this datasource.
|
boolean |
isPoolPreparedStatements()
Returns true if we are pooling statements.
|
boolean |
isWrapperFor(java.lang.Class<?> iface) |
protected void |
log(java.lang.String message) |
void |
postDeregister() |
void |
postRegister(java.lang.Boolean registrationDone) |
void |
preDeregister() |
javax.management.ObjectName |
preRegister(javax.management.MBeanServer server,
javax.management.ObjectName objectName) |
void |
removeConnectionProperty(java.lang.String name)
Removes a custom connection property.
|
void |
setAbandonedLogWriter(java.io.PrintWriter logWriter)
Sets the print writer to be used by this configuration to log information on abandoned objects.
|
void |
setAbandonedUsageTracking(boolean usageTracking)
If the connection pool implements
UsageTracking , configure whether
the connection pool should record a stack trace every time a method is called on a pooled connection and retain
the most recent stack trace to aid debugging of abandoned connections. |
void |
setAccessToUnderlyingConnectionAllowed(boolean allow)
Sets the value of the accessToUnderlyingConnectionAllowed property.
|
void |
setCacheState(boolean cacheState)
Sets the state caching flag.
|
void |
setConnectionInitSqls(java.util.Collection<java.lang.String> connectionInitSqls)
Sets the list of SQL statements to be executed when a physical connection is first created.
|
void |
setConnectionProperties(java.lang.String connectionProperties)
Sets the connection properties passed to driver.connect(...).
|
void |
setDefaultAutoCommit(java.lang.Boolean defaultAutoCommit)
Sets default auto-commit state of connections returned by this datasource.
|
void |
setDefaultCatalog(java.lang.String defaultCatalog)
Sets the default catalog.
|
void |
setDefaultQueryTimeout(java.lang.Integer defaultQueryTimeoutSeconds)
Sets the default query timeout that will be used for
Statement s created from this
connection. |
void |
setDefaultReadOnly(java.lang.Boolean defaultReadOnly)
Sets defaultReadonly property.
|
void |
setDefaultTransactionIsolation(int defaultTransactionIsolation)
Sets the default transaction isolation state for returned connections.
|
void |
setDisconnectionSqlCodes(java.util.Collection<java.lang.String> disconnectionSqlCodes)
Sets the SQL_STATE codes considered to signal fatal conditions.
|
void |
setDriver(java.sql.Driver driver)
Sets the JDBC Driver instance to use for this pool.
|
void |
setDriverClassLoader(java.lang.ClassLoader driverClassLoader)
Sets the class loader to be used to load the JDBC driver.
|
void |
setDriverClassName(java.lang.String driverClassName)
Sets the JDBC driver class name.
|
void |
setEnableAutoCommitOnReturn(boolean enableAutoCommitOnReturn)
Sets the value of the flag that controls whether or not connections being returned to the pool will be checked
and configured with
Connection.setAutoCommit(true) if the auto commit
setting is false when the connection is returned. |
void |
setEvictionPolicyClassName(java.lang.String evictionPolicyClassName)
Sets the EvictionPolicy implementation to use with this connection pool.
|
void |
setFastFailValidation(boolean fastFailValidation) |
void |
setInitialSize(int initialSize)
Sets the initial size of the connection pool.
|
void |
setJmxName(java.lang.String jmxName)
Sets the JMX name that has been requested for this DataSource.
|
void |
setLifo(boolean lifo)
Sets the LIFO property.
|
void |
setLogAbandoned(boolean logAbandoned) |
void |
setLogExpiredConnections(boolean logExpiredConnections)
When
getMaxConnLifetimeMillis() is set to limit connection lifetime, this property determines whether or
not log messages are generated when the pool closes connections due to maximum lifetime exceeded. |
void |
setLoginTimeout(int loginTimeout)
BasicDataSource does NOT support this method.
|
void |
setLogWriter(java.io.PrintWriter logWriter)
Sets the log writer being used by this data source.
|
void |
setMaxConnLifetimeMillis(long maxConnLifetimeMillis)
Sets the maximum permitted lifetime of a connection in milliseconds.
|
void |
setMaxIdle(int maxIdle)
Sets the maximum number of connections that can remain idle in the pool.
|
void |
setMaxOpenPreparedStatements(int maxOpenStatements)
Sets the value of the
maxOpenPreparedStatements property. |
void |
setMaxTotal(int maxTotal)
Sets the maximum total number of idle and borrows connections that can be active at the same time.
|
void |
setMaxWaitMillis(long maxWaitMillis)
Sets the MaxWaitMillis property.
|
void |
setMinEvictableIdleTimeMillis(long minEvictableIdleTimeMillis)
Sets the
minEvictableIdleTimeMillis property. |
void |
setMinIdle(int minIdle)
Sets the minimum number of idle connections in the pool.
|
void |
setNumTestsPerEvictionRun(int numTestsPerEvictionRun)
Sets the value of the
numTestsPerEvictionRun property. |
void |
setPassword(java.lang.String password)
Sets the
password . |
void |
setPoolPreparedStatements(boolean poolingStatements)
Sets whether to pool statements or not.
|
void |
setRemoveAbandonedOnBorrow(boolean removeAbandonedOnBorrow) |
void |
setRemoveAbandonedOnMaintenance(boolean removeAbandonedOnMaintenance) |
void |
setRemoveAbandonedTimeout(int removeAbandonedTimeout)
Sets the timeout in seconds before an abandoned connection can be removed.
|
void |
setRollbackOnReturn(boolean rollbackOnReturn)
Sets the flag that controls if a connection will be rolled back when it is returned to the pool if auto commit is
not enabled and the connection is not read only.
|
void |
setSoftMinEvictableIdleTimeMillis(long softMinEvictableIdleTimeMillis)
Sets the minimum amount of time a connection may sit idle in the pool before it is eligible for eviction by the
idle object evictor, with the extra condition that at least "minIdle" connections remain in the pool.
|
void |
setTestOnBorrow(boolean testOnBorrow)
Sets the
testOnBorrow property. |
void |
setTestOnCreate(boolean testOnCreate)
Sets the
testOnCreate property. |
void |
setTestOnReturn(boolean testOnReturn)
Sets the
testOnReturn property. |
void |
setTestWhileIdle(boolean testWhileIdle)
Sets the
testWhileIdle property. |
void |
setTimeBetweenEvictionRunsMillis(long timeBetweenEvictionRunsMillis)
Sets the
timeBetweenEvictionRunsMillis property. |
void |
setUrl(java.lang.String url)
Sets the
url . |
void |
setUsername(java.lang.String userName)
Sets the
userName . |
void |
setValidationQuery(java.lang.String validationQuery)
Sets the
validationQuery . |
void |
setValidationQueryTimeout(int validationQueryTimeoutSeconds)
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.
|
protected void |
startPoolMaintenance()
Starts the connection pool maintenance task, if configured.
|
<T> T |
unwrap(java.lang.Class<T> iface) |
protected static void |
validateConnectionFactory(PoolableConnectionFactory connectionFactory) |
public java.lang.Boolean getDefaultAutoCommit()
getDefaultAutoCommit
in interface BasicDataSourceMXBean
public void setDefaultAutoCommit(java.lang.Boolean defaultAutoCommit)
Sets default auto-commit state of connections returned by this datasource.
Note: this method currently has no effect once the pool has been initialized. The pool is initialized the first
time one of the following methods is invoked: getConnection, setLogwriter,
setLoginTimeout, getLoginTimeout, getLogWriter.
defaultAutoCommit
- default auto-commit valuepublic java.lang.Boolean getDefaultReadOnly()
getDefaultReadOnly
in interface BasicDataSourceMXBean
public void setDefaultReadOnly(java.lang.Boolean defaultReadOnly)
Sets defaultReadonly property.
Note: this method currently has no effect once the pool has been initialized. The pool is initialized the first
time one of the following methods is invoked: getConnection, setLogwriter,
setLoginTimeout, getLoginTimeout, getLogWriter.
defaultReadOnly
- default read-only valuepublic int getDefaultTransactionIsolation()
getDefaultTransactionIsolation
in interface BasicDataSourceMXBean
Connection.getTransactionIsolation()
public void setDefaultTransactionIsolation(int defaultTransactionIsolation)
Sets the default transaction isolation state for returned connections.
Note: this method currently has no effect once the pool has been initialized. The pool is initialized the first
time one of the following methods is invoked: getConnection, setLogwriter,
setLoginTimeout, getLoginTimeout, getLogWriter.
defaultTransactionIsolation
- the default transaction isolation stateConnection.getTransactionIsolation()
public java.lang.Integer getDefaultQueryTimeout()
Statement
s created from this
connection. null
means that the driver default will be used.public void setDefaultQueryTimeout(java.lang.Integer defaultQueryTimeoutSeconds)
Statement
s created from this
connection. null
means that the driver default will be used.defaultQueryTimeoutSeconds
- The default query timeout in seconds.public java.lang.String getDefaultCatalog()
getDefaultCatalog
in interface BasicDataSourceMXBean
public void setDefaultCatalog(java.lang.String defaultCatalog)
Sets the default catalog.
Note: this method currently has no effect once the pool has been initialized. The pool is initialized the first
time one of the following methods is invoked: getConnection, setLogwriter,
setLoginTimeout, getLoginTimeout, getLogWriter.
defaultCatalog
- the default catalogpublic boolean getCacheState()
getCacheState
in interface BasicDataSourceMXBean
public void setCacheState(boolean cacheState)
cacheState
- The new value for the state caching flagpublic java.sql.Driver getDriver()
Note: This getter only returns the last value set by a call to setDriver(Driver)
. It does not return any
driver instance that may have been created from the value set via setDriverClassName(String)
.
public void setDriver(java.sql.Driver driver)
Note: this method currently has no effect once the pool has been initialized. The pool is initialized the first
time one of the following methods is invoked: getConnection, setLogwriter,
setLoginTimeout, getLoginTimeout, getLogWriter.
driver
- The JDBC Driver instance to use for this pool.public java.lang.String getDriverClassName()
Note: This getter only returns the last value set by a call to setDriverClassName(String)
. It does not
return the class name of any driver that may have been set via setDriver(Driver)
.
getDriverClassName
in interface BasicDataSourceMXBean
public void setDriverClassName(java.lang.String driverClassName)
Sets the JDBC driver class name.
Note: this method currently has no effect once the pool has been initialized. The pool is initialized the first
time one of the following methods is invoked: getConnection, setLogwriter,
setLoginTimeout, getLoginTimeout, getLogWriter.
driverClassName
- the class name of the JDBC driverpublic java.lang.ClassLoader getDriverClassLoader()
null
if no class loader has
been explicitly specified.
Note: This getter only returns the last value set by a call to setDriverClassLoader(ClassLoader)
. It
does not return the class loader of any driver that may have been set via setDriver(Driver)
.
public void setDriverClassLoader(java.lang.ClassLoader driverClassLoader)
Sets the class loader to be used to load the JDBC driver.
Note: this method currently has no effect once the pool has been initialized. The pool is initialized the first
time one of the following methods is invoked: getConnection, setLogwriter,
setLoginTimeout, getLoginTimeout, getLogWriter.
driverClassLoader
- the class loader with which to load the JDBC driverpublic boolean getLifo()
getLifo
in interface BasicDataSourceMXBean
public void setLifo(boolean lifo)
lifo
- the new value for the LIFO propertypublic int getMaxTotal()
Returns the maximum number of active connections that can be allocated at the same time.
A negative number means that there is no limit.
getMaxTotal
in interface BasicDataSourceMXBean
public void setMaxTotal(int maxTotal)
maxTotal
- the new value for maxTotalgetMaxTotal()
public int getMaxIdle()
Returns the maximum number of connections that can remain idle in the pool. Excess idle connections are destroyed on return to the pool.
A negative value indicates that there is no limit
getMaxIdle
in interface BasicDataSourceMXBean
public void setMaxIdle(int maxIdle)
maxIdle
- the new value for maxIdlegetMaxIdle()
public int getMinIdle()
timeBetweenEvictionRunsMillis
has a positive value.getMinIdle
in interface BasicDataSourceMXBean
GenericObjectPool.getMinIdle()
public void setMinIdle(int minIdle)
timeBetweenEvictionRunsMillis
has a positive value.minIdle
- the new value for minIdleGenericObjectPool.setMinIdle(int)
public int getInitialSize()
getInitialSize
in interface BasicDataSourceMXBean
public void setInitialSize(int initialSize)
Sets the initial size of the connection pool.
Note: this method currently has no effect once the pool has been initialized. The pool is initialized the first
time one of the following methods is invoked: getConnection, setLogwriter,
setLoginTimeout, getLoginTimeout, getLogWriter.
initialSize
- the number of connections created when the pool is initializedpublic long getMaxWaitMillis()
getMaxWaitMillis
in interface BasicDataSourceMXBean
public void setMaxWaitMillis(long maxWaitMillis)
maxWaitMillis
- the new value for MaxWaitMillisgetMaxWaitMillis()
public boolean isPoolPreparedStatements()
isPoolPreparedStatements
in interface BasicDataSourceMXBean
public void setPoolPreparedStatements(boolean poolingStatements)
Sets whether to pool statements or not.
Note: this method currently has no effect once the pool has been initialized. The pool is initialized the first
time one of the following methods is invoked: getConnection, setLogwriter,
setLoginTimeout, getLoginTimeout, getLogWriter.
poolingStatements
- pooling on or offpublic int getMaxOpenPreparedStatements()
maxOpenPreparedStatements
property.getMaxOpenPreparedStatements
in interface BasicDataSourceMXBean
public void setMaxOpenPreparedStatements(int maxOpenStatements)
Sets the value of the maxOpenPreparedStatements
property.
Note: this method currently has no effect once the pool has been initialized. The pool is initialized the first
time one of the following methods is invoked: getConnection, setLogwriter,
setLoginTimeout, getLoginTimeout, getLogWriter.
maxOpenStatements
- the new maximum number of prepared statementspublic boolean getTestOnCreate()
testOnCreate
property.getTestOnCreate
in interface BasicDataSourceMXBean
testOnCreate
public void setTestOnCreate(boolean testOnCreate)
testOnCreate
property. This property determines whether or not the pool will validate objects
immediately after they are created by the pooltestOnCreate
- new value for testOnCreate propertypublic boolean getTestOnBorrow()
testOnBorrow
property.getTestOnBorrow
in interface BasicDataSourceMXBean
testOnBorrow
public void setTestOnBorrow(boolean testOnBorrow)
testOnBorrow
property. This property determines whether or not the pool will validate objects
before they are borrowed from the pool.testOnBorrow
- new value for testOnBorrow propertypublic boolean getTestOnReturn()
testOnReturn
property.testOnReturn
public void setTestOnReturn(boolean testOnReturn)
testOnReturn
property. This property determines whether or not the pool will validate
objects before they are returned to the pool.testOnReturn
- new value for testOnReturn propertypublic long getTimeBetweenEvictionRunsMillis()
timeBetweenEvictionRunsMillis
property.getTimeBetweenEvictionRunsMillis
in interface BasicDataSourceMXBean
timeBetweenEvictionRunsMillis
public void setTimeBetweenEvictionRunsMillis(long timeBetweenEvictionRunsMillis)
timeBetweenEvictionRunsMillis
property.timeBetweenEvictionRunsMillis
- the new time between evictor runstimeBetweenEvictionRunsMillis
public int getNumTestsPerEvictionRun()
numTestsPerEvictionRun
property.getNumTestsPerEvictionRun
in interface BasicDataSourceMXBean
numTestsPerEvictionRun
public void setNumTestsPerEvictionRun(int numTestsPerEvictionRun)
numTestsPerEvictionRun
property.numTestsPerEvictionRun
- the new numTestsPerEvictionRun
valuenumTestsPerEvictionRun
public long getMinEvictableIdleTimeMillis()
minEvictableIdleTimeMillis
property.getMinEvictableIdleTimeMillis
in interface BasicDataSourceMXBean
minEvictableIdleTimeMillis
propertyminEvictableIdleTimeMillis
public void setMinEvictableIdleTimeMillis(long minEvictableIdleTimeMillis)
minEvictableIdleTimeMillis
property.minEvictableIdleTimeMillis
- the minimum amount of time an object may sit idle in the poolminEvictableIdleTimeMillis
public void setSoftMinEvictableIdleTimeMillis(long softMinEvictableIdleTimeMillis)
softMinEvictableIdleTimeMillis
- minimum amount of time a connection may sit idle in the pool before it is eligible for eviction,
assuming there are minIdle idle connections in the pool.getSoftMinEvictableIdleTimeMillis()
public long getSoftMinEvictableIdleTimeMillis()
Returns the minimum amount of time a connection may sit idle in the pool before it is eligible for eviction by the idle object evictor, with the extra condition that at least "minIdle" connections remain in the pool.
When minEvictableIdleTimeMillis
is set to a positive value,
minEvictableIdleTimeMillis is examined first by the idle connection evictor - i.e. when idle connections are
visited by the evictor, idle time is first compared against minEvictableIdleTimeMillis
(without
considering the number of idle connections in the pool) and then against softMinEvictableIdleTimeMillis
,
including the minIdle
, constraint.
getSoftMinEvictableIdleTimeMillis
in interface BasicDataSourceMXBean
public java.lang.String getEvictionPolicyClassName()
public void setEvictionPolicyClassName(java.lang.String evictionPolicyClassName)
evictionPolicyClassName
- The fully qualified class name of the EvictionPolicy implementationpublic boolean getTestWhileIdle()
testWhileIdle
property.getTestWhileIdle
in interface BasicDataSourceMXBean
testWhileIdle
public void setTestWhileIdle(boolean testWhileIdle)
testWhileIdle
property. This property determines whether or not the idle object evictor
will validate connections.testWhileIdle
- new value for testWhileIdle propertypublic int getNumActive()
getNumActive
in interface BasicDataSourceMXBean
public int getNumIdle()
getNumIdle
in interface BasicDataSourceMXBean
public java.lang.String getPassword()
getPassword
in interface BasicDataSourceMXBean
public void setPassword(java.lang.String password)
Sets the password
.
Note: this method currently has no effect once the pool has been initialized. The pool is initialized the first
time one of the following methods is invoked: getConnection, setLogwriter,
setLoginTimeout, getLoginTimeout, getLogWriter.
password
- new value for the passwordpublic java.lang.String getUrl()
url
property.getUrl
in interface BasicDataSourceMXBean
url
passed to the JDBC driver to establish connectionspublic void setUrl(java.lang.String url)
Sets the url
.
Note: this method currently has no effect once the pool has been initialized. The pool is initialized the first
time one of the following methods is invoked: getConnection, setLogwriter,
setLoginTimeout, getLoginTimeout, getLogWriter.
url
- the new value for the JDBC connection urlpublic java.lang.String getUsername()
userName
property.getUsername
in interface BasicDataSourceMXBean
userName
passed to the JDBC driver to establish connectionspublic void setUsername(java.lang.String userName)
Sets the userName
.
Note: this method currently has no effect once the pool has been initialized. The pool is initialized the first
time one of the following methods is invoked: getConnection, setLogwriter,
setLoginTimeout, getLoginTimeout, getLogWriter.
userName
- the new value for the JDBC connection user namepublic java.lang.String getValidationQuery()
getValidationQuery
in interface BasicDataSourceMXBean
validationQuery
public void setValidationQuery(java.lang.String validationQuery)
Sets the validationQuery
.
Note: this method currently has no effect once the pool has been initialized. The pool is initialized the first
time one of the following methods is invoked: getConnection, setLogwriter,
setLoginTimeout, getLoginTimeout, getLogWriter.
validationQuery
- the new value for the validation querypublic int getValidationQueryTimeout()
getValidationQueryTimeout
in interface BasicDataSourceMXBean
public void setValidationQueryTimeout(int validationQueryTimeoutSeconds)
Note: this method currently has no effect once the pool has been initialized. The pool is initialized the first
time one of the following methods is invoked: getConnection, setLogwriter,
setLoginTimeout, getLoginTimeout, getLogWriter.
validationQueryTimeoutSeconds
- new validation query timeout value in secondspublic java.util.List<java.lang.String> getConnectionInitSqls()
public java.lang.String[] getConnectionInitSqlsAsArray()
getConnectionInitSqls()
but in an array so it is accessible via JMX.getConnectionInitSqlsAsArray
in interface BasicDataSourceMXBean
getConnectionInitSqlsAsArray()
public void setConnectionInitSqls(java.util.Collection<java.lang.String> connectionInitSqls)
Note: this method currently has no effect once the pool has been initialized. The pool is initialized the first
time one of the following methods is invoked: getConnection, setLogwriter,
setLoginTimeout, getLoginTimeout, getLogWriter.
connectionInitSqls
- Collection of SQL statements to execute on connection creationpublic boolean isAccessToUnderlyingConnectionAllowed()
isAccessToUnderlyingConnectionAllowed
in interface BasicDataSourceMXBean
public void setAccessToUnderlyingConnectionAllowed(boolean allow)
Sets the value of the accessToUnderlyingConnectionAllowed property. It controls if the PoolGuard allows access to the underlying connection. (Default: false)
Note: this method currently has no effect once the pool has been initialized. The pool is initialized the first
time one of the following methods is invoked: getConnection, setLogwriter,
setLoginTimeout, getLoginTimeout, getLogWriter.
allow
- Access to the underlying connection is granted when true.public long getMaxConnLifetimeMillis()
getMaxConnLifetimeMillis
in interface BasicDataSourceMXBean
getMaxConnLifetimeMillis()
public boolean getLogExpiredConnections()
getMaxConnLifetimeMillis()
is set to limit connection lifetime, this property determines whether or
not log messages are generated when the pool closes connections due to maximum lifetime exceeded.getLogExpiredConnections
in interface BasicDataSourceMXBean
getLogExpiredConnections()
public void setMaxConnLifetimeMillis(long maxConnLifetimeMillis)
Sets the maximum permitted lifetime of a connection in milliseconds. A value of zero or less indicates an infinite lifetime.
Note: this method currently has no effect once the pool has been initialized. The pool is initialized the first
time one of the following methods is invoked: getConnection, setLogwriter,
setLoginTimeout, getLoginTimeout, getLogWriter.
maxConnLifetimeMillis
- The maximum permitted lifetime of a connection in milliseconds.public void setLogExpiredConnections(boolean logExpiredConnections)
getMaxConnLifetimeMillis()
is set to limit connection lifetime, this property determines whether or
not log messages are generated when the pool closes connections due to maximum lifetime exceeded. Set this
property to false to suppress log messages when connections expire.logExpiredConnections
- Whether or not log messages are generated when the pool closes connections due to maximum lifetime
exceeded.public java.lang.String getJmxName()
public void setJmxName(java.lang.String jmxName)
jmxName
- The JMX name that has been requested for this DataSourcepublic boolean getEnableAutoCommitOnReturn()
Connection.setAutoCommit(true)
if the auto commit
setting is false
when the connection is returned. It is true
by default.public void setEnableAutoCommitOnReturn(boolean enableAutoCommitOnReturn)
Connection.setAutoCommit(true)
if the auto commit
setting is false
when the connection is returned. It is true
by default.enableAutoCommitOnReturn
- Whether or not connections being returned to the pool will be checked and configured with auto-commit.public boolean getRollbackOnReturn()
public void setRollbackOnReturn(boolean rollbackOnReturn)
rollbackOnReturn
- whether a connection will be rolled back when it is returned to the pool.public java.util.Set<java.lang.String> getDisconnectionSqlCodes()
setDisconnectionSqlCodes(Collection)
public java.lang.String[] getDisconnectionSqlCodesAsArray()
getDisconnectionSqlCodes()
but in an array so it is accessible via JMX.getDisconnectionSqlCodesAsArray
in interface BasicDataSourceMXBean
getDisconnectionSqlCodesAsArray()
public void setDisconnectionSqlCodes(java.util.Collection<java.lang.String> disconnectionSqlCodes)
Overrides the defaults in Utils.DISCONNECTION_SQL_CODES
(plus anything starting with
Utils.DISCONNECTION_SQL_CODE_PREFIX
). If this property is non-null and getFastFailValidation()
is true
, whenever connections created by this datasource 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 getFastFailValidation()
is false
setting this property has no effect.
Note: this method currently has no effect once the pool has been initialized. The pool is initialized the first
time one of the following methods is invoked: getConnection, setLogwriter,
setLoginTimeout, getLoginTimeout, getLogWriter
.
disconnectionSqlCodes
- SQL_STATE codes considered to signal fatal conditionspublic boolean getFastFailValidation()
getFastFailValidation
in interface BasicDataSourceMXBean
setDisconnectionSqlCodes(Collection)
public void setFastFailValidation(boolean fastFailValidation)
fastFailValidation
- true means connections created by this factory will fast fail validationgetFastFailValidation()
protected GenericObjectPool<PoolableConnection> getConnectionPool()
public java.sql.Connection getConnection() throws java.sql.SQLException
getConnection
in interface javax.sql.DataSource
java.sql.SQLException
- if a database access error occurspublic java.sql.Connection getConnection(java.lang.String user, java.lang.String pass) throws java.sql.SQLException
getConnection
in interface javax.sql.DataSource
user
- Database user on whose behalf the Connection is being madepass
- The database user's passwordjava.lang.UnsupportedOperationException
- always thrown.java.sql.SQLException
- if a database access error occurspublic int getLoginTimeout() throws java.sql.SQLException
Returns the login timeout (in seconds) for connecting to the database.
Calls createDataSource()
, so has the side effect of initializing the connection pool.
getLoginTimeout
in interface javax.sql.CommonDataSource
java.sql.SQLException
- if a database access error occursjava.lang.UnsupportedOperationException
- If the DataSource implementation does not support the login timeout feature.public java.io.PrintWriter getLogWriter() throws java.sql.SQLException
Returns the log writer being used by this data source.
Calls createDataSource()
, so has the side effect of initializing the connection pool.
getLogWriter
in interface javax.sql.CommonDataSource
java.sql.SQLException
- if a database access error occurspublic void setLoginTimeout(int loginTimeout) throws java.sql.SQLException
Set the login timeout (in seconds) for connecting to the database.
Calls createDataSource()
, so has the side effect of initializing the connection pool.
setLoginTimeout
in interface javax.sql.CommonDataSource
loginTimeout
- The new login timeout, or zero for no timeoutjava.lang.UnsupportedOperationException
- If the DataSource implementation does not support the login timeout feature.java.sql.SQLException
- if a database access error occurspublic void setLogWriter(java.io.PrintWriter logWriter) throws java.sql.SQLException
Sets the log writer being used by this data source.
Calls createDataSource()
, so has the side effect of initializing the connection pool.
setLogWriter
in interface javax.sql.CommonDataSource
logWriter
- The new log writerjava.sql.SQLException
- if a database access error occurspublic boolean getRemoveAbandonedOnBorrow()
Flag to remove abandoned connections if they exceed the removeAbandonedTimeout when borrowObject is invoked.
The default value is false.
If set to true a connection is considered abandoned and eligible for removal if it has not been used for more
than removeAbandonedTimeout
seconds.
Abandoned connections are identified and removed when getConnection()
is invoked and all of the
following conditions hold:
getRemoveAbandonedOnBorrow
in interface BasicDataSourceMXBean
getRemoveAbandonedOnBorrow()
getRemoveAbandonedTimeout()
public void setRemoveAbandonedOnMaintenance(boolean removeAbandonedOnMaintenance)
removeAbandonedOnMaintenance
- true means abandoned connections may be removed on pool maintenance.getRemoveAbandonedOnMaintenance()
public boolean getRemoveAbandonedOnMaintenance()
Flag to remove abandoned connections if they exceed the removeAbandonedTimeout during pool maintenance.
The default value is false.
If set to true a connection is considered abandoned and eligible for removal if it has not been used for more
than removeAbandonedTimeout
seconds.
getRemoveAbandonedOnMaintenance
in interface BasicDataSourceMXBean
getRemoveAbandonedOnMaintenance()
getRemoveAbandonedTimeout()
public void setRemoveAbandonedOnBorrow(boolean removeAbandonedOnBorrow)
removeAbandonedOnBorrow
- true means abandoned connections may be removed when connections are borrowed from the pool.getRemoveAbandonedOnBorrow()
public int getRemoveAbandonedTimeout()
Timeout in seconds before an abandoned connection can be removed.
Creating a Statement, PreparedStatement or CallableStatement or using one of these to execute a query (using one of the execute methods) resets the lastUsed property of the parent connection.
Abandoned connection cleanup happens when:
getRemoveAbandonedOnBorrow()
or getRemoveAbandonedOnMaintenance()
= truenumIdle
< 2numActive
> maxTotal
- 3The default value is 300 seconds.
getRemoveAbandonedTimeout
in interface BasicDataSourceMXBean
getRemoveAbandonedTimeout()
public void setRemoveAbandonedTimeout(int removeAbandonedTimeout)
Sets the timeout in seconds before an abandoned connection can be removed.
Setting this property has no effect if getRemoveAbandonedOnBorrow()
and
getRemoveAbandonedOnMaintenance()
are false.
removeAbandonedTimeout
- new abandoned timeout in secondsgetRemoveAbandonedTimeout()
,
getRemoveAbandonedOnBorrow()
,
getRemoveAbandonedOnMaintenance()
public boolean getLogAbandoned()
Flag to log stack traces for application code which abandoned a Statement or Connection.
Defaults to false.
Logging of abandoned Statements and Connections adds overhead for every Connection open or new Statement because a stack trace has to be generated.
getLogAbandoned
in interface BasicDataSourceMXBean
getLogAbandoned()
public void setLogAbandoned(boolean logAbandoned)
logAbandoned
- new logAbandoned property valuepublic java.io.PrintWriter getAbandonedLogWriter()
public void setAbandonedLogWriter(java.io.PrintWriter logWriter)
logWriter
- The new log writerpublic boolean getAbandonedUsageTracking()
UsageTracking
, should the
connection pool record a stack trace every time a method is called on a pooled connection and retain the most
recent stack trace to aid debugging of abandoned connections?getAbandonedUsageTracking
in interface BasicDataSourceMXBean
true
if usage tracking is enabledpublic void setAbandonedUsageTracking(boolean usageTracking)
UsageTracking
, configure whether
the connection pool should record a stack trace every time a method is called on a pooled connection and retain
the most recent stack trace to aid debugging of abandoned connections.usageTracking
- A value of true
will enable the recording of a stack trace on every use of a pooled
connectionpublic void addConnectionProperty(java.lang.String name, java.lang.String value)
name
- Name of the custom connection propertyvalue
- Value of the custom connection propertypublic void removeConnectionProperty(java.lang.String name)
name
- Name of the custom connection property to removeaddConnectionProperty(String, String)
public void setConnectionProperties(java.lang.String connectionProperties)
Format of the string must be [propertyName=property;]*
NOTE - The "user" and "password" properties will be added explicitly, so they do not need to be included here.
connectionProperties
- the connection properties used to create new connectionspublic void close() throws java.sql.SQLException
Closes and releases all idle connections that are currently stored in the connection pool associated with this data source.
Connections that are checked out to clients when this method is invoked are not affected. When client
applications subsequently invoke Connection.close()
to return these connections to the pool, the
underlying JDBC connections are closed.
Attempts to acquire connections using getConnection()
after this method has been invoked result in
SQLExceptions.
This method is idempotent - i.e., closing an already closed BasicDataSource has no effect and does not generate exceptions.
close
in interface java.lang.AutoCloseable
java.sql.SQLException
- if an error occurs closing idle connectionspublic boolean isClosed()
isClosed
in interface BasicDataSourceMXBean
public boolean isWrapperFor(java.lang.Class<?> iface) throws java.sql.SQLException
isWrapperFor
in interface java.sql.Wrapper
java.sql.SQLException
public <T> T unwrap(java.lang.Class<T> iface) throws java.sql.SQLException
unwrap
in interface java.sql.Wrapper
java.sql.SQLException
public java.util.logging.Logger getParentLogger() throws java.sql.SQLFeatureNotSupportedException
getParentLogger
in interface javax.sql.CommonDataSource
java.sql.SQLFeatureNotSupportedException
public void invalidateConnection(java.sql.Connection connection) throws java.lang.IllegalStateException
connection
- The Connection to invalidate.java.lang.IllegalStateException
- if invalidating the connection failed.protected javax.sql.DataSource createDataSource() throws java.sql.SQLException
Creates (if necessary) and return the internal data source we are using to manage our connections.
java.sql.SQLException
- if the object pool cannot be created.protected ConnectionFactory createConnectionFactory() throws java.sql.SQLException
setDriver(Driver)
use itdriverClassName
is specified that class is loaded using the
ClassLoader
of this class or, if driverClassLoader
is set, driverClassName
is loaded
with the specified ClassLoader
.driverClassName
is specified and the previous attempt fails, the class is loaded using the
context class loader of the current thread.DriverManager
using the specified url
.
java.sql.SQLException
- If the connection factort cannot be createdprotected void createConnectionPool(PoolableConnectionFactory factory)
This implementation configures all pool properties other than timeBetweenEvictionRunsMillis. Setting that
property is deferred to startPoolMaintenance()
, since setting timeBetweenEvictionRunsMillis to a
positive value causes GenericObjectPool
's eviction timer to be started.
factory
- The factory to use to create new connections for this pool.protected GenericObjectPool<PoolableConnection> createObjectPool(PoolableConnectionFactory factory, GenericObjectPoolConfig<PoolableConnection> poolConfig, AbandonedConfig abandonedConfig)
JDBC connections
.factory
- the object factorypoolConfig
- the object pool configurationabandonedConfig
- the abandoned objects configurationprotected void startPoolMaintenance()
protected javax.sql.DataSource createDataSourceInstance() throws java.sql.SQLException
java.sql.SQLException
- if unable to create a datasource instanceprotected PoolableConnectionFactory createPoolableConnectionFactory(ConnectionFactory driverConnectionFactory) throws java.sql.SQLException
driverConnectionFactory
- JDBC connection factoryjava.sql.SQLException
- if an error occurs creating the PoolableConnectionFactoryprotected static void validateConnectionFactory(PoolableConnectionFactory connectionFactory) throws java.lang.Exception
java.lang.Exception
protected void log(java.lang.String message)
public javax.management.ObjectName preRegister(javax.management.MBeanServer server, javax.management.ObjectName objectName)
preRegister
in interface javax.management.MBeanRegistration
public void postRegister(java.lang.Boolean registrationDone)
postRegister
in interface javax.management.MBeanRegistration
public void preDeregister() throws java.lang.Exception
preDeregister
in interface javax.management.MBeanRegistration
java.lang.Exception
public void postDeregister()
postDeregister
in interface javax.management.MBeanRegistration
protected javax.management.ObjectName getRegisteredJmxName()
Copyright © 2000-2018 Apache Software Foundation. All Rights Reserved.