org.apache.commons.dbcp

Class BasicDataSource

Implemented Interfaces:
DataSource

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 $
Authors:
Glenn L. Nielsen
Craig R. McClanahan
Dirk Verbeeck

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)
Create (if necessary) and return a connection to the database.
boolean
getDefaultAutoCommit()
String
getDefaultCatalog()
boolean
getDefaultReadOnly()
int
getDefaultTransactionIsolation()
String
getDriverClassName()
int
getInitialSize()
boolean
getLogAbandoned()
Deprecated.
PrintWriter
getLogWriter()
Return the log writer being used by this data source.
int
getLoginTimeout()
Return the login timeout (in seconds) for connecting to the database.
int
getMaxActive()
int
getMaxIdle()
int
getMaxOpenPreparedStatements()
long
getMaxWait()
long
getMinEvictableIdleTimeMillis()
int
getMinIdle()
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()
String
getPassword()
boolean
getRemoveAbandoned()
Deprecated.
int
getRemoveAbandonedTimeout()
Deprecated.
boolean
getTestOnBorrow()
boolean
getTestOnReturn()
boolean
getTestWhileIdle()
long
getTimeBetweenEvictionRunsMillis()
String
getUrl()
String
getUsername()
String
getValidationQuery()
boolean
isAccessToUnderlyingConnectionAllowed()
Returns the value of the accessToUnderlyingConnectionAllowed property.
boolean
isPoolPreparedStatements()
Returns true if we are pooling statements.
void
removeConnectionProperty(String name)
void
setAccessToUnderlyingConnectionAllowed(boolean allow)
Sets the value of the accessToUnderlyingConnectionAllowed property.
void
setDefaultAutoCommit(boolean defaultAutoCommit)
void
setDefaultCatalog(String defaultCatalog)
void
setDefaultReadOnly(boolean defaultReadOnly)
void
setDefaultTransactionIsolation(int defaultTransactionIsolation)
void
setDriverClassName(String driverClassName)
void
setInitialSize(int initialSize)
void
setLogAbandoned(boolean logAbandoned)
Deprecated.
void
setLogWriter(PrintWriter logWriter)
Set the log writer being used by this data source.
void
setLoginTimeout(int loginTimeout)
Set the login timeout (in seconds) for connecting to the database.
void
setMaxActive(int maxActive)
void
setMaxIdle(int maxIdle)
void
setMaxOpenPreparedStatements(int maxOpenStatements)
void
setMaxWait(long maxWait)
void
setMinEvictableIdleTimeMillis(long minEvictableIdleTimeMillis)
void
setMinIdle(int minIdle)
void
setNumTestsPerEvictionRun(int numTestsPerEvictionRun)
void
setPassword(String password)
void
setPoolPreparedStatements(boolean poolingStatements)
Sets whether to pool statements or not.
void
setRemoveAbandoned(boolean removeAbandoned)
Deprecated.
void
setRemoveAbandonedTimeout(int removeAbandonedTimeout)
Deprecated.
void
setTestOnBorrow(boolean testOnBorrow)
void
setTestOnReturn(boolean testOnReturn)
void
setTestWhileIdle(boolean testWhileIdle)
void
setTimeBetweenEvictionRunsMillis(long timeBetweenEvictionRunsMillis)
void
setUrl(String url)
void
setUsername(String username)
void
setValidationQuery(String validationQuery)

Method Details

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()
            throws SQLException
Close and release all connections that are currently stored in the connection pool associated with our data source.

getConnection

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

getConnection

public Connection getConnection(String username,
                                String password)
            throws SQLException
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

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.

getLogWriter

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

getLoginTimeout

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

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 -

setLogWriter

public void setLogWriter(PrintWriter logWriter)
            throws SQLException
Set the log writer being used by this data source.
Parameters:
logWriter - The new log writer

setLoginTimeout

public void setLoginTimeout(int loginTimeout)
            throws SQLException
Set the login timeout (in seconds) for connecting to the database.
Parameters:
loginTimeout - The new login timeout, or zero for no timeout

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:

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 March 27 2007.