org.apache.commons.dbcp

Class BasicDataSource

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: 1.37 $ $Date: 2004/06/09 18:21:23 $

Author: Glenn L. Nielsen Craig R. McClanahan Dirk Verbeeck

Method Summary
voidaddConnectionProperty(String name, String value)
Add a custom connection property to the set that will be passed to our JDBC driver.
voidclose()
Close and release all connections that are currently stored in the connection pool associated with our data source.
ConnectiongetConnection()
Create (if necessary) and return a connection to the database.
ConnectiongetConnection(String username, String password)
Create (if necessary) and return a connection to the database.
booleangetDefaultAutoCommit()
StringgetDefaultCatalog()
booleangetDefaultReadOnly()
intgetDefaultTransactionIsolation()
StringgetDriverClassName()
intgetInitialSize()
booleangetLogAbandoned()
Flag to log stack traces for application code which abandoned a Statement or Connection.
intgetLoginTimeout()
Return the login timeout (in seconds) for connecting to the database.
PrintWritergetLogWriter()
Return the log writer being used by this data source.
intgetMaxActive()
intgetMaxIdle()
intgetMaxOpenPreparedStatements()
longgetMaxWait()
longgetMinEvictableIdleTimeMillis()
intgetMinIdle()
intgetNumActive()
[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()
StringgetPassword()
booleangetRemoveAbandoned()
Flag to remove abandoned connections if they exceed the removeAbandonedTimout.
intgetRemoveAbandonedTimeout()
Timeout in seconds before an abandoned connection can be removed.
booleangetTestOnBorrow()
booleangetTestOnReturn()
booleangetTestWhileIdle()
longgetTimeBetweenEvictionRunsMillis()
StringgetUrl()
StringgetUsername()
StringgetValidationQuery()
booleanisAccessToUnderlyingConnectionAllowed()
Returns the value of the accessToUnderlyingConnectionAllowed property.
booleanisPoolPreparedStatements()
Returns true if we are pooling statements.
voidremoveConnectionProperty(String name)
voidsetAccessToUnderlyingConnectionAllowed(boolean allow)
Sets the value of the accessToUnderlyingConnectionAllowed property.
voidsetDefaultAutoCommit(boolean defaultAutoCommit)
voidsetDefaultCatalog(String defaultCatalog)
voidsetDefaultReadOnly(boolean defaultReadOnly)
voidsetDefaultTransactionIsolation(int defaultTransactionIsolation)
voidsetDriverClassName(String driverClassName)
voidsetInitialSize(int initialSize)
voidsetLogAbandoned(boolean logAbandoned)
voidsetLoginTimeout(int loginTimeout)
Set the login timeout (in seconds) for connecting to the database.
voidsetLogWriter(PrintWriter logWriter)
Set the log writer being used by this data source.
voidsetMaxActive(int maxActive)
voidsetMaxIdle(int maxIdle)
voidsetMaxOpenPreparedStatements(int maxOpenStatements)
voidsetMaxWait(long maxWait)
voidsetMinEvictableIdleTimeMillis(long minEvictableIdleTimeMillis)
voidsetMinIdle(int minIdle)
voidsetNumTestsPerEvictionRun(int numTestsPerEvictionRun)
voidsetPassword(String password)
voidsetPoolPreparedStatements(boolean poolingStatements)
Sets whether to pool statements or not.
voidsetRemoveAbandoned(boolean removeAbandoned)
voidsetRemoveAbandonedTimeout(int removeAbandonedTimeout)
voidsetTestOnBorrow(boolean testOnBorrow)
voidsetTestOnReturn(boolean testOnReturn)
voidsetTestWhileIdle(boolean testWhileIdle)
voidsetTimeBetweenEvictionRunsMillis(long timeBetweenEvictionRunsMillis)
voidsetUrl(String url)
voidsetUsername(String username)
voidsetValidationQuery(String validationQuery)

Method Detail

addConnectionProperty

public void addConnectionProperty(String name, 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).

Parameters: name Name of the custom connection property value Value of the custom connection property

close

public void close()
Close and release all connections that are currently stored in the connection pool associated with our data source.

Throws: SQLException if a database error occurs

getConnection

public Connection getConnection()
Create (if necessary) and return a connection to the database.

Throws: SQLException if a database access error occurs

getConnection

public Connection getConnection(String username, String password)
Create (if necessary) and return a connection to the database.

Parameters: username Database user on whose behalf the Connection is being made password The database user's password

Throws: SQLException if a database access error occurs

getDefaultAutoCommit

public boolean getDefaultAutoCommit()

getDefaultCatalog

public String getDefaultCatalog()

getDefaultReadOnly

public boolean getDefaultReadOnly()

getDefaultTransactionIsolation

public int getDefaultTransactionIsolation()

getDriverClassName

public String getDriverClassName()

getInitialSize

public int getInitialSize()

getLogAbandoned

public boolean getLogAbandoned()

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.

getLoginTimeout

public int getLoginTimeout()
Return the login timeout (in seconds) for connecting to the database.

Throws: SQLException if a database access error occurs

getLogWriter

public PrintWriter getLogWriter()
Return the log writer being used by this data source.

Throws: SQLException if a database access error occurs

getMaxActive

public int getMaxActive()

getMaxIdle

public int getMaxIdle()

getMaxOpenPreparedStatements

public int getMaxOpenPreparedStatements()

getMaxWait

public long getMaxWait()

getMinEvictableIdleTimeMillis

public long getMinEvictableIdleTimeMillis()

getMinIdle

public int getMinIdle()

getNumActive

public int getNumActive()
[Read Only] The current number of active connections that have been allocated from this data source.

getNumIdle

public int getNumIdle()
[Read Only] The current number of idle connections that are waiting to be allocated from this data source.

getNumTestsPerEvictionRun

public int getNumTestsPerEvictionRun()

getPassword

public String getPassword()

getRemoveAbandoned

public boolean getRemoveAbandoned()

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.

getRemoveAbandonedTimeout

public int getRemoveAbandonedTimeout()

Deprecated:

Timeout in seconds before an abandoned connection can be removed. Defaults to 300 seconds.

getTestOnBorrow

public boolean getTestOnBorrow()

getTestOnReturn

public boolean getTestOnReturn()

getTestWhileIdle

public boolean getTestWhileIdle()

getTimeBetweenEvictionRunsMillis

public long getTimeBetweenEvictionRunsMillis()

getUrl

public String getUrl()

getUsername

public String getUsername()

getValidationQuery

public String getValidationQuery()

isAccessToUnderlyingConnectionAllowed

public boolean isAccessToUnderlyingConnectionAllowed()
Returns the value of the accessToUnderlyingConnectionAllowed property.

Returns: true if access to the underlying is allowed, false otherwise.

isPoolPreparedStatements

public boolean isPoolPreparedStatements()
Returns true if we are pooling statements.

Returns: boolean

removeConnectionProperty

public void removeConnectionProperty(String name)

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)

