Docjar: A Java Source and Docuemnt Enginecom.*    java.*    javax.*    org.*    all    new    plug-in

Quick Search    Search Deep

org.apache.commons.dbcp
Class BasicDataSource  view BasicDataSource download BasicDataSource.java

java.lang.Object
  extended byorg.apache.commons.dbcp.BasicDataSource
All Implemented Interfaces:
javax.sql.DataSource

public class BasicDataSource
extends java.lang.Object
implements javax.sql.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 $

Field Summary
private  AbandonedConfig abandonedConfig
           
private  boolean accessToUnderlyingConnectionAllowed
          Controls access to the underlying connection
protected  org.apache.commons.pool.impl.GenericObjectPool connectionPool
          The object pool that internally manages our connections.
protected  java.util.Properties connectionProperties
          The connection properties that will be sent to our JDBC driver when establishing new connections.
protected  javax.sql.DataSource dataSource
          The data source we will use to manage connections.
protected  boolean defaultAutoCommit
          The default auto-commit state of connections created by this pool.
protected  java.lang.String defaultCatalog
          The default "catalog" of connections created by this pool.
protected  java.lang.Boolean defaultReadOnly
          The default read-only state of connections created by this pool.
protected  int defaultTransactionIsolation
          The default TransactionIsolation state of connections created by this pool.
protected  java.lang.String driverClassName
          The fully qualified Java class name of the JDBC driver to be used.
protected  int initialSize
          The initial number of connections that are created when the pool is started.
protected  java.io.PrintWriter logWriter
          The PrintWriter to which log messages should be directed.
protected  int maxActive
          The maximum number of active connections that can be allocated from this pool at the same time, or zero for no limit.
protected  int maxIdle
          The maximum number of active connections that can remain idle in the pool, without extra ones being released, or zero for no limit.
protected  int maxOpenPreparedStatements
          The maximum number of open statements that can be allocated from the statement pool at the same time, or zero for no limit.
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 -1 to wait indefinitely.
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).
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.
protected  int numTestsPerEvictionRun
          The number of objects to examine during each run of the idle object evictor thread (if any).
protected  java.lang.String password
          The connection password to be passed to our JDBC driver to establish a connection.
protected  boolean poolPreparedStatements
          Prepared statement pooling for this pool.
private  boolean restartNeeded
           
protected  boolean testOnBorrow
          The indication of whether objects will be validated before being borrowed from the pool.
protected  boolean testOnReturn
          The indication of whether objects will be validated before being returned to the pool.
protected  boolean testWhileIdle
          The indication of whether objects will be validated by the idle object evictor (if any).
protected  long timeBetweenEvictionRunsMillis
          The number of milliseconds to sleep between runs of the idle object evictor thread.
protected  java.lang.String url
          The connection URL to be passed to our JDBC driver to establish a connection.
protected  java.lang.String username
          The connection username to be passed to our JDBC driver to establish a connection.
protected  java.lang.String validationQuery
          The SQL query that will be used to validate connections from this pool before returning them to the caller.
 
Constructor Summary
BasicDataSource()
           
 
Method Summary
 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.
 void close()
          Close and release all connections that are currently stored in the connection pool associated with our data source.
protected  javax.sql.DataSource createDataSource()
          Create (if necessary) and return the internal data source we are using to manage our connections.
 java.sql.Connection getConnection()
          Create (if necessary) and return a connection to the database.
 java.sql.Connection getConnection(java.lang.String username, java.lang.String password)
          Create (if necessary) and return a connection to the database.
 boolean getDefaultAutoCommit()
           
 java.lang.String getDefaultCatalog()
           
 boolean getDefaultReadOnly()
           
 int getDefaultTransactionIsolation()
           
 java.lang.String getDriverClassName()
           
 int getInitialSize()
           
 boolean getLogAbandoned()
          Deprecated.  
 int getLoginTimeout()
          Return the login timeout (in seconds) for connecting to the database.
 java.io.PrintWriter getLogWriter()
          Return the log writer being used by this data source.
 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()
           
 java.lang.String getPassword()
           
 boolean getRemoveAbandoned()
          Deprecated.  
 int getRemoveAbandonedTimeout()
          Deprecated.  
 boolean getTestOnBorrow()
           
 boolean getTestOnReturn()
           
 boolean getTestWhileIdle()
           
 long getTimeBetweenEvictionRunsMillis()
           
 java.lang.String getUrl()
           
 java.lang.String getUsername()
           
 java.lang.String getValidationQuery()
           
 boolean isAccessToUnderlyingConnectionAllowed()
          Returns the value of the accessToUnderlyingConnectionAllowed property.
 boolean isPoolPreparedStatements()
          Returns true if we are pooling statements.
