org.apache.commons.dbcp
public class BasicDataSource extends Object implements DataSource
Basic implementation of javax.sql.DataSource
that 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.
Version: $Revision: 506087 $ $Date: 2007-02-11 11:37:43 -0700 (Sun, 11 Feb 2007) $
Method Summary | |
---|---|
void | addConnectionProperty(String name, String value)
Add a custom connection property to the set that will be passed to our
JDBC driver. |
void | close()
Close and release all connections that are currently stored in the
connection pool associated with our data source.
|
Connection | getConnection()
Create (if necessary) and return a connection to the database.
|
Connection | getConnection(String username, String password)
BasicDataSource does NOT support this method.
|
boolean | getDefaultAutoCommit()
Returns the default auto-commit property.
|
String | getDefaultCatalog()
Returns the default catalog.
|
boolean | getDefaultReadOnly()
Returns the default readOnly property.
|
int | getDefaultTransactionIsolation()
Returns the default transaction isolation state of returned connections.
|
String | getDriverClassName()
Returns the jdbc driver class name.
|
int | getInitialSize()
Returns the initial size of the connection pool.
|
boolean | getLogAbandoned() Flag to log stack traces for application code which abandoned a Statement or Connection. |
int | getLoginTimeout() Returns the login timeout (in seconds) for connecting to the database. |
PrintWriter | getLogWriter() Returns the log writer being used by this data source. Calls createDataSource, so has the side effect of initializing the connection pool. |
int | getMaxActive() Returns the maximum number of active connections that can be allocated at the same time. |
int | getMaxIdle() Returns the maximum number of connections that can remain idle in the pool. |
int | getMaxOpenPreparedStatements()
Gets the value of the maxOpenPreparedStatements property.
|
long | getMaxWait() Returns the maximum number of milliseconds that the pool will wait for a connection to be returned before throwing an exception. |
long | getMinEvictableIdleTimeMillis()
Returns the minEvictableIdleTimeMillis property.
|
int | getMinIdle()
Returns the minimum number of idle connections in the pool
|
int | getNumActive()
[Read Only] The current number of active connections that have been
allocated from this data source.
|
int | getNumIdle()
[Read Only] The current number of idle connections that are waiting
to be allocated from this data source.
|
int | getNumTestsPerEvictionRun()
Returns the value of the numTestsPerEvictionRun property.
|
String | getPassword()
Returns the password passed to the JDBC driver to establish connections.
|
boolean | getRemoveAbandoned()
Flag to remove abandoned connections if they exceed the
removeAbandonedTimout.
|
int | getRemoveAbandonedTimeout()
Timeout in seconds before an abandoned connection can be removed.
|
boolean | getTestOnBorrow()
Returns the testOnBorrow property.
|
boolean | getTestOnReturn()
Returns the value of the testOnReturn property.
|
boolean | getTestWhileIdle()
Returns the value of the testWhileIdle property.
|
long | getTimeBetweenEvictionRunsMillis()
Returns the value of the timeBetweenEvictionRunsMillis
property.
|
String | getUrl()
Returns the JDBC connection url property.
|
String | getUsername()
Returns the JDBC connection username property.
|
String | getValidationQuery()
Returns the validation query used to validate connections before
returning them.
|
boolean | isAccessToUnderlyingConnectionAllowed()
Returns the value of the accessToUnderlyingConnectionAllowed property.
|
boolean | isPoolPreparedStatements()
Returns true if we are pooling statements.
|
void | removeConnectionProperty(String name)
Remove a custom connection property.
|
void | setAccessToUnderlyingConnectionAllowed(boolean allow) Sets the value of the accessToUnderlyingConnectionAllowed property. |
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. |
void | setDefaultCatalog(String defaultCatalog) Sets the default catalog. Note: this method currently has no effect once the pool has been initialized. |
void | setDefaultReadOnly(boolean defaultReadOnly) Sets defaultReadonly property. Note: this method currently has no effect once the pool has been initialized. |
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. |
void | setDriverClassName(String driverClassName) Sets the jdbc driver class name. Note: this method currently has no effect once the pool has been initialized. |
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. |
void | setLogAbandoned(boolean logAbandoned) |
void | setLoginTimeout(int loginTimeout) Set the login timeout (in seconds) for connecting to the database. Calls createDataSource, so has the side effect of initializing the connection pool. |
void | setLogWriter(PrintWriter logWriter) Sets the log writer being used by this data source. Calls createDataSource, so has the side effect of initializing the connection pool. |
void | setMaxActive(int maxActive)
Sets the maximum number of active connections that can be
allocated at the same time.
|
void | setMaxIdle(int maxIdle)
Sets the maximum number of connections that can remail idle in the
pool.
|
void | setMaxOpenPreparedStatements(int maxOpenStatements) Sets the value of the maxOpenPreparedStatements property. Note: this method currently has no effect once the pool has been initialized. |
void | setMaxWait(long maxWait)
Sets the maxWait property.
|
void | setMinEvictableIdleTimeMillis(long minEvictableIdleTimeMillis)
Sets the minEvictableIdleTimeMillis property.
|
void | setMinIdle(int minIdle)
Sets the minimum number of idle connections in the pool.
|
void | setNumTestsPerEvictionRun(int numTestsPerEvictionRun)
Sets the value of the numTestsPerEvictionRun property.
|
void | setPassword(String password) Sets the password. Note: this method currently has no effect once the pool has been initialized. |
void | setPoolPreparedStatements(boolean poolingStatements) Sets whether to pool statements or not. Note: this method currently has no effect once the pool has been initialized. |
void | setRemoveAbandoned(boolean removeAbandoned) |
void | setRemoveAbandonedTimeout(int removeAbandonedTimeout) |
void | setTestOnBorrow(boolean testOnBorrow)
Sets the testOnBorrow property. |
void | setTestOnReturn(boolean testOnReturn)
Sets the testOnReturn property. |
void | setTestWhileIdle(boolean testWhileIdle)
Sets the testWhileIdle property. |
void | setTimeBetweenEvictionRunsMillis(long timeBetweenEvictionRunsMillis)
Sets the timeBetweenEvictionRunsMillis property.
|
void | setUrl(String url) Sets the url. Note: this method currently has no effect once the pool has been initialized. |
void | setUsername(String username) Sets the username. Note: this method currently has no effect once the pool has been initialized. |
void | setValidationQuery(String validationQuery) Sets the validationQuery. Note: this method currently has no effect once the pool has been initialized. |
Parameters: name Name of the custom connection property value Value of the custom connection property
Throws: SQLException if a database error occurs
Returns: a database connection
Throws: SQLException if a database access error occurs
Parameters: username Database user on whose behalf the Connection is being made password The database user's password
Returns: nothing - always throws UnsupportedOperationException
Throws: UnsupportedOperationException SQLException if a database access error occurs
Returns: true if default auto-commit is enabled
Returns: the default catalog
Returns: true if connections are readOnly by default
Returns: the default value for transaction isolation state
See Also: Connection#getTransactionIsolation
Returns: the jdbc driver class name
Returns: the number of connections created when the pool is initialized
Deprecated:
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.
Returns the login timeout (in seconds) for connecting to the database.
Calls createDataSource, so has the side effect of initializing the connection pool.
Returns: login timeout in seconds
Throws: SQLException if a database access error occurs UnsupportedOperationException If the DataSource implementation does not support the login timeout feature.
Returns the log writer being used by this data source.
Calls createDataSource, so has the side effect of initializing the connection pool.
Returns: log writer in use
Throws: SQLException if a database access error occurs
Returns the maximum number of active connections that can be allocated at the same time.
A non-positive number means that there is no limit.
Returns: the maximum number of active connections
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
Returns: the maximum number of open statements
See Also: maxOpenPreparedStatements
Returns the maximum number of milliseconds that the pool will wait for a connection to be returned before throwing an exception.
Returns -1 if the pool is set to wait indefinitely.
Returns: the maxWait property value
Returns: the value of the minEvictableIdleTimeMillis property
See Also: minEvictableIdleTimeMillis
Returns: the minimum number of idle connections
See Also: GenericObjectPool#getMinIdle()
Returns: the current number of active connections
Returns: the current number of idle connections
Returns: the number of objects to examine during idle object evictor runs
See Also: numTestsPerEvictionRun
Returns: the connection password
Deprecated:
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.Deprecated:
Timeout in seconds before an abandoned connection can be removed. Defaults to 300 seconds.Returns: abandoned connection timeout
Returns: true if objects are validated before being borrowed from the pool
See Also: testOnBorrow
Returns: true if objects are validated before being returned to the pool
See Also: testOnReturn
Returns: true if objects examined by the idle object evictor are validated
See Also: testWhileIdle
Returns: the time (in miliseconds) between evictor runs
See Also: timeBetweenEvictionRunsMillis
Returns: the url passed to the JDBC driver to establish connections
Returns: the username passed to the JDBC driver to establish connections
Returns: the SQL validation query
See Also: validationQuery
Returns: true if access to the underlying connection is allowed, false otherwise.
Returns: true if prepared statements are pooled
Parameters: name Name of the custom connection property to remove
See Also: BasicDataSource
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.
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
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
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
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
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
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
Deprecated:
Parameters: logAbandoned new logAbandoned property value
Set the login timeout (in seconds) for connecting to the database.
Calls createDataSource, so has the side effect of initializing the connection pool.
Parameters: loginTimeout The new login timeout, or zero for no timeout
Throws: SQLException if a database access error occurs
Sets the log writer being used by this data source.
Calls createDataSource, so has the side effect of initializing the connection pool.
Parameters: logWriter The new log writer
Throws: SQLException if a database access error occurs
Parameters: maxActive the new value for maxActive
See Also: getMaxActive
Parameters: maxIdle the new value for maxIdle
See Also: getMaxIdle
Sets the value of the maxOpenPreparedStatements property.
Note: this method currently has no effect once the pool has been
initialized. The pool is initialized the first time one of the
following methods is invoked: getConnection, setLogwriter,
setLoginTimeout, getLoginTimeout, getLogWriter.
Parameters: maxOpenStatements the new maximum number of prepared statements
See Also: maxOpenPreparedStatements
Parameters: maxWait the new value for maxWait
See Also: getMaxWait
Parameters: minEvictableIdleTimeMillis the minimum amount of time an object may sit idle in the pool
See Also: minEvictableIdleTimeMillis
Parameters: minIdle the new value for minIdle
See Also: GenericObjectPool#setMinIdle(int)
Parameters: numTestsPerEvictionRun the new numTestsPerEvictionRun value
See Also: numTestsPerEvictionRun
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
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
Deprecated:
Parameters: removeAbandoned new removeAbandoned property value
Deprecated:
Parameters: removeAbandonedTimeout new removeAbandonedTimeout value
true
value to have any effect, the
validationQuery
property must be set to a non-null string.
Parameters: testOnBorrow new value for testOnBorrow property
testOnReturn
property. This property determines
whether or not the pool will validate objects before they are returned
to the pool. For a true
value to have any effect, the
validationQuery
property must be set to a non-null string.
Parameters: testOnReturn new value for testOnReturn property
testWhileIdle
property. This property determines
whether or not the idle object evictor will validate connections. For a
true
value to have any effect, the
validationQuery
property must be set to a non-null string.
Parameters: testWhileIdle new value for testWhileIdle property
Parameters: timeBetweenEvictionRunsMillis the new time between evictor runs
See Also: timeBetweenEvictionRunsMillis
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
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
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