public class BasicDataSource extends Object implements DataSource, BasicDataSourceMXBean, MBeanRegistration, AutoCloseable
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 solution for basic requirements.
Constructor and Description |
---|
BasicDataSource() |
Modifier and Type | Method and Description |
---|---|
void |
addConnectionProperty(String name,
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 data source.
|
protected void |
createConnectionPool(PoolableConnectionFactory factory)
Creates a connection pool for this datasource.
|
protected DataSource |
createDataSource()
Creates (if necessary) and return the internal data source we are using to manage our connections.
|
protected 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.
|
void |
evict()
Manually evicts idle connections
|
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 |
getAutoCommitOnReturn()
Gets 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. |
boolean |
getCacheState()
Gets the state caching flag.
|
Connection |
getConnection()
Creates (if necessary) and return a connection to the database.
|
Connection |
getConnection(String user,
String pass)
BasicDataSource does NOT support this method.
|
String |
getConnectionFactoryClassName()
Gets the ConnectionFactoryClassName that has been configured for use by this pool.
|
List<String> |
getConnectionInitSqls()
Gets the list of SQL statements executed when a physical connection is first created.
|
String[] |
getConnectionInitSqlsAsArray()
Provides the same data as
getConnectionInitSqls() but in an array so it is accessible via JMX. |
protected GenericObjectPool<PoolableConnection> |
getConnectionPool()
Gets the underlying connection pool.
|
Boolean |
getDefaultAutoCommit()
Gets the default auto-commit property.
|
String |
getDefaultCatalog()
Gets the default catalog.
|
Integer |
getDefaultQueryTimeout()
Deprecated.
|
Duration |
getDefaultQueryTimeoutDuration()
Gets the default query timeout that will be used for
Statement s created from this
connection. |
Boolean |
getDefaultReadOnly()
Gets the default readOnly property.
|
String |
getDefaultSchema()
Gets the default schema.
|
int |
getDefaultTransactionIsolation()
Gets the default transaction isolation state of returned connections.
|
Set<String> |
getDisconnectionSqlCodes()
Gets the set of SQL_STATE codes considered to signal fatal conditions.
|
String[] |
getDisconnectionSqlCodesAsArray()
Provides the same data as
getDisconnectionSqlCodes() but in an array so it is accessible via JMX. |
Driver |
getDriver()
Gets the JDBC Driver that has been configured for use by this pool.
|
ClassLoader |
getDriverClassLoader()
Gets the class loader specified for loading the JDBC driver.
|
String |
getDriverClassName()
Gets the JDBC driver class name.
|
Duration |
getDurationBetweenEvictionRuns()
Gets the value of the {code durationBetweenEvictionRuns} property.
|
boolean |
getEnableAutoCommitOnReturn()
Deprecated.
|
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()
Gets the initial size of the connection pool.
|
String |
getJmxName()
Gets the JMX name that has been requested for this DataSource.
|
boolean |
getLifo()
Gets the LIFO property.
|
boolean |
getLogAbandoned()
Flag to log stack traces for application code which abandoned a Statement or Connection.
|
boolean |
getLogExpiredConnections()
When
getMaxConnDuration() 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.
|
PrintWriter |
getLogWriter()
Gets the log writer being used by this data source.
|
Duration |
getMaxConnDuration()
Gets the maximum permitted duration of a connection.
|
long |
getMaxConnLifetimeMillis()
Deprecated.
Use
getMaxConnDuration() . |
int |
getMaxIdle()
Gets the maximum number of connections that can remain idle in the pool.
|
int |
getMaxOpenPreparedStatements()
Gets the value of the
maxOpenPreparedStatements property. |
int |
getMaxTotal()
Gets the maximum number of active connections that can be allocated at the same time.
|
Duration |
getMaxWaitDuration()
Gets the maximum Duration that the pool will wait for a connection to be returned before throwing an exception.
|
long |
getMaxWaitMillis()
Deprecated.
Use
getMaxWaitDuration() . |
Duration |
getMinEvictableIdleDuration()
Gets the {code minEvictableIdleDuration} property.
|
long |
getMinEvictableIdleTimeMillis()
Deprecated.
|
int |
getMinIdle()
Gets 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()
Gets the value of the {code numTestsPerEvictionRun} property.
|
Logger |
getParentLogger() |
String |
getPassword()
Gets the password passed to the JDBC driver to establish connections.
|
protected ObjectName |
getRegisteredJmxName()
Gets the registered JMX ObjectName.
|
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()
Deprecated.
|
Duration |
getRemoveAbandonedTimeoutDuration()
Gets the timeout 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.
|
Duration |
getSoftMinEvictableIdleDuration()
Gets 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.
|
long |
getSoftMinEvictableIdleTimeMillis()
Deprecated.
|
boolean |
getTestOnBorrow()
Gets the {code testOnBorrow} property.
|
boolean |
getTestOnCreate()
Gets the {code testOnCreate} property.
|
boolean |
getTestOnReturn()
Gets the value of the {code testOnReturn} property.
|
boolean |
getTestWhileIdle()
Gets the value of the {code testWhileIdle} property.
|
long |
getTimeBetweenEvictionRunsMillis()
Deprecated.
|
String |
getUrl()
Gets the JDBC connection {code url} property.
|
String |
getUsername()
Gets the JDBC connection {code userName} property.
|
String |
getValidationQuery()
Gets the validation query used to validate connections before returning them.
|
int |
getValidationQueryTimeout()
Deprecated.
|
Duration |
getValidationQueryTimeoutDuration()
Gets the validation query timeout.
|
void |
invalidateConnection(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()
Gets the value of the accessToUnderlyingConnectionAllowed property.
|
boolean |
isClearStatementPoolOnReturn()
Returns true if the statement pool is cleared when the connection is returned to its pool.
|
boolean |
isClosed()
If true, this data source is closed and no more connections can be retrieved from this data source.
|
boolean |
isPoolPreparedStatements()
Returns true if we are pooling statements.
|
boolean |
isWrapperFor(Class<?> iface) |
protected void |
log(String message)
Logs the given message.
|
protected void |
log(String message,
Throwable throwable)
Logs the given throwable.
|
void |
postDeregister() |
void |
postRegister(Boolean registrationDone) |
void |
preDeregister() |
ObjectName |
preRegister(MBeanServer server,
ObjectName objectName) |
void |
removeConnectionProperty(String name)
Removes a custom connection property.
|
void |
restart()
Restarts the datasource.
|
void |
setAbandonedLogWriter(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 |
setAutoCommitOnReturn(boolean autoCommitOnReturn)
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 |
setCacheState(boolean cacheState)
Sets the state caching flag.
|
void |
setClearStatementPoolOnReturn(boolean clearStatementPoolOnReturn)
Sets whether the pool of statements (which was enabled with
setPoolPreparedStatements(boolean) ) should
be cleared when the connection is returned to its pool. |
void |
setConnectionFactoryClassName(String connectionFactoryClassName)
Sets the ConnectionFactory class name.
|
void |
setConnectionInitSqls(Collection<String> connectionInitSqls)
Sets the list of SQL statements to be executed when a physical connection is first created.
|
void |
setConnectionProperties(String connectionProperties)
Sets the connection properties passed to driver.connect(...).
|
void |
setDefaultAutoCommit(Boolean defaultAutoCommit)
Sets default auto-commit state of connections returned by this datasource.
|
void |
setDefaultCatalog(String defaultCatalog)
Sets the default catalog.
|
void |
setDefaultQueryTimeout(Duration defaultQueryTimeoutDuration)
Sets the default query timeout that will be used for
Statement s created from this
connection. |
void |
setDefaultQueryTimeout(Integer defaultQueryTimeoutSeconds)
Deprecated.
|
void |
setDefaultReadOnly(Boolean defaultReadOnly)
Sets defaultReadonly property.
|
void |
setDefaultSchema(String defaultSchema)
Sets the default schema.
|
void |
setDefaultTransactionIsolation(int defaultTransactionIsolation)
Sets the default transaction isolation state for returned connections.
|
void |
setDisconnectionSqlCodes(Collection<String> disconnectionSqlCodes)
Sets the SQL_STATE codes considered to signal fatal conditions.
|
void |
setDriver(Driver driver)
Sets the JDBC Driver instance to use for this pool.
|
void |
setDriverClassLoader(ClassLoader driverClassLoader)
Sets the class loader to be used to load the JDBC driver.
|
void |
setDriverClassName(String driverClassName)
Sets the JDBC driver class name.
|
void |
setDurationBetweenEvictionRuns(Duration timeBetweenEvictionRunsMillis)
Sets the {code durationBetweenEvictionRuns} property.
|
void |
setEnableAutoCommitOnReturn(boolean autoCommitOnReturn)
Deprecated.
|
void |
setEvictionPolicyClassName(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(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
getMaxConnDuration() 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(PrintWriter logWriter)
Sets the log writer being used by this data source.
|
void |
setMaxConn(Duration maxConnDuration)
Sets the maximum permitted lifetime of a connection.
|
void |
setMaxConnLifetimeMillis(long maxConnLifetimeMillis)
Deprecated.
Use
setMaxConn(Duration) . |
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 |
setMaxWait(Duration maxWaitDuration)
Sets the MaxWaitMillis property.
|
void |
setMaxWaitMillis(long maxWaitMillis)
Deprecated.
|
void |
setMinEvictableIdle(Duration minEvictableIdleDuration)
Sets the {code minEvictableIdleDuration} property.
|
void |
setMinEvictableIdleTimeMillis(long minEvictableIdleTimeMillis)
Deprecated.
|
void |
setMinIdle(int minIdle)
Sets the minimum number of idle connections in the pool.
|
void |
setNumTestsPerEvictionRun(int numTestsPerEvictionRun)
Sets the value of the {code numTestsPerEvictionRun} property.
|
void |
setPassword(String password)
Sets the {code password}.
|
void |
setPoolPreparedStatements(boolean poolingStatements)
Sets whether to pool statements or not.
|
void |
setRemoveAbandonedOnBorrow(boolean removeAbandonedOnBorrow) |
void |
setRemoveAbandonedOnMaintenance(boolean removeAbandonedOnMaintenance) |
void |
setRemoveAbandonedTimeout(Duration removeAbandonedTimeout)
Sets the timeout before an abandoned connection can be removed.
|
void |
setRemoveAbandonedTimeout(int removeAbandonedTimeout)
Deprecated.
|
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 |
setSoftMinEvictableIdle(Duration 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 |
setSoftMinEvictableIdleTimeMillis(long softMinEvictableIdleTimeMillis)
Deprecated.
|
void |
setTestOnBorrow(boolean testOnBorrow)
Sets the {code testOnBorrow} property.
|
void |
setTestOnCreate(boolean testOnCreate)
Sets the {code testOnCreate} property.
|
void |
setTestOnReturn(boolean testOnReturn)
Sets the
testOnReturn property. |
void |
setTestWhileIdle(boolean testWhileIdle)
Sets the
testWhileIdle property. |
void |
setTimeBetweenEvictionRunsMillis(long timeBetweenEvictionRunsMillis)
Deprecated.
|
void |
setUrl(String url)
Sets the {code url}.
|
void |
setUsername(String userName)
Sets the {code userName}.
|
void |
setValidationQuery(String validationQuery)
Sets the {code validationQuery}.
|
void |
setValidationQueryTimeout(Duration validationQueryTimeoutDuration)
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.
|
void |
setValidationQueryTimeout(int validationQueryTimeoutSeconds)
Deprecated.
|
void |
start()
Starts the datasource.
|
protected void |
startPoolMaintenance()
Starts the connection pool maintenance task, if configured.
|
<T> T |
unwrap(Class<T> iface) |
protected static void |
validateConnectionFactory(PoolableConnectionFactory connectionFactory)
Validates the given factory.
|
protected static void validateConnectionFactory(PoolableConnectionFactory connectionFactory) throws Exception
connectionFactory
- the factoryException
- Thrown by one of the factory methods while managing a temporary pooled object.public void addConnectionProperty(String name, String value)
name
- Name of the custom connection propertyvalue
- Value of the custom connection propertypublic void close() throws SQLException
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. To reopen a datasource that has been closed using this method, use start()
.
This method is idempotent - i.e., closing an already closed BasicDataSource has no effect and does not generate exceptions.
close
in interface AutoCloseable
SQLException
- if an error occurs closing idle connectionsprotected ConnectionFactory createConnectionFactory() throws SQLException
setDriver(Driver)
use itClassLoader
of this class or, if {code driverClassLoader} is set, {code driverClassName} is loaded
with the specified ClassLoader
.DriverManager
using the specified {code url}.
This method exists so subclasses can replace the implementation class.
SQLException
- If the connection factory 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 DataSource createDataSource() throws SQLException
SQLException
- if the object pool cannot be created.protected DataSource createDataSourceInstance() throws SQLException
SQLException
- if unable to create a datasource instanceprotected GenericObjectPool<PoolableConnection> createObjectPool(PoolableConnectionFactory factory, GenericObjectPoolConfig<PoolableConnection> poolConfig, AbandonedConfig abandonedConfig)
JDBC connections
.factory
- the object factorypoolConfig
- the object pool configurationabandonedConfig
- the abandoned objects configurationprotected PoolableConnectionFactory createPoolableConnectionFactory(ConnectionFactory driverConnectionFactory) throws SQLException
driverConnectionFactory
- JDBC connection factorySQLException
- if an error occurs creating the PoolableConnectionFactorypublic void evict() throws Exception
Exception
- when there is a problem evicting idle objects.public PrintWriter getAbandonedLogWriter()
public 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 DataSourceMXBean
true
if usage tracking is enabledpublic boolean getAutoCommitOnReturn()
Connection.setAutoCommit(true)
if the auto commit
setting is false
when the connection is returned. It is true
by default.public boolean getCacheState()
getCacheState
in interface DataSourceMXBean
public Connection getConnection() throws SQLException
getConnection
in interface DataSource
SQLException
- if a database access error occurspublic Connection getConnection(String user, String pass) throws SQLException
getConnection
in interface DataSource
user
- Database user on whose behalf the Connection is being madepass
- The database user's passwordUnsupportedOperationException
- always thrown.SQLException
- if a database access error occurspublic String getConnectionFactoryClassName()
Note: This getter only returns the last value set by a call to setConnectionFactoryClassName(String)
.
public List<String> getConnectionInitSqls()
public String[] getConnectionInitSqlsAsArray()
getConnectionInitSqls()
but in an array so it is accessible via JMX.getConnectionInitSqlsAsArray
in interface DataSourceMXBean
getConnectionInitSqlsAsArray()
.protected GenericObjectPool<PoolableConnection> getConnectionPool()
public Boolean getDefaultAutoCommit()
getDefaultAutoCommit
in interface DataSourceMXBean
public String getDefaultCatalog()
getDefaultCatalog
in interface DataSourceMXBean
@Deprecated public Integer getDefaultQueryTimeout()
getDefaultQueryTimeoutDuration()
.Statement
s created from this
connection. null
means that the driver default will be used.public Duration getDefaultQueryTimeoutDuration()
Statement
s created from this
connection. null
means that the driver default will be used.public Boolean getDefaultReadOnly()
getDefaultReadOnly
in interface DataSourceMXBean
public String getDefaultSchema()
getDefaultSchema
in interface DataSourceMXBean
public int getDefaultTransactionIsolation()
getDefaultTransactionIsolation
in interface DataSourceMXBean
Connection.getTransactionIsolation()
public Set<String> getDisconnectionSqlCodes()
setDisconnectionSqlCodes(Collection)
public String[] getDisconnectionSqlCodesAsArray()
getDisconnectionSqlCodes()
but in an array so it is accessible via JMX.getDisconnectionSqlCodesAsArray
in interface DataSourceMXBean
getDisconnectionSqlCodesAsArray()
.public 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 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 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 DataSourceMXBean
@Deprecated public boolean getEnableAutoCommitOnReturn()
getAutoCommitOnReturn()
.Connection.setAutoCommit(true)
if the auto commit
setting is false
when the connection is returned. It is true
by default.public String getEvictionPolicyClassName()
public boolean getFastFailValidation()
getFastFailValidation
in interface DataSourceMXBean
setDisconnectionSqlCodes(Collection)
public int getInitialSize()
getInitialSize
in interface DataSourceMXBean
public String getJmxName()
public boolean getLifo()
getLifo
in interface DataSourceMXBean
public boolean getLogAbandoned()
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 DataSourceMXBean
getLogAbandoned()
.public boolean getLogExpiredConnections()
getMaxConnDuration()
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 DataSourceMXBean
getLogExpiredConnections()
.public int getLoginTimeout() throws SQLException
Gets 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 CommonDataSource
SQLException
- if a database access error occursUnsupportedOperationException
- If the DataSource implementation does not support the login timeout
feature.public PrintWriter getLogWriter() throws SQLException
Calls createDataSource()
, so has the side effect of initializing the connection pool.
getLogWriter
in interface CommonDataSource
SQLException
- if a database access error occurspublic Duration getMaxConnDuration()
getMaxConnDuration
in interface DataSourceMXBean
@Deprecated public long getMaxConnLifetimeMillis()
getMaxConnDuration()
.getMaxConnLifetimeMillis
in interface DataSourceMXBean
getMaxConnLifetimeMillis()
.public int getMaxIdle()
A negative value indicates that there is no limit
getMaxIdle
in interface DataSourceMXBean
public int getMaxOpenPreparedStatements()
maxOpenPreparedStatements
property.getMaxOpenPreparedStatements
in interface DataSourceMXBean
public int getMaxTotal()
A negative number means that there is no limit.
getMaxTotal
in interface DataSourceMXBean
public Duration getMaxWaitDuration()
getMaxWaitDuration
in interface DataSourceMXBean
@Deprecated public long getMaxWaitMillis()
getMaxWaitDuration()
.getMaxWaitMillis
in interface DataSourceMXBean
public Duration getMinEvictableIdleDuration()
getMinEvictableIdleDuration
in interface DataSourceMXBean
setMinEvictableIdle(Duration)
@Deprecated public long getMinEvictableIdleTimeMillis()
getMinEvictableIdleDuration()
.getMinEvictableIdleTimeMillis
in interface DataSourceMXBean
setMinEvictableIdle(Duration)
public int getMinIdle()
getMinIdle
in interface DataSourceMXBean
GenericObjectPool.getMinIdle()
public int getNumActive()
getNumActive
in interface DataSourceMXBean
public int getNumIdle()
getNumIdle
in interface DataSourceMXBean
public int getNumTestsPerEvictionRun()
getNumTestsPerEvictionRun
in interface DataSourceMXBean
setNumTestsPerEvictionRun(int)
public Logger getParentLogger() throws SQLFeatureNotSupportedException
getParentLogger
in interface CommonDataSource
SQLFeatureNotSupportedException
public String getPassword()
getPassword
in interface BasicDataSourceMXBean
protected ObjectName getRegisteredJmxName()
public boolean getRemoveAbandonedOnBorrow()
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 DataSourceMXBean
getRemoveAbandonedOnBorrow()
.getRemoveAbandonedTimeoutDuration()
public boolean getRemoveAbandonedOnMaintenance()
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 DataSourceMXBean
getRemoveAbandonedOnMaintenance()
.getRemoveAbandonedTimeoutDuration()
@Deprecated public int getRemoveAbandonedTimeout()
getRemoveAbandonedTimeoutDuration()
.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 DataSourceMXBean
getRemoveAbandonedTimeout()
.public Duration getRemoveAbandonedTimeoutDuration()
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.
getRemoveAbandonedTimeoutDuration
in interface DataSourceMXBean
public boolean getRollbackOnReturn()
public Duration getSoftMinEvictableIdleDuration()
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.
getSoftMinEvictableIdleDuration
in interface DataSourceMXBean
@Deprecated public long getSoftMinEvictableIdleTimeMillis()
getSoftMinEvictableIdleDuration()
.
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 DataSourceMXBean
public boolean getTestOnBorrow()
getTestOnBorrow
in interface DataSourceMXBean
setTestOnBorrow(boolean)
public boolean getTestOnCreate()
getTestOnCreate
in interface DataSourceMXBean
setTestOnCreate(boolean)
public boolean getTestOnReturn()
setTestOnReturn(boolean)
public boolean getTestWhileIdle()
getTestWhileIdle
in interface DataSourceMXBean
setTestWhileIdle(boolean)
public Duration getDurationBetweenEvictionRuns()
getDurationBetweenEvictionRuns
in interface DataSourceMXBean
setDurationBetweenEvictionRuns(Duration)
@Deprecated public long getTimeBetweenEvictionRunsMillis()
getDurationBetweenEvictionRuns()
.getTimeBetweenEvictionRunsMillis
in interface DataSourceMXBean
setDurationBetweenEvictionRuns(Duration)
public String getUrl()
getUrl
in interface DataSourceMXBean
public String getUsername()
getUsername
in interface DataSourceMXBean
public String getValidationQuery()
getValidationQuery
in interface DataSourceMXBean
setValidationQuery(String)
public Duration getValidationQueryTimeoutDuration()
getValidationQueryTimeoutDuration
in interface DataSourceMXBean
@Deprecated public int getValidationQueryTimeout()
getValidationQueryTimeoutDuration()
.getValidationQueryTimeout
in interface DataSourceMXBean
public void invalidateConnection(Connection connection) throws IllegalStateException
connection
- The Connection to invalidate.IllegalStateException
- if invalidating the connection failed.public boolean isAccessToUnderlyingConnectionAllowed()
isAccessToUnderlyingConnectionAllowed
in interface DataSourceMXBean
public boolean isClearStatementPoolOnReturn()
isClearStatementPoolOnReturn
in interface DataSourceMXBean
public boolean isClosed()
isClosed
in interface DataSourceMXBean
public boolean isPoolPreparedStatements()
isPoolPreparedStatements
in interface DataSourceMXBean
public boolean isWrapperFor(Class<?> iface) throws SQLException
isWrapperFor
in interface Wrapper
SQLException
protected void log(String message)
message
- the message to log.protected void log(String message, Throwable throwable)
message
- TODOthrowable
- the throwable.public void postDeregister()
postDeregister
in interface MBeanRegistration
public void postRegister(Boolean registrationDone)
postRegister
in interface MBeanRegistration
public void preDeregister() throws Exception
preDeregister
in interface MBeanRegistration
Exception
public ObjectName preRegister(MBeanServer server, ObjectName objectName)
preRegister
in interface MBeanRegistration
public void removeConnectionProperty(String name)
name
- Name of the custom connection property to removeaddConnectionProperty(String, String)
public void restart() throws SQLException
This method calls close()
and start()
in sequence within synchronized scope so any
connection requests that come in while the datasource is shutting down will be served by the new pool.
Idle connections that are stored in the connection pool when this method is invoked are closed, but
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. These connections do not count in getMaxTotal()
or
getNumActive()
after invoking this method. For example, if there are 3 connections checked out by
clients when restart()
is invoked, after this method is called, getNumActive()
will
return 0 and up to getMaxTotal()
+ 3 connections may be open until the connections sourced from
the original pool are returned.
The new connection pool created by this method is initialized with currently set configuration properties.
restart
in interface DataSourceMXBean
SQLException
- if an error occurs initializing the datasourcepublic void setAbandonedLogWriter(PrintWriter logWriter)
logWriter
- The new log writerpublic 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 setAccessToUnderlyingConnectionAllowed(boolean allow)
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 void setAutoCommitOnReturn(boolean autoCommitOnReturn)
Connection.setAutoCommit(true)
if the auto commit
setting is false
when the connection is returned. It is true
by default.autoCommitOnReturn
- Whether or not connections being returned to the pool will be checked and configured
with auto-commit.public void setCacheState(boolean cacheState)
cacheState
- The new value for the state caching flagpublic void setClearStatementPoolOnReturn(boolean clearStatementPoolOnReturn)
setPoolPreparedStatements(boolean)
) should
be cleared when the connection is returned to its pool. Default is false.clearStatementPoolOnReturn
- clear or notpublic void setConnectionFactoryClassName(String connectionFactoryClassName)
connectionFactoryClassName
- A class name.public void setConnectionInitSqls(Collection<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 void setConnectionProperties(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 setDefaultAutoCommit(Boolean defaultAutoCommit)
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 void setDefaultCatalog(String defaultCatalog)
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 void setDefaultQueryTimeout(Duration defaultQueryTimeoutDuration)
Statement
s created from this
connection. null
means that the driver default will be used.defaultQueryTimeoutDuration
- The default query timeout Duration.@Deprecated public void setDefaultQueryTimeout(Integer defaultQueryTimeoutSeconds)
setDefaultQueryTimeout(Duration)
.Statement
s created from this
connection. null
means that the driver default will be used.defaultQueryTimeoutSeconds
- The default query timeout in seconds.public void setDefaultReadOnly(Boolean defaultReadOnly)
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 void setDefaultSchema(String defaultSchema)
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.
defaultSchema
- the default catalogpublic void setDefaultTransactionIsolation(int defaultTransactionIsolation)
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 void setDisconnectionSqlCodes(Collection<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 void setDriver(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 void setDriverClassLoader(ClassLoader driverClassLoader)
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 void setDriverClassName(String driverClassName)
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 driver@Deprecated public void setEnableAutoCommitOnReturn(boolean autoCommitOnReturn)
setAutoCommitOnReturn(boolean)
.Connection.setAutoCommit(true)
if the auto commit
setting is false
when the connection is returned. It is true
by default.autoCommitOnReturn
- Whether or not connections being returned to the pool will be checked and configured
with auto-commit.public void setEvictionPolicyClassName(String evictionPolicyClassName)
evictionPolicyClassName
- The fully qualified class name of the EvictionPolicy implementationpublic void setFastFailValidation(boolean fastFailValidation)
fastFailValidation
- true means connections created by this factory will fast fail validationgetFastFailValidation()
public void setInitialSize(int initialSize)
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 void setJmxName(String jmxName)
jmxName
- The JMX name that has been requested for this DataSourcepublic void setLifo(boolean lifo)
lifo
- the new value for the LIFO propertypublic void setLogAbandoned(boolean logAbandoned)
logAbandoned
- new logAbandoned property valuepublic void setLogExpiredConnections(boolean logExpiredConnections)
getMaxConnDuration()
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 void setLoginTimeout(int loginTimeout) throws 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 CommonDataSource
loginTimeout
- The new login timeout, or zero for no timeoutUnsupportedOperationException
- If the DataSource implementation does not support the login timeout
feature.SQLException
- if a database access error occurspublic void setLogWriter(PrintWriter logWriter) throws SQLException
Calls createDataSource()
, so has the side effect of initializing the connection pool.
setLogWriter
in interface CommonDataSource
logWriter
- The new log writerSQLException
- if a database access error occurspublic void setMaxConn(Duration maxConnDuration)
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.
maxConnDuration
- The maximum permitted lifetime of a connection.@Deprecated public void setMaxConnLifetimeMillis(long maxConnLifetimeMillis)
setMaxConn(Duration)
.
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 setMaxIdle(int maxIdle)
maxIdle
- the new value for maxIdlegetMaxIdle()
public void setMaxOpenPreparedStatements(int maxOpenStatements)
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 void setMaxTotal(int maxTotal)
maxTotal
- the new value for maxTotalgetMaxTotal()
public void setMaxWait(Duration maxWaitDuration)
maxWaitDuration
- the new value for MaxWaitMillisgetMaxWaitDuration()
@Deprecated public void setMaxWaitMillis(long maxWaitMillis)
setMaxWait(Duration)
.maxWaitMillis
- the new value for MaxWaitMillisgetMaxWaitDuration()
public void setMinEvictableIdle(Duration minEvictableIdleDuration)
minEvictableIdleDuration
- the minimum amount of time an object may sit idle in the poolsetMinEvictableIdle(Duration)
@Deprecated public void setMinEvictableIdleTimeMillis(long minEvictableIdleTimeMillis)
setMinEvictableIdle(Duration)
.minEvictableIdleTimeMillis
- the minimum amount of time an object may sit idle in the poolsetMinEvictableIdle(Duration)
public void setMinIdle(int minIdle)
minIdle
- the new value for minIdleGenericObjectPool.setMinIdle(int)
public void setNumTestsPerEvictionRun(int numTestsPerEvictionRun)
numTestsPerEvictionRun
- the new {code numTestsPerEvictionRun} valuesetNumTestsPerEvictionRun(int)
public void setPassword(String 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 void setPoolPreparedStatements(boolean poolingStatements)
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 void setRemoveAbandonedOnBorrow(boolean removeAbandonedOnBorrow)
removeAbandonedOnBorrow
- true means abandoned connections may be removed when connections are borrowed from
the pool.getRemoveAbandonedOnBorrow()
public void setRemoveAbandonedOnMaintenance(boolean removeAbandonedOnMaintenance)
removeAbandonedOnMaintenance
- true means abandoned connections may be removed on pool maintenance.getRemoveAbandonedOnMaintenance()
public void setRemoveAbandonedTimeout(Duration removeAbandonedTimeout)
Setting this property has no effect if getRemoveAbandonedOnBorrow()
and
{code getRemoveAbandonedOnMaintenance()} are false.
removeAbandonedTimeout
- new abandoned timeoutgetRemoveAbandonedTimeoutDuration()
,
getRemoveAbandonedOnBorrow()
,
getRemoveAbandonedOnMaintenance()
@Deprecated public void setRemoveAbandonedTimeout(int removeAbandonedTimeout)
setRemoveAbandonedTimeout(Duration)
.
Setting this property has no effect if getRemoveAbandonedOnBorrow()
and
getRemoveAbandonedOnMaintenance()
are false.
removeAbandonedTimeout
- new abandoned timeout in secondsgetRemoveAbandonedTimeoutDuration()
,
getRemoveAbandonedOnBorrow()
,
getRemoveAbandonedOnMaintenance()
public void setRollbackOnReturn(boolean rollbackOnReturn)
rollbackOnReturn
- whether a connection will be rolled back when it is returned to the pool.public void setSoftMinEvictableIdle(Duration 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()
@Deprecated public void setSoftMinEvictableIdleTimeMillis(long softMinEvictableIdleTimeMillis)
setSoftMinEvictableIdle(Duration)
.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 void setTestOnBorrow(boolean testOnBorrow)
testOnBorrow
- new value for testOnBorrow propertypublic void setTestOnCreate(boolean testOnCreate)
testOnCreate
- new value for testOnCreate propertypublic 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 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 void setDurationBetweenEvictionRuns(Duration timeBetweenEvictionRunsMillis)
timeBetweenEvictionRunsMillis
- the new time between evictor runssetDurationBetweenEvictionRuns(Duration)
@Deprecated public void setTimeBetweenEvictionRunsMillis(long timeBetweenEvictionRunsMillis)
setDurationBetweenEvictionRuns(Duration)
.timeBetweenEvictionRunsMillis
- the new time between evictor runssetDurationBetweenEvictionRuns(Duration)
public void setUrl(String 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 void setUsername(String 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 void setValidationQuery(String 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 void setValidationQueryTimeout(Duration validationQueryTimeoutDuration)
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.
validationQueryTimeoutDuration
- new validation query timeout value in seconds@Deprecated public void setValidationQueryTimeout(int validationQueryTimeoutSeconds)
setValidationQueryTimeout(Duration)
.
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 void start() throws SQLException
It is not necessary to call this method before using a newly created BasicDataSource instance, but
calling it in that context causes the datasource to be immediately initialized (instead of waiting for
the first getConnection()
request). Its primary use is to restart and reinitialize a
datasource that has been closed.
When this method is called after close()
, connections checked out by clients
before the datasource was stopped do not count in getMaxTotal()
or getNumActive()
.
For example, if there are 3 connections checked out by clients when close()
is invoked and they are
not returned before start()
is invoked, after this method is called, getNumActive()
will
return 0. These connections will be physically closed when they are returned, but they will not count against
the maximum allowed in the newly started datasource.
start
in interface DataSourceMXBean
SQLException
- if an error occurs initializing the datasourceprotected void startPoolMaintenance()
public <T> T unwrap(Class<T> iface) throws SQLException
unwrap
in interface Wrapper
SQLException
Copyright © 2000-2022 Apache Software Foundation. All Rights Reserved.