private  boolean isRestartNeeded()
          Returns whether or not a restart is needed.
private  void log(java.lang.String message)
           
 void removeConnectionProperty(java.lang.String name)
           
private  void restart()
           
 void setAccessToUnderlyingConnectionAllowed(boolean allow)
          Sets the value of the accessToUnderlyingConnectionAllowed property.
 void setDefaultAutoCommit(boolean defaultAutoCommit)
           
 void setDefaultCatalog(java.lang.String defaultCatalog)
           
 void setDefaultReadOnly(boolean defaultReadOnly)
           
 void setDefaultTransactionIsolation(int defaultTransactionIsolation)
           
 void setDriverClassName(java.lang.String driverClassName)
           
 void setInitialSize(int initialSize)
           
 void setLogAbandoned(boolean logAbandoned)
          Deprecated.  
 void setLoginTimeout(int loginTimeout)
          Set the login timeout (in seconds) for connecting to the database.
 void setLogWriter(java.io.PrintWriter logWriter)
          Set the log writer being used by this data source.
 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(java.lang.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(java.lang.String url)
           
 void setUsername(java.lang.String username)
           
 void setValidationQuery(java.lang.String validationQuery)
           
private static void validateConnectionFactory(PoolableConnectionFactory connectionFactory)
           
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Field Detail

defaultAutoCommit

protected boolean defaultAutoCommit
The default auto-commit state of connections created by this pool.


defaultReadOnly

protected java.lang.Boolean defaultReadOnly
The default read-only state of connections created by this pool.


defaultTransactionIsolation

protected int defaultTransactionIsolation
The default TransactionIsolation state of connections created by this pool.


defaultCatalog

protected 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.


maxActive

protected int maxActive
The maximum number of active connections that can be allocated from this pool at the same time, or zero for no limit.


maxIdle

protected int maxIdle
The maximum number of active connections that can remain idle in the pool, without extra ones being released, or zero for no limit.


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 -1 to wait indefinitely.


poolPreparedStatements

protected boolean poolPreparedStatements
Prepared statement pooling for this pool.


maxOpenPreparedStatements

protected int maxOpenPreparedStatements
The maximum number of open statements that can be allocated from the statement pool at the same time, or zero 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.


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 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 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.


accessToUnderlyingConnectionAllowed

private boolean accessToUnderlyingConnectionAllowed
Controls access to the underlying connection


restartNeeded

private boolean restartNeeded

connectionPool

protected 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 javax.sql.DataSource dataSource
The data source we will use to manage connections. This object should be acquired ONLY by calls to the createDataSource() method.


logWriter

protected java.io.PrintWriter logWriter
The PrintWriter to which log messages should be directed.


abandonedConfig

private AbandonedConfig abandonedConfig
Constructor Detail

BasicDataSource

public BasicDataSource()
Method Detail

getDefaultAutoCommit

public boolean getDefaultAutoCommit()

setDefaultAutoCommit

public void setDefaultAutoCommit(boolean defaultAutoCommit)

getDefaultReadOnly

public boolean getDefaultReadOnly()

setDefaultReadOnly

public void setDefaultReadOnly(boolean defaultReadOnly)

getDefaultTransactionIsolation

public int getDefaultTransactionIsolation()

setDefaultTransactionIsolation

public void setDefaultTransactionIsolation(int defaultTransactionIsolation)

getDefaultCatalog

public java.lang.String getDefaultCatalog()

setDefaultCatalog

public void setDefaultCatalog(java.lang.String defaultCatalog)

getDriverClassName

public java.lang.String getDriverClassName()

setDriverClassName

public void setDriverClassName(java.lang.String driverClassName)

getMaxActive

public int getMaxActive()

setMaxActive

public void setMaxActive(int maxActive)

getMaxIdle

public int getMaxIdle()

setMaxIdle

public void setMaxIdle(int maxIdle)

getMinIdle

public int getMinIdle()

setMinIdle

public void setMinIdle(int minIdle)

getInitialSize

public int getInitialSize()

setInitialSize

public void setInitialSize(int initialSize)

getMaxWait

public long getMaxWait()

setMaxWait

public void setMaxWait(long maxWait)

isPoolPreparedStatements

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


setPoolPreparedStatements

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


getMaxOpenPreparedStatements

public int getMaxOpenPreparedStatements()

setMaxOpenPreparedStatements

public void setMaxOpenPreparedStatements(int maxOpenStatements)

getTestOnBorrow

public boolean getTestOnBorrow()

setTestOnBorrow

public void setTestOnBorrow(boolean testOnBorrow)

getTestOnReturn

public boolean getTestOnReturn()

setTestOnReturn

public void setTestOnReturn(boolean testOnReturn)

getTimeBetweenEvictionRunsMillis

public long getTimeBetweenEvictionRunsMillis()

setTimeBetweenEvictionRunsMillis

public void setTimeBetweenEvictionRunsMillis(long timeBetweenEvictionRunsMillis)

getNumTestsPerEvictionRun

public int getNumTestsPerEvictionRun()

setNumTestsPerEvictionRun

public void setNumTestsPerEvictionRun(int numTestsPerEvictionRun)

getMinEvictableIdleTimeMillis

public long getMinEvictableIdleTimeMillis()

setMinEvictableIdleTimeMillis

public void setMinEvictableIdleTimeMillis(long minEvictableIdleTimeMillis)

getTestWhileIdle

public boolean getTestWhileIdle()

setTestWhileIdle

public void setTestWhileIdle(boolean testWhileIdle)

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.


getPassword

public java.lang.String getPassword()

setPassword

public void setPassword(java.lang.String password)

getUrl

public java.lang.String getUrl()

setUrl

public void setUrl(java.lang.String url)

getUsername

public java.lang.String getUsername()

setUsername

public void setUsername(java.lang.String username)

getValidationQuery

public java.lang.String getValidationQuery()

setValidationQuery

public void setValidationQuery(java.lang.String validationQuery)

isAccessToUnderlyingConnectionAllowed

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


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)