Parameters: allow Access to the underlying connection is granted when true.

setDefaultAutoCommit

public void setDefaultAutoCommit(boolean defaultAutoCommit)

setDefaultCatalog

public void setDefaultCatalog(String defaultCatalog)

setDefaultReadOnly

public void setDefaultReadOnly(boolean defaultReadOnly)

setDefaultTransactionIsolation

public void setDefaultTransactionIsolation(int defaultTransactionIsolation)

setDriverClassName

public void setDriverClassName(String driverClassName)

setInitialSize

public void setInitialSize(int initialSize)

setLogAbandoned

public void setLogAbandoned(boolean logAbandoned)

Deprecated:

Parameters: logAbandoned

setLoginTimeout

public void setLoginTimeout(int loginTimeout)
Set the login timeout (in seconds) for connecting to the database.

Parameters: loginTimeout The new login timeout, or zero for no timeout

Throws: SQLException if a database access error occurs

setLogWriter

public void setLogWriter(PrintWriter logWriter)
Set the log writer being used by this data source.

Parameters: logWriter The new log writer

Throws: SQLException if a database access error occurs

setMaxActive

public void setMaxActive(int maxActive)

setMaxIdle

public void setMaxIdle(int maxIdle)

setMaxOpenPreparedStatements

public void setMaxOpenPreparedStatements(int maxOpenStatements)

setMaxWait

public void setMaxWait(long maxWait)

setMinEvictableIdleTimeMillis

public void setMinEvictableIdleTimeMillis(long minEvictableIdleTimeMillis)

setMinIdle

public void setMinIdle(int minIdle)

setNumTestsPerEvictionRun

public void setNumTestsPerEvictionRun(int numTestsPerEvictionRun)

setPassword

public void setPassword(String password)

setPoolPreparedStatements

public void setPoolPreparedStatements(boolean poolingStatements)
Sets whether to pool statements or not.

Parameters: poolPreparedStatements pooling on or off

setRemoveAbandoned

public void setRemoveAbandoned(boolean removeAbandoned)

Deprecated:

Parameters: removeAbandoned

setRemoveAbandonedTimeout

public void setRemoveAbandonedTimeout(int removeAbandonedTimeout)

Deprecated:

Parameters: removeAbandonedTimeout

setTestOnBorrow

public void setTestOnBorrow(boolean testOnBorrow)

setTestOnReturn

public void setTestOnReturn(boolean testOnReturn)

setTestWhileIdle

public void setTestWhileIdle(boolean testWhileIdle)

setTimeBetweenEvictionRunsMillis

public void setTimeBetweenEvictionRunsMillis(long timeBetweenEvictionRunsMillis)

setUrl

public void setUrl(String url)

setUsername

public void setUsername(String username)

setValidationQuery

public void setValidationQuery(String validationQuery)
Copyright © 2001-2003 Apache Software Foundation. Documenation generated February 19 2008.