Class BasicDataSource
- java.lang.Object
-
- org.apache.commons.dbcp.BasicDataSource
-
- All Implemented Interfaces:
java.sql.Wrapper,javax.sql.CommonDataSource,javax.sql.DataSource
- Direct Known Subclasses:
BasicManagedDataSource
public class BasicDataSource extends java.lang.Object implements javax.sql.DataSourceBasic implementation of
javax.sql.DataSourcethat is configured via JavaBeans properties. This is not the only way to combine the commons-dbcp and commons-pool packages, but provides a "one stop shopping" solution for basic requirements.Users extending this class should take care to use appropriate accessors rather than accessing protected fields directly to ensure thread-safety.
- Version:
- $Revision: 895844 $ $Date: 2010-01-04 20:50:04 -0500 (Mon, 04 Jan 2010) $
- Author:
- Glenn L. Nielsen, Craig R. McClanahan, Dirk Verbeeck
-
-
Field Summary
Fields Modifier and Type Field Description protected booleanclosedprotected java.util.ListconnectionInitSqlsThese SQL statements run once after a Connection is created.protected org.apache.commons.pool.impl.GenericObjectPoolconnectionPoolThe object pool that internally manages our connections.protected java.util.PropertiesconnectionPropertiesThe connection properties that will be sent to our JDBC driver when establishing new connections.protected javax.sql.DataSourcedataSourceThe data source we will use to manage connections.protected booleandefaultAutoCommitThe default auto-commit state of connections created by this pool.protected java.lang.StringdefaultCatalogThe default "catalog" of connections created by this pool.protected java.lang.BooleandefaultReadOnlyThe default read-only state of connections created by this pool.protected intdefaultTransactionIsolationThe default TransactionIsolation state of connections created by this pool.protected java.lang.ClassLoaderdriverClassLoaderThe class loader instance to use to load the JDBC driver.protected java.lang.StringdriverClassNameThe fully qualified Java class name of the JDBC driver to be used.protected intinitialSizeThe initial number of connections that are created when the pool is started.protected java.io.PrintWriterlogWriterThe PrintWriter to which log messages should be directed.protected intmaxActiveThe maximum number of active connections that can be allocated from this pool at the same time, or negative for no limit.protected intmaxIdleThe maximum number of connections that can remain idle in the pool, without extra ones being released, or negative for no limit.protected intmaxOpenPreparedStatementsThe maximum number of open statements that can be allocated from the statement pool at the same time, or non-positive for no limit.protected longmaxWaitThe maximum number of milliseconds that the pool will wait (when there are no available connections) for a connection to be returned before throwing an exception, or <= 0 to wait indefinitely.protected longminEvictableIdleTimeMillisThe minimum amount of time an object may sit idle in the pool before it is eligable for eviction by the idle object evictor (if any).protected intminIdleThe minimum number of active connections that can remain idle in the pool, without extra ones being created, or 0 to create none.protected intnumTestsPerEvictionRunThe number of objects to examine during each run of the idle object evictor thread (if any).protected java.lang.StringpasswordThe connection password to be passed to our JDBC driver to establish a connection.protected booleanpoolPreparedStatementsPrepared statement pooling for this pool.protected booleantestOnBorrowThe indication of whether objects will be validated before being borrowed from the pool.protected booleantestOnReturnThe indication of whether objects will be validated before being returned to the pool.protected booleantestWhileIdleThe indication of whether objects will be validated by the idle object evictor (if any).protected longtimeBetweenEvictionRunsMillisThe number of milliseconds to sleep between runs of the idle object evictor thread.protected java.lang.StringurlThe connection URL to be passed to our JDBC driver to establish a connection.protected java.lang.StringusernameThe connection username to be passed to our JDBC driver to establish a connection.protected java.lang.StringvalidationQueryThe SQL query that will be used to validate connections from this pool before returning them to the caller.protected intvalidationQueryTimeoutTimeout in seconds before connection validation queries fail.
-
Constructor Summary
Constructors Constructor Description BasicDataSource()
-
Method Summary
All Methods Static Methods Instance Methods Concrete Methods Modifier and Type Method Description voidaddConnectionProperty(java.lang.String name, java.lang.String value)Add a custom connection property to the set that will be passed to our JDBC driver.voidclose()Closes and releases all idle connections that are currently stored in the connection pool associated with this data source.protected ConnectionFactorycreateConnectionFactory()Creates a JDBC connection factory for this datasource.protected voidcreateConnectionPool()Creates a connection pool for this datasource.protected javax.sql.DataSourcecreateDataSource()Create (if necessary) and return the internal data source we are using to manage our connections.protected voidcreateDataSourceInstance()Creates the actual data source instance.protected voidcreatePoolableConnectionFactory(ConnectionFactory driverConnectionFactory, org.apache.commons.pool.KeyedObjectPoolFactory statementPoolFactory, AbandonedConfig configuration)Creates the PoolableConnectionFactory and attaches it to the connection pool.java.sql.ConnectiongetConnection()Create (if necessary) and return a connection to the database.java.sql.ConnectiongetConnection(java.lang.String user, java.lang.String pass)BasicDataSource does NOT support this method.java.util.CollectiongetConnectionInitSqls()Returns the list of SQL statements executed when a physical connection is first created.booleangetDefaultAutoCommit()Returns the default auto-commit property.java.lang.StringgetDefaultCatalog()Returns the default catalog.booleangetDefaultReadOnly()Returns the default readOnly property.intgetDefaultTransactionIsolation()Returns the default transaction isolation state of returned connections.java.lang.ClassLoadergetDriverClassLoader()Returns the class loader specified for loading the JDBC driver.java.lang.StringgetDriverClassName()Returns the jdbc driver class name.intgetInitialSize()Returns the initial size of the connection pool.booleangetLogAbandoned()Flag to log stack traces for application code which abandoned a Statement or Connection.intgetLoginTimeout()BasicDataSource does NOT support this method.java.io.PrintWritergetLogWriter()Returns the log writer being used by this data source.intgetMaxActive()Returns the maximum number of active connections that can be allocated at the same time.intgetMaxIdle()Returns the maximum number of connections that can remain idle in the pool.intgetMaxOpenPreparedStatements()Gets the value of themaxOpenPreparedStatementsproperty.longgetMaxWait()Returns the maximum number of milliseconds that the pool will wait for a connection to be returned before throwing an exception.longgetMinEvictableIdleTimeMillis()Returns theminEvictableIdleTimeMillisproperty.intgetMinIdle()Returns the minimum number of idle connections in the poolintgetNumActive()[Read Only] The current number of active connections that have been allocated from this data source.intgetNumIdle()[Read Only] The current number of idle connections that are waiting to be allocated from this data source.intgetNumTestsPerEvictionRun()Returns the value of thenumTestsPerEvictionRunproperty.java.util.logging.LoggergetParentLogger()java.lang.StringgetPassword()Returns the password passed to the JDBC driver to establish connections.booleangetRemoveAbandoned()Flag to remove abandoned connections if they exceed the removeAbandonedTimout.intgetRemoveAbandonedTimeout()Timeout in seconds before an abandoned connection can be removed.booleangetTestOnBorrow()Returns thetestOnBorrowproperty.booleangetTestOnReturn()Returns the value of thetestOnReturnproperty.booleangetTestWhileIdle()Returns the value of thetestWhileIdleproperty.longgetTimeBetweenEvictionRunsMillis()Returns the value of thetimeBetweenEvictionRunsMillisproperty.java.lang.StringgetUrl()Returns the JDBC connectionurlproperty.java.lang.StringgetUsername()Returns the JDBC connectionusernameproperty.java.lang.StringgetValidationQuery()Returns the validation query used to validate connections before returning them.intgetValidationQueryTimeout()Returns the validation query timeout.booleanisAccessToUnderlyingConnectionAllowed()Returns the value of the accessToUnderlyingConnectionAllowed property.booleanisClosed()If true, this data source is closed and no more connections can be retrieved from this datasource.booleanisPoolPreparedStatements()Returns true if we are pooling statements.booleanisWrapperFor(java.lang.Class<?> iface)protected voidlog(java.lang.String message)voidremoveConnectionProperty(java.lang.String name)Remove a custom connection property.voidsetAccessToUnderlyingConnectionAllowed(boolean allow)Sets the value of the accessToUnderlyingConnectionAllowed property.voidsetConnectionInitSqls(java.util.Collection connectionInitSqls)Sets the list of SQL statements to be executed when a physical connection is first created.voidsetConnectionProperties(java.lang.String connectionProperties)Sets the connection properties passed to driver.connect(...).voidsetDefaultAutoCommit(boolean defaultAutoCommit)Sets default auto-commit state of connections returned by this datasource.voidsetDefaultCatalog(java.lang.String defaultCatalog)Sets the default catalog.voidsetDefaultReadOnly(boolean defaultReadOnly)Sets defaultReadonly property.voidsetDefaultTransactionIsolation(int defaultTransactionIsolation)Sets the default transaction isolation state for returned connections.voidsetDriverClassLoader(java.lang.ClassLoader driverClassLoader)Sets the class loader to be used to load the JDBC driver.voidsetDriverClassName(java.lang.String driverClassName)Sets the jdbc driver class name.voidsetInitialSize(int initialSize)Sets the initial size of the connection pool.voidsetLogAbandoned(boolean logAbandoned)voidsetLoginTimeout(int loginTimeout)BasicDataSource does NOT support this method.voidsetLogWriter(java.io.PrintWriter logWriter)Sets the log writer being used by this data source.voidsetMaxActive(int maxActive)Sets the maximum number of active connections that can be allocated at the same time.voidsetMaxIdle(int maxIdle)Sets the maximum number of connections that can remain idle in the pool.voidsetMaxOpenPreparedStatements(int maxOpenStatements)Sets the value of themaxOpenPreparedStatementsproperty.voidsetMaxWait(long maxWait)Sets the maxWait property.voidsetMinEvictableIdleTimeMillis(long minEvictableIdleTimeMillis)Sets theminEvictableIdleTimeMillisproperty.voidsetMinIdle(int minIdle)Sets the minimum number of idle connections in the pool.voidsetNumTestsPerEvictionRun(int numTestsPerEvictionRun)Sets the value of thenumTestsPerEvictionRunproperty.voidsetPassword(java.lang.String password)Sets thepassword.voidsetPoolPreparedStatements(boolean poolingStatements)Sets whether to pool statements or not.voidsetRemoveAbandoned(boolean removeAbandoned)voidsetRemoveAbandonedTimeout(int removeAbandonedTimeout)voidsetTestOnBorrow(boolean testOnBorrow)Sets thetestOnBorrowproperty.voidsetTestOnReturn(boolean testOnReturn)Sets thetestOnReturnproperty.voidsetTestWhileIdle(boolean testWhileIdle)Sets thetestWhileIdleproperty.voidsetTimeBetweenEvictionRunsMillis(long timeBetweenEvictionRunsMillis)Sets thetimeBetweenEvictionRunsMillisproperty.voidsetUrl(java.lang.String url)Sets theurl.voidsetUsername(java.lang.String username)Sets theusername.voidsetValidationQuery(java.lang.String validationQuery)Sets thevalidationQuery.voidsetValidationQueryTimeout(int timeout)Sets the validation query timeout, the amount of time, in seconds, that connection validation will wait for a response from the database when executing a validation query.<T> Tunwrap(java.lang.Class<T> iface)protected static voidvalidateConnectionFactory(PoolableConnectionFactory connectionFactory)
-
-
-
Field Detail
-
defaultAutoCommit
protected volatile boolean defaultAutoCommit
The default auto-commit state of connections created by this pool.
-
defaultReadOnly
protected transient java.lang.Boolean defaultReadOnly
The default read-only state of connections created by this pool.
-
defaultTransactionIsolation
protected volatile int defaultTransactionIsolation
The default TransactionIsolation state of connections created by this pool.
-
defaultCatalog
protected volatile java.lang.String defaultCatalog
The default "catalog" of connections created by this pool.
-
driverClassName
protected java.lang.String driverClassName
The fully qualified Java class name of the JDBC driver to be used.
-
driverClassLoader
protected java.lang.ClassLoader driverClassLoader
The class loader instance to use to load the JDBC driver. If not specified,Class.forName(String)is used to load the JDBC driver. If specified,Class.forName(String, boolean, ClassLoader)is used.
-
maxActive
protected int maxActive
The maximum number of active connections that can be allocated from this pool at the same time, or negative for no limit.
-
maxIdle
protected int maxIdle
The maximum number of connections that can remain idle in the pool, without extra ones being released, or negative for no limit. If maxIdle is set too low on heavily loaded systems it is possible you will see connections being closed and almost immediately new connections being opened. This is a result of the active threads momentarily closing connections faster than they are opening them, causing the number of idle connections to rise above maxIdle. The best value for maxIdle for heavily loaded system will vary but the default is a good starting point.
-
minIdle
protected int minIdle
The minimum number of active connections that can remain idle in the pool, without extra ones being created, or 0 to create none.
-
initialSize
protected int initialSize
The initial number of connections that are created when the pool is started.- Since:
- 1.2
-
maxWait
protected long maxWait
The maximum number of milliseconds that the pool will wait (when there are no available connections) for a connection to be returned before throwing an exception, or <= 0 to wait indefinitely.
-
poolPreparedStatements
protected boolean poolPreparedStatements
Prepared statement pooling for this pool. When this property is set totrueboth PreparedStatements and CallableStatements are pooled.
-
maxOpenPreparedStatements
protected int maxOpenPreparedStatements
The maximum number of open statements that can be allocated from the statement pool at the same time, or non-positive for no limit. Since a connection usually only uses one or two statements at a time, this is mostly used to help detect resource leaks.
Note: As of version 1.3, CallableStatements (those produced by
Connection.prepareCall(java.lang.String)) are pooled along with PreparedStatements (produced byConnection.prepareStatement(java.lang.String)) andmaxOpenPreparedStatementslimits the total number of prepared or callable statements that may be in use at a given time.
-
testOnBorrow
protected boolean testOnBorrow
The indication of whether objects will be validated before being borrowed from the pool. If the object fails to validate, it will be dropped from the pool, and we will attempt to borrow another.
-
testOnReturn
protected boolean testOnReturn
The indication of whether objects will be validated before being returned to the pool.
-
timeBetweenEvictionRunsMillis
protected long timeBetweenEvictionRunsMillis
The number of milliseconds to sleep between runs of the idle object evictor thread. When non-positive, no idle object evictor thread will be run.
-
numTestsPerEvictionRun
protected int numTestsPerEvictionRun
The number of objects to examine during each run of the idle object evictor thread (if any).
-
minEvictableIdleTimeMillis
protected long minEvictableIdleTimeMillis
The minimum amount of time an object may sit idle in the pool before it is eligable for eviction by the idle object evictor (if any).
-
testWhileIdle
protected boolean testWhileIdle
The indication of whether objects will be validated by the idle object evictor (if any). If an object fails to validate, it will be dropped from the pool.
-
password
protected volatile java.lang.String password
The connection password to be passed to our JDBC driver to establish a connection.
-
url
protected java.lang.String url
The connection URL to be passed to our JDBC driver to establish a connection.
-
username
protected java.lang.String username
The connection username to be passed to our JDBC driver to establish a connection.
-
validationQuery
protected volatile java.lang.String validationQuery
The SQL query that will be used to validate connections from this pool before returning them to the caller. If specified, this query MUST be an SQL SELECT statement that returns at least one row.
-
validationQueryTimeout
protected volatile int validationQueryTimeout
Timeout in seconds before connection validation queries fail.- Since:
- 1.3
-
connectionInitSqls
protected volatile java.util.List connectionInitSqls
These SQL statements run once after a Connection is created.This property can be used for example to run ALTER SESSION SET NLS_SORT=XCYECH in an Oracle Database only once after connection creation.
- Since:
- 1.3
-
connectionPool
protected volatile org.apache.commons.pool.impl.GenericObjectPool connectionPool
The object pool that internally manages our connections.
-
connectionProperties
protected java.util.Properties connectionProperties
The connection properties that will be sent to our JDBC driver when establishing new connections. NOTE - The "user" and "password" properties will be passed explicitly, so they do not need to be included here.
-
dataSource
protected volatile javax.sql.DataSource dataSource
The data source we will use to manage connections. This object should be acquired ONLY by calls to thecreateDataSource()method.
-
logWriter
protected java.io.PrintWriter logWriter
The PrintWriter to which log messages should be directed.
-
closed
protected boolean closed
-
-
Method Detail
-
getDefaultAutoCommit
public boolean getDefaultAutoCommit()
Returns the default auto-commit property.- Returns:
- true if default auto-commit is enabled
-
setDefaultAutoCommit
public void setDefaultAutoCommit(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.- Parameters:
defaultAutoCommit- default auto-commit value
-
getDefaultReadOnly
public boolean getDefaultReadOnly()
Returns the default readOnly property.- Returns:
- true if connections are readOnly by default
-
setDefaultReadOnly
public void setDefaultReadOnly(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.- Parameters:
defaultReadOnly- default read-only value
-
getDefaultTransactionIsolation
public int getDefaultTransactionIsolation()
Returns the default transaction isolation state of returned connections.- Returns:
- the default value for transaction isolation state
- See Also:
Connection.getTransactionIsolation()
-
setDefaultTransactionIsolation
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.- Parameters:
defaultTransactionIsolation- the default transaction isolation state- See Also:
Connection.getTransactionIsolation()
-
getDefaultCatalog
public java.lang.String getDefaultCatalog()
Returns the default catalog.- Returns:
- the default catalog
-
setDefaultCatalog
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.- Parameters:
defaultCatalog- the default catalog
-
getDriverClassName
public java.lang.String getDriverClassName()
Returns the jdbc driver class name.- Returns:
- the jdbc driver class name
-
setDriverClassName
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.- Parameters:
driverClassName- the class name of the jdbc driver
-
getDriverClassLoader
public java.lang.ClassLoader getDriverClassLoader()
Returns the class loader specified for loading the JDBC driver. Returnsnullif no class loader has been explicitly specified.
-
setDriverClassLoader
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.- Parameters:
driverClassLoader- the class loader with which to load the JDBC driver
-
getMaxActive
public int getMaxActive()
Returns the maximum number of active connections that can be allocated at the same time.
A negative number means that there is no limit.
- Returns:
- the maximum number of active connections
-
setMaxActive
public void setMaxActive(int maxActive)
Sets the maximum number of active connections that can be allocated at the same time. Use a negative value for no limit.- Parameters:
maxActive- the new value for maxActive- See Also:
getMaxActive()
-
getMaxIdle
public int getMaxIdle()
Returns the maximum number of connections that can remain idle in the pool.
A negative value indicates that there is no limit
- Returns:
- the maximum number of idle connections
-
setMaxIdle
public void setMaxIdle(int maxIdle)
Sets the maximum number of connections that can remain idle in the pool.- Parameters:
maxIdle- the new value for maxIdle- See Also:
getMaxIdle()
-
getMinIdle
public int getMinIdle()
Returns the minimum number of idle connections in the pool- Returns:
- the minimum number of idle connections
- See Also:
GenericObjectPool.getMinIdle()
-
setMinIdle
public void setMinIdle(int minIdle)
Sets the minimum number of idle connections in the pool.- Parameters:
minIdle- the new value for minIdle- See Also:
GenericObjectPool.setMinIdle(int)
-
getInitialSize
public int getInitialSize()
Returns the initial size of the connection pool.- Returns:
- the number of connections created when the pool is initialized
-
setInitialSize
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.- Parameters:
initialSize- the number of connections created when the pool is initialized
-
getMaxWait
public long getMaxWait()
Returns the maximum number of milliseconds that the pool will wait for a connection to be returned before throwing an exception.
A value less than or equal to zero means the pool is set to wait indefinitely.
- Returns:
- the maxWait property value
-
setMaxWait
public void setMaxWait(long maxWait)
Sets the maxWait property.
Use -1 to make the pool wait indefinitely.
- Parameters:
maxWait- the new value for maxWait- See Also:
getMaxWait()
-
isPoolPreparedStatements
public boolean isPoolPreparedStatements()
Returns true if we are pooling statements.- Returns:
- true if prepared and callable statements are pooled
-
setPoolPreparedStatements
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.- Parameters:
poolingStatements- pooling on or off
-
getMaxOpenPreparedStatements
public int getMaxOpenPreparedStatements()
Gets the value of themaxOpenPreparedStatementsproperty.- Returns:
- the maximum number of open statements
- See Also:
maxOpenPreparedStatements
-
setMaxOpenPreparedStatements
public void setMaxOpenPreparedStatements(int maxOpenStatements)
Sets the value of the
maxOpenPreparedStatementsproperty.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.- Parameters:
maxOpenStatements- the new maximum number of prepared statements- See Also:
maxOpenPreparedStatements
-
getTestOnBorrow
public boolean getTestOnBorrow()
Returns thetestOnBorrowproperty.- Returns:
- true if objects are validated before being borrowed from the pool
- See Also:
testOnBorrow
-
setTestOnBorrow
public void setTestOnBorrow(boolean testOnBorrow)
Sets thetestOnBorrowproperty. This property determines whether or not the pool will validate objects before they are borrowed from the pool. For atruevalue to have any effect, thevalidationQueryproperty must be set to a non-null string.- Parameters:
testOnBorrow- new value for testOnBorrow property
-
getTestOnReturn
public boolean getTestOnReturn()
Returns the value of thetestOnReturnproperty.- Returns:
- true if objects are validated before being returned to the pool
- See Also:
testOnReturn
-
setTestOnReturn
public void setTestOnReturn(boolean testOnReturn)
Sets thetestOnReturnproperty. This property determines whether or not the pool will validate objects before they are returned to the pool. For atruevalue to have any effect, thevalidationQueryproperty must be set to a non-null string.- Parameters:
testOnReturn- new value for testOnReturn property
-
getTimeBetweenEvictionRunsMillis
public long getTimeBetweenEvictionRunsMillis()
Returns the value of thetimeBetweenEvictionRunsMillisproperty.- Returns:
- the time (in miliseconds) between evictor runs
- See Also:
timeBetweenEvictionRunsMillis
-
setTimeBetweenEvictionRunsMillis
public void setTimeBetweenEvictionRunsMillis(long timeBetweenEvictionRunsMillis)
Sets thetimeBetweenEvictionRunsMillisproperty.- Parameters:
timeBetweenEvictionRunsMillis- the new time between evictor runs- See Also:
timeBetweenEvictionRunsMillis
-
getNumTestsPerEvictionRun
public int getNumTestsPerEvictionRun()
Returns the value of thenumTestsPerEvictionRunproperty.- Returns:
- the number of objects to examine during idle object evictor runs
- See Also:
numTestsPerEvictionRun
-
setNumTestsPerEvictionRun
public void setNumTestsPerEvictionRun(int numTestsPerEvictionRun)
Sets the value of thenumTestsPerEvictionRunproperty.- Parameters:
numTestsPerEvictionRun- the newnumTestsPerEvictionRunvalue- See Also:
numTestsPerEvictionRun
-
getMinEvictableIdleTimeMillis
public long getMinEvictableIdleTimeMillis()
Returns theminEvictableIdleTimeMillisproperty.- Returns:
- the value of the
minEvictableIdleTimeMillisproperty - See Also:
minEvictableIdleTimeMillis
-
setMinEvictableIdleTimeMillis
public void setMinEvictableIdleTimeMillis(long minEvictableIdleTimeMillis)
Sets theminEvictableIdleTimeMillisproperty.- Parameters:
minEvictableIdleTimeMillis- the minimum amount of time an object may sit idle in the pool- See Also:
minEvictableIdleTimeMillis
-
getTestWhileIdle
public boolean getTestWhileIdle()
Returns the value of thetestWhileIdleproperty.- Returns:
- true if objects examined by the idle object evictor are validated
- See Also:
testWhileIdle
-
setTestWhileIdle
public void setTestWhileIdle(boolean testWhileIdle)
Sets thetestWhileIdleproperty. This property determines whether or not the idle object evictor will validate connections. For atruevalue to have any effect, thevalidationQueryproperty must be set to a non-null string.- Parameters:
testWhileIdle- new value for testWhileIdle property
-
getNumActive
public int getNumActive()
[Read Only] The current number of active connections that have been allocated from this data source.- Returns:
- the current number of active connections
-
getNumIdle
public int getNumIdle()
[Read Only] The current number of idle connections that are waiting to be allocated from this data source.- Returns:
- the current number of idle connections
-
getPassword
public java.lang.String getPassword()
Returns the password passed to the JDBC driver to establish connections.- Returns:
- the connection password
-
setPassword
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.- Parameters:
password- new value for the password
-
getUrl
public java.lang.String getUrl()
Returns the JDBC connectionurlproperty.- Returns:
- the
urlpassed to the JDBC driver to establish connections
-
setUrl
public 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.- Parameters:
url- the new value for the JDBC connection url
-
getUsername
public java.lang.String getUsername()
Returns the JDBC connectionusernameproperty.- Returns:
- the
usernamepassed to the JDBC driver to establish connections
-
setUsername
public 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.- Parameters:
username- the new value for the JDBC connection username
-
getValidationQuery
public java.lang.String getValidationQuery()
Returns the validation query used to validate connections before returning them.- Returns:
- the SQL validation query
- See Also:
validationQuery
-
setValidationQuery
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.- Parameters:
validationQuery- the new value for the validation query
-
getValidationQueryTimeout
public int getValidationQueryTimeout()
Returns the validation query timeout.- Returns:
- the timeout in seconds before connection validation queries fail.
- Since:
- 1.3
-
setValidationQueryTimeout
public void setValidationQueryTimeout(int timeout)
Sets the validation query timeout, the amount of time, in seconds, that connection validation will wait for a response from the database when executing a validation query. Use a value less than or equal to 0 for no timeout.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.- Parameters:
timeout- new validation query timeout value in seconds- Since:
- 1.3
-
getConnectionInitSqls
public java.util.Collection getConnectionInitSqls()
Returns the list of SQL statements executed when a physical connection is first created. Returns an empty list if there are no initialization statements configured.- Returns:
- initialization SQL statements
- Since:
- 1.3
-
setConnectionInitSqls
public void setConnectionInitSqls(java.util.Collection connectionInitSqls)
Sets the list of SQL statements to be executed when a physical connection is first created.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.- Parameters:
connectionInitSqls- Collection of SQL statements to execute on connection creation
-
isAccessToUnderlyingConnectionAllowed
public boolean isAccessToUnderlyingConnectionAllowed()
Returns the value of the accessToUnderlyingConnectionAllowed property.- Returns:
- true if access to the underlying connection is allowed, false otherwise.
-
setAccessToUnderlyingConnectionAllowed
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.- Parameters:
allow- Access to the underlying connection is granted when true.
-
getConnection
public java.sql.Connection getConnection() throws java.sql.SQLExceptionCreate (if necessary) and return a connection to the database.- Specified by:
getConnectionin interfacejavax.sql.DataSource- Returns:
- a database connection
- Throws:
java.sql.SQLException- if a database access error occurs
-
getConnection
public java.sql.Connection getConnection(java.lang.String user, java.lang.String pass) throws java.sql.SQLExceptionBasicDataSource does NOT support this method.- Specified by:
getConnectionin interfacejavax.sql.DataSource- Parameters:
user- Database user on whose behalf the Connection is being madepass- The database user's password- Returns:
- nothing - always throws UnsupportedOperationException
- Throws:
java.lang.UnsupportedOperationExceptionjava.sql.SQLException- if a database access error occurs
-
getLoginTimeout
public int getLoginTimeout() throws java.sql.SQLExceptionBasicDataSource does NOT support this method.Returns the login timeout (in seconds) for connecting to the database.
Calls
createDataSource(), so has the side effect of initializing the connection pool.- Specified by:
getLoginTimeoutin interfacejavax.sql.CommonDataSource- Specified by:
getLoginTimeoutin interfacejavax.sql.DataSource- Returns:
- login timeout in seconds
- Throws:
java.sql.SQLException- if a database access error occursjava.lang.UnsupportedOperationException- If the DataSource implementation does not support the login timeout feature.
-
getLogWriter
public java.io.PrintWriter getLogWriter() throws java.sql.SQLExceptionReturns the log writer being used by this data source.
Calls
createDataSource(), so has the side effect of initializing the connection pool.- Specified by:
getLogWriterin interfacejavax.sql.CommonDataSource- Specified by:
getLogWriterin interfacejavax.sql.DataSource- Returns:
- log writer in use
- Throws:
java.sql.SQLException- if a database access error occurs
-
setLoginTimeout
public void setLoginTimeout(int loginTimeout) throws java.sql.SQLExceptionBasicDataSource does NOT support this method.Set the login timeout (in seconds) for connecting to the database.
Calls
createDataSource(), so has the side effect of initializing the connection pool.- Specified by:
setLoginTimeoutin interfacejavax.sql.CommonDataSource- Specified by:
setLoginTimeoutin interfacejavax.sql.DataSource- Parameters:
loginTimeout- The new login timeout, or zero for no timeout- Throws:
java.lang.UnsupportedOperationException- If the DataSource implementation does not support the login timeout feature.java.sql.SQLException- if a database access error occurs
-
setLogWriter
public void setLogWriter(java.io.PrintWriter logWriter) throws java.sql.SQLExceptionSets the log writer being used by this data source.
Calls
createDataSource(), so has the side effect of initializing the connection pool.- Specified by:
setLogWriterin interfacejavax.sql.CommonDataSource- Specified by:
setLogWriterin interfacejavax.sql.DataSource- Parameters:
logWriter- The new log writer- Throws:
java.sql.SQLException- if a database access error occurs
-
getRemoveAbandoned
public boolean getRemoveAbandoned()
Flag to remove abandoned connections if they exceed the removeAbandonedTimout. Set to true or false, default false. If set to true a connection is considered abandoned and eligible for removal if it has been idle longer than the removeAbandonedTimeout. Setting this to true can recover db connections from poorly written applications which fail to close a connection.Abandonded connections are identified and removed when
getConnection()is invoked and the following conditions holdgetRemoveAbandoned()= truegetNumActive()>getMaxActive()- 3getNumIdle()< 2
-
setRemoveAbandoned
public void setRemoveAbandoned(boolean removeAbandoned)
- Parameters:
removeAbandoned- new removeAbandoned property value- See Also:
getRemoveAbandoned()
-
getRemoveAbandonedTimeout
public int getRemoveAbandonedTimeout()
Timeout in seconds before an abandoned connection can be removed. Defaults to 300 seconds.- Returns:
- abandoned connection timeout
-
setRemoveAbandonedTimeout
public void setRemoveAbandonedTimeout(int removeAbandonedTimeout)
- Parameters:
removeAbandonedTimeout- new removeAbandonedTimeout value
-
getLogAbandoned
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.
-
setLogAbandoned
public void setLogAbandoned(boolean logAbandoned)
- Parameters:
logAbandoned- new logAbandoned property value
-
addConnectionProperty
public void addConnectionProperty(java.lang.String name, java.lang.String value)Add a custom connection property to the set that will be passed to our JDBC driver. This MUST be called before the first connection is retrieved (along with all the other configuration property setters). Calls to this method after the connection pool has been initialized have no effect.- Parameters:
name- Name of the custom connection propertyvalue- Value of the custom connection property
-
removeConnectionProperty
public void removeConnectionProperty(java.lang.String name)
Remove a custom connection property.- Parameters:
name- Name of the custom connection property to remove- See Also:
addConnectionProperty(String, String)
-
setConnectionProperties
public void setConnectionProperties(java.lang.String connectionProperties)
Sets the connection properties passed to driver.connect(...). 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.- Parameters:
connectionProperties- the connection properties used to create new connections
-
close
public void close() throws java.sql.SQLExceptionCloses 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.
- Throws:
java.sql.SQLException- if an error occurs closing idle connections
-
isClosed
public boolean isClosed()
If true, this data source is closed and no more connections can be retrieved from this datasource.- Returns:
- true, if the data source is closed; false otherwise
-
isWrapperFor
public boolean isWrapperFor(java.lang.Class<?> iface) throws java.sql.SQLException- Specified by:
isWrapperForin interfacejava.sql.Wrapper- Throws:
java.sql.SQLException
-
unwrap
public <T> T unwrap(java.lang.Class<T> iface) throws java.sql.SQLException- Specified by:
unwrapin interfacejava.sql.Wrapper- Throws:
java.sql.SQLException
-
createDataSource
protected javax.sql.DataSource createDataSource() throws java.sql.SQLExceptionCreate (if necessary) and return the internal data source we are using to manage our connections.
IMPLEMENTATION NOTE - It is tempting to use the "double checked locking" idiom in an attempt to avoid synchronizing on every single call to this method. However, this idiom fails to work correctly in the face of some optimizations that are legal for a JVM to perform.
- Throws:
java.sql.SQLException- if the object pool cannot be created.
-
createConnectionFactory
protected ConnectionFactory createConnectionFactory() throws java.sql.SQLException
Creates a JDBC connection factory for this datasource. This method only exists so subclasses can replace the implementation class.- Throws:
java.sql.SQLException
-
createConnectionPool
protected void createConnectionPool()
Creates a connection pool for this datasource. This method only exists so subclasses can replace the implementation class.
-
createDataSourceInstance
protected void createDataSourceInstance() throws java.sql.SQLExceptionCreates the actual data source instance. This method only exists so subclasses can replace the implementation class.- Throws:
java.sql.SQLException- if unable to create a datasource instance
-
createPoolableConnectionFactory
protected void createPoolableConnectionFactory(ConnectionFactory driverConnectionFactory, org.apache.commons.pool.KeyedObjectPoolFactory statementPoolFactory, AbandonedConfig configuration) throws java.sql.SQLException
Creates the PoolableConnectionFactory and attaches it to the connection pool. This method only exists so subclasses can replace the default implementation.- Parameters:
driverConnectionFactory- JDBC connection factorystatementPoolFactory- statement pool factory (null if statement pooling is turned off)configuration- abandoned connection tracking configuration (null if no tracking)- Throws:
java.sql.SQLException- if an error occurs creating the PoolableConnectionFactory
-
validateConnectionFactory
protected static void validateConnectionFactory(PoolableConnectionFactory connectionFactory) throws java.lang.Exception
- Throws:
java.lang.Exception
-
log
protected void log(java.lang.String message)
-
getParentLogger
public java.util.logging.Logger getParentLogger() throws java.sql.SQLFeatureNotSupportedException- Specified by:
getParentLoggerin interfacejavax.sql.CommonDataSource- Throws:
java.sql.SQLFeatureNotSupportedException
-
-