isRestartNeeded

private boolean isRestartNeeded()
Returns whether or not a restart is needed.


getConnection

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

Specified by:
getConnection in interface javax.sql.DataSource

getConnection

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

Specified by:
getConnection in interface javax.sql.DataSource

getLoginTimeout

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

Specified by:
getLoginTimeout in interface javax.sql.DataSource

getLogWriter

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

Specified by:
getLogWriter in interface javax.sql.DataSource

setLoginTimeout

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

Specified by:
setLoginTimeout in interface javax.sql.DataSource

setLogWriter

public void setLogWriter(java.io.PrintWriter logWriter)
                  throws java.sql.SQLException
Set the log writer being used by this data source.

Specified by:
setLogWriter in interface javax.sql.DataSource

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.


setRemoveAbandoned

public void setRemoveAbandoned(boolean removeAbandoned)
Deprecated.  


getRemoveAbandonedTimeout

public int getRemoveAbandonedTimeout()
Deprecated.  

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


setRemoveAbandonedTimeout

public void setRemoveAbandonedTimeout(int removeAbandonedTimeout)
Deprecated.  


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.


setLogAbandoned

public void setLogAbandoned(boolean logAbandoned)
Deprecated.  


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).


removeConnectionProperty

public void removeConnectionProperty(java.lang.String name)

close

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


createDataSource

protected javax.sql.DataSource createDataSource()
                                         throws java.sql.SQLException

Create (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.


validateConnectionFactory

private static void validateConnectionFactory(PoolableConnectionFactory connectionFactory)
                                       throws java.lang.Exception

restart

private void restart()

log

private void log(java.lang.String message)