Save This Page
Home » commons-dbcp-1.2.2-src » org.apache.commons » dbcp » [javadoc | source]
org.apache.commons.dbcp
public class: BasicDataSource [javadoc | source]
java.lang.Object
   org.apache.commons.dbcp.BasicDataSource

All Implemented Interfaces:
    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.

Field Summary
protected  boolean defaultAutoCommit    The default auto-commit state of connections created by this pool. 
protected  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  String defaultCatalog    The default "catalog" of connections created by this pool. 
protected  String driverClassName    The fully qualified Java class name of the JDBC driver to be used. 
protected  int maxActive    The maximum number of active connections that can be allocated from this pool at the same time, or non-positive for no limit. 
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. 
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 initialSize    The initial number of connections that are created when the pool is started.
    since: 1.2 -
 
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  boolean poolPreparedStatements    Prepared statement pooling for this pool. 
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. 
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. 
protected  boolean testOnReturn    The indication of whether objects will be validated before being returned to the pool. 
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. 
protected  int numTestsPerEvictionRun    The number of objects to examine during each run of the idle object evictor thread (if any). 
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  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. 
protected  String password    The connection password to be passed to our JDBC driver to establish a connection. 
protected  String url    The connection URL to be passed to our JDBC driver to establish a connection. 
protected  String username    The connection username to be passed to our JDBC driver to establish a connection. 
protected  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. 
protected  GenericObjectPool connectionPool    The object pool that internally manages our connections. 
protected  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. 
protected  DataSource dataSource    The data source we will use to manage connections. This object should be acquired ONLY by calls to the createDataSource() method. 
protected  PrintWriter logWriter    The PrintWriter to which log messages should be directed. 
Method from org.apache.commons.dbcp.BasicDataSource Summary:
addConnectionProperty,   close,   createDataSource,   getConnection,   getConnection,   getDefaultAutoCommit,   getDefaultCatalog,   getDefaultReadOnly,   getDefaultTransactionIsolation,   getDriverClassName,   getInitialSize,   getLogAbandoned,   getLogWriter,   getLoginTimeout,   getMaxActive,   getMaxIdle,   getMaxOpenPreparedStatements,   getMaxWait,   getMinEvictableIdleTimeMillis,   getMinIdle,   getNumActive,   getNumIdle,   getNumTestsPerEvictionRun,   getPassword,   getRemoveAbandoned,   getRemoveAbandonedTimeout,   getTestOnBorrow,   getTestOnReturn,   getTestWhileIdle,   getTimeBetweenEvictionRunsMillis,   getUrl,   getUsername,   getValidationQuery,   isAccessToUnderlyingConnectionAllowed,   isPoolPreparedStatements,   removeConnectionProperty,   setAccessToUnderlyingConnectionAllowed,   setDefaultAutoCommit,   setDefaultCatalog,   setDefaultReadOnly,   setDefaultTransactionIsolation,   setDriverClassName,   setInitialSize,   setLogAbandoned,   setLogWriter,   setLoginTimeout,   setMaxActive,   setMaxIdle,   setMaxOpenPreparedStatements,   setMaxWait,   setMinEvictableIdleTimeMillis,   setMinIdle,   setNumTestsPerEvictionRun,   setPassword,   setPoolPreparedStatements,   setRemoveAbandoned,   setRemoveAbandonedTimeout,   setTestOnBorrow,   setTestOnReturn,   setTestWhileIdle,   setTimeBetweenEvictionRunsMillis,   setUrl,   setUsername,   setValidationQuery
Methods from java.lang.Object:
equals,   getClass,   hashCode,   notify,   notifyAll,   toString,   wait,   wait,   wait
Method from org.apache.commons.dbcp.BasicDataSource Detail:
 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). Calls to this method after the connection pool has been initialized have no effect.
 public synchronized  void close() throws SQLException 
    Close and release all connections that are currently stored in the connection pool associated with our data source.
 protected synchronized DataSource createDataSource() throws 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.

 public Connection getConnection() throws SQLException 
    Create (if necessary) and return a connection to the database.
 public Connection getConnection(String username,
    String password) throws SQLException 
    BasicDataSource does NOT support this method.
 public synchronized boolean getDefaultAutoCommit() 
    Returns the default auto-commit property.
 public synchronized String getDefaultCatalog() 
    Returns the default catalog.
 public synchronized boolean getDefaultReadOnly() 
    Returns the default readOnly property.
 public synchronized int getDefaultTransactionIsolation() 
    Returns the default transaction isolation state of returned connections.
 public synchronized String getDriverClassName() 
    Returns the jdbc driver class name.
 public synchronized int getInitialSize() 
    Returns the initial size of the connection pool.
 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.

 public PrintWriter getLogWriter() throws SQLException 

    Returns the log writer being used by this data source.

    Calls #createDataSource() , so has the side effect of initializing the connection pool.

 public int getLoginTimeout() throws SQLException 

    Returns the login timeout (in seconds) for connecting to the database.

    Calls #createDataSource() , so has the side effect of initializing the connection pool.

 public synchronized int getMaxActive() 

    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.

 public synchronized int getMaxIdle() 

    Returns the maximum number of connections that can remain idle in the pool.

    A negative value indicates that there is no limit

 public synchronized int getMaxOpenPreparedStatements() 
 public synchronized long getMaxWait() 

    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.

 public synchronized long getMinEvictableIdleTimeMillis() 
 public synchronized int getMinIdle() 
    Returns the minimum number of idle connections in the pool
 public synchronized int getNumActive() 
    [Read Only] The current number of active connections that have been allocated from this data source.
 public synchronized int getNumIdle() 
    [Read Only] The current number of idle connections that are waiting to be allocated from this data source.
 public synchronized int getNumTestsPerEvictionRun() 
 public synchronized String getPassword() 
    Returns the password passed to the JDBC driver to establish connections.
 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.
 public int getRemoveAbandonedTimeout() 
Deprecated!
    Timeout in seconds before an abandoned connection can be removed. Defaults to 300 seconds.
 public synchronized boolean getTestOnBorrow() 
 public synchronized boolean getTestOnReturn() 
 public synchronized boolean getTestWhileIdle() 
 public synchronized long getTimeBetweenEvictionRunsMillis() 
 public synchronized String getUrl() 
    Returns the JDBC connection #url property.
 public synchronized String getUsername() 
    Returns the JDBC connection #username property.
 public synchronized String getValidationQuery() 
    Returns the validation query used to validate connections before returning them.
 public synchronized boolean isAccessToUnderlyingConnectionAllowed() 
    Returns the value of the accessToUnderlyingConnectionAllowed property.
 public synchronized boolean isPoolPreparedStatements() 
    Returns true if we are pooling statements.
 public  void removeConnectionProperty(String name) 
    Remove a custom connection property.
 public synchronized  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.

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

 public synchronized  void setDefaultCatalog(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.

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

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

 public synchronized  void setDriverClassName(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.

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

 public  void setLogAbandoned(boolean logAbandoned) 
Deprecated!
 public  void setLogWriter(PrintWriter logWriter) throws SQLException 

    Sets the log writer being used by this data source.

    Calls #createDataSource() , so has the side effect of initializing the connection pool.

 public  void setLoginTimeout(int loginTimeout) throws SQLException 

    Set the login timeout (in seconds) for connecting to the database.

    Calls #createDataSource() , so has the side effect of initializing the connection pool.

 public synchronized  void setMaxActive(int maxActive) 
    Sets the maximum number of active connections that can be allocated at the same time.
 public synchronized  void setMaxIdle(int maxIdle) 
    Sets the maximum number of connections that can remail idle in the pool.
 public synchronized  void setMaxOpenPreparedStatements(int maxOpenStatements) 

    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.

 public synchronized  void setMaxWait(long maxWait) 
    Sets the maxWait property.
 public synchronized  void setMinEvictableIdleTimeMillis(long minEvictableIdleTimeMillis) 
 public synchronized  void setMinIdle(int minIdle) 
    Sets the minimum number of idle connections in the pool.
 public synchronized  void setNumTestsPerEvictionRun(int numTestsPerEvictionRun) 
 public synchronized  void setPassword(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.

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

 public  void setRemoveAbandoned(boolean removeAbandoned) 
Deprecated!
 public  void setRemoveAbandonedTimeout(int removeAbandonedTimeout) 
Deprecated!
 public synchronized  void setTestOnBorrow(boolean testOnBorrow) 
    Sets the #testOnBorrow property. This property determines whether or not the pool will validate objects before they are borrowed from the pool. For a true value to have any effect, the validationQuery property must be set to a non-null string.
 public synchronized  void setTestOnReturn(boolean testOnReturn) 
    Sets the 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.
 public synchronized  void setTestWhileIdle(boolean testWhileIdle) 
    Sets the 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.
 public synchronized  void setTimeBetweenEvictionRunsMillis(long timeBetweenEvictionRunsMillis) 
 public synchronized  void setUrl(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.

 public synchronized  void setUsername(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.

 public synchronized  void setValidationQuery(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.