public class BasicDataSource extends Object implements DataSource
Basic implementation of javax.sql.DataSource
that is
configured via JavaBeans properties.
Modifier and Type | Field and Description |
---|---|
protected boolean |
closed |
protected List |
connectionInitSqls
These SQL statements run once after a Connection is created.
|
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.
|
protected 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 String |
defaultCatalog
The default "catalog" 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 ClassLoader |
driverClassLoader |
protected String |
driverClassName
The fully qualified Java class name of the JDBC driver to be used.
|
protected int |
initialSize |
protected PrintWriter |
logWriter
The PrintWriter to which log messages should be directed.
|
protected int |
maxActive |
protected int |
maxIdle |
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.
|
protected long |
maxWait |
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 |
protected int |
numTestsPerEvictionRun
The number of objects to examine during each run of the idle object
evictor thread (if any).
|
protected 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.
|
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 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.
|
protected int |
validationQueryTimeout
Timeout in seconds before connection validation queries fail.
|
Constructor and Description |
---|
BasicDataSource() |
Modifier and Type | Method and Description |
---|---|
void |
addConnectionProperty(String name,
String value) |
void |
close() |
protected ConnectionFactory |
createConnectionFactory() |
protected void |
createConnectionPool() |
protected DataSource |
createDataSource() |
protected void |
createDataSourceInstance() |
protected void |
createPoolableConnectionFactory(ConnectionFactory driverConnectionFactory,
KeyedObjectPoolFactory statementPoolFactory,
AbandonedConfig configuration) |
Connection |
getConnection()
Create (if necessary) and return a connection to the database.
|
Connection |
getConnection(String user,
String pass) |
Collection |
getConnectionInitSqls()
Returns the list of SQL statements executed when a physical connection
is first created.
|
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.
|
ClassLoader |
getDriverClassLoader() |
String |
getDriverClassName()
Returns the jdbc driver class name.
|
int |
getInitialSize() |
boolean |
getLogAbandoned() |
int |
getLoginTimeout() |
PrintWriter |
getLogWriter() |
int |
getMaxActive() |
int |
getMaxIdle() |
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() |
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. |
Logger |
getParentLogger() |
String |
getPassword()
Returns the password passed to the JDBC driver to establish connections.
|
boolean |
getRemoveAbandoned() |
int |
getRemoveAbandonedTimeout() |
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.
|
int |
getValidationQueryTimeout()
Returns the validation query timeout.
|
boolean |
isAccessToUnderlyingConnectionAllowed()
Returns the value of the accessToUnderlyingConnectionAllowed property.
|
boolean |
isClosed() |
boolean |
isPoolPreparedStatements()
Returns true if we are pooling statements.
|
boolean |
isWrapperFor(Class<?> iface) |
protected void |
log(String message) |
void |
removeConnectionProperty(String name) |
void |
setAccessToUnderlyingConnectionAllowed(boolean allow)
Sets the value of the accessToUnderlyingConnectionAllowed property.
|
void |
setConnectionInitSqls(Collection connectionInitSqls)
Sets the list of SQL statements to be executed when a physical
connection is first created.
|
void |
setConnectionProperties(String connectionProperties) |
void |
setDefaultAutoCommit(boolean defaultAutoCommit)
Sets default auto-commit state of connections returned by this
datasource.
|
void |
setDefaultCatalog(String defaultCatalog)
Sets the default catalog.
|
void |
setDefaultReadOnly(boolean defaultReadOnly)
Sets defaultReadonly property.
|
void |
setDefaultTransactionIsolation(int defaultTransactionIsolation)
Sets the default transaction isolation state for returned
connections.
|
void |
setDriverClassLoader(ClassLoader driverClassLoader) |
void |
setDriverClassName(String driverClassName)
Sets the jdbc driver class name.
|
void |
setInitialSize(int initialSize) |
void |
setLogAbandoned(boolean logAbandoned) |
void |
setLoginTimeout(int loginTimeout) |
void |
setLogWriter(PrintWriter logWriter) |
void |
setMaxActive(int maxActive) |
void |
setMaxIdle(int maxIdle) |
void |
setMaxOpenPreparedStatements(int maxOpenStatements)
Sets the value of the
maxOpenPreparedStatements
property. |
void |
setMaxWait(long maxWait)
Sets the maxWait property.
|
void |
setMinEvictableIdleTimeMillis(long minEvictableIdleTimeMillis)
Sets the
minEvictableIdleTimeMillis property. |
void |
setMinIdle(int minIdle) |
void |
setNumTestsPerEvictionRun(int numTestsPerEvictionRun)
Sets the value of the
numTestsPerEvictionRun property. |
void |
setPassword(String password)
Sets the
password . |
void |
setPoolPreparedStatements(boolean poolingStatements)
Sets whether to pool statements or not.
|
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 . |
void |
setUsername(String username)
Sets the
username . |
void |
setValidationQuery(String validationQuery)
Sets the
validationQuery . |
void |
setValidationQueryTimeout(int timeout)
Sets the validation query timeout, the amount of time, in seconds, that
connection validation will wait for a response from the database when
executing a validation query.
|
<T> T |
unwrap(Class<T> iface) |
protected static void |
validateConnectionFactory(PoolableConnectionFactory connectionFactory) |
protected volatile boolean defaultAutoCommit
protected transient Boolean defaultReadOnly
protected volatile int defaultTransactionIsolation
protected volatile String defaultCatalog
protected String driverClassName
protected ClassLoader driverClassLoader
protected int maxActive
protected int maxIdle
protected int minIdle
protected int initialSize
protected long maxWait
protected boolean poolPreparedStatements
true
both PreparedStatements and CallableStatements are pooled.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.
Note: As of version 1.3, CallableStatements (those produced by Connection.prepareCall(java.lang.String)
)
are pooled along with PreparedStatements (produced by Connection.prepareStatement(java.lang.String)
)
and maxOpenPreparedStatements
limits the total number of prepared or callable statements
that may be in use at a given time.
protected boolean testOnBorrow
protected boolean testOnReturn
protected long timeBetweenEvictionRunsMillis
protected int numTestsPerEvictionRun
protected long minEvictableIdleTimeMillis
protected boolean testWhileIdle
protected volatile String password
protected String url
protected String username
protected volatile String validationQuery
protected volatile int validationQueryTimeout
protected volatile List connectionInitSqls
This property can be used for example to run ALTER SESSION SET NLS_SORT=XCYECH in an Oracle Database only once after connection creation.
protected volatile GenericObjectPool connectionPool
protected Properties connectionProperties
protected volatile DataSource dataSource
createDataSource()
method.protected PrintWriter logWriter
protected boolean closed
public boolean getDefaultAutoCommit()
public 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.
defaultAutoCommit
- default auto-commit valuepublic boolean getDefaultReadOnly()
public 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.
defaultReadOnly
- default read-only valuepublic int getDefaultTransactionIsolation()
Connection.getTransactionIsolation()
public 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.
defaultTransactionIsolation
- the default transaction isolation
stateConnection.getTransactionIsolation()
public String getDefaultCatalog()
public 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.
defaultCatalog
- the default catalogpublic String getDriverClassName()
public 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.
driverClassName
- the class name of the jdbc driverpublic ClassLoader getDriverClassLoader()
public void setDriverClassLoader(ClassLoader driverClassLoader)
public int getMaxActive()
public void setMaxActive(int maxActive)
public int getMaxIdle()
public void setMaxIdle(int maxIdle)
public int getMinIdle()
public void setMinIdle(int minIdle)
public int getInitialSize()
public void setInitialSize(int initialSize)
public long getMaxWait()
Returns the maximum number of milliseconds that the pool will wait for a connection to be returned before throwing an exception.
A value less than or equal to zero means the pool is set to wait indefinitely.
public void setMaxWait(long maxWait)
Sets the maxWait property.
Use -1 to make the pool wait indefinitely.
maxWait
- the new value for maxWaitgetMaxWait()
public boolean isPoolPreparedStatements()
public 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.
poolingStatements
- pooling on or offpublic int getMaxOpenPreparedStatements()
maxOpenPreparedStatements
property.maxOpenPreparedStatements
public 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.
maxOpenStatements
- the new maximum number of prepared statementsmaxOpenPreparedStatements
public boolean getTestOnBorrow()
testOnBorrow
property.testOnBorrow
public void setTestOnBorrow(boolean testOnBorrow)
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.testOnBorrow
- new value for testOnBorrow propertypublic boolean getTestOnReturn()
testOnReturn
property.testOnReturn
public void setTestOnReturn(boolean testOnReturn)
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.testOnReturn
- new value for testOnReturn propertypublic long getTimeBetweenEvictionRunsMillis()
timeBetweenEvictionRunsMillis
property.timeBetweenEvictionRunsMillis
public void setTimeBetweenEvictionRunsMillis(long timeBetweenEvictionRunsMillis)
timeBetweenEvictionRunsMillis
property.timeBetweenEvictionRunsMillis
- the new time between evictor runstimeBetweenEvictionRunsMillis
public int getNumTestsPerEvictionRun()
numTestsPerEvictionRun
property.numTestsPerEvictionRun
public void setNumTestsPerEvictionRun(int numTestsPerEvictionRun)
numTestsPerEvictionRun
property.numTestsPerEvictionRun
- the new numTestsPerEvictionRun
valuenumTestsPerEvictionRun
public long getMinEvictableIdleTimeMillis()
minEvictableIdleTimeMillis
property.minEvictableIdleTimeMillis
propertyminEvictableIdleTimeMillis
public void setMinEvictableIdleTimeMillis(long minEvictableIdleTimeMillis)
minEvictableIdleTimeMillis
property.minEvictableIdleTimeMillis
- the minimum amount of time an object
may sit idle in the poolminEvictableIdleTimeMillis
public boolean getTestWhileIdle()
testWhileIdle
property.testWhileIdle
public void setTestWhileIdle(boolean testWhileIdle)
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.testWhileIdle
- new value for testWhileIdle propertypublic int getNumActive()
public int getNumIdle()
public String getPassword()
public 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.
password
- new value for the passwordpublic String getUrl()
url
property.url
passed to the JDBC driver to establish
connectionspublic 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.
url
- the new value for the JDBC connection urlpublic String getUsername()
username
property.username
passed to the JDBC driver to establish
connectionspublic 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.
username
- the new value for the JDBC connection usernamepublic String getValidationQuery()
validationQuery
public 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.
validationQuery
- the new value for the validation querypublic int getValidationQueryTimeout()
public void setValidationQueryTimeout(int timeout)
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.
timeout
- new validation query timeout value in secondspublic Collection getConnectionInitSqls()
public void setConnectionInitSqls(Collection connectionInitSqls)
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.
connectionInitSqls
- Collection of SQL statements to execute
on connection creationpublic boolean isAccessToUnderlyingConnectionAllowed()
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)
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.
allow
- Access to the underlying connection is granted when true.public Connection getConnection() throws SQLException
getConnection
in interface DataSource
SQLException
- if a database access error occurspublic Connection getConnection(String user, String pass) throws SQLException
getConnection
in interface DataSource
SQLException
public int getLoginTimeout() throws SQLException
getLoginTimeout
in interface CommonDataSource
SQLException
public PrintWriter getLogWriter() throws SQLException
getLogWriter
in interface CommonDataSource
SQLException
public void setLoginTimeout(int loginTimeout) throws SQLException
setLoginTimeout
in interface CommonDataSource
SQLException
public void setLogWriter(PrintWriter logWriter) throws SQLException
setLogWriter
in interface CommonDataSource
SQLException
public boolean getRemoveAbandoned()
public void setRemoveAbandoned(boolean removeAbandoned)
public int getRemoveAbandonedTimeout()
public void setRemoveAbandonedTimeout(int removeAbandonedTimeout)
public boolean getLogAbandoned()
public void setLogAbandoned(boolean logAbandoned)
public void removeConnectionProperty(String name)
public void setConnectionProperties(String connectionProperties)
public void close() throws SQLException
SQLException
public boolean isClosed()
public boolean isWrapperFor(Class<?> iface) throws SQLException
isWrapperFor
in interface Wrapper
SQLException
public <T> T unwrap(Class<T> iface) throws SQLException
unwrap
in interface Wrapper
SQLException
protected DataSource createDataSource() throws SQLException
SQLException
protected ConnectionFactory createConnectionFactory() throws SQLException
SQLException
protected void createConnectionPool()
protected void createDataSourceInstance()
protected void createPoolableConnectionFactory(ConnectionFactory driverConnectionFactory, KeyedObjectPoolFactory statementPoolFactory, AbandonedConfig configuration) throws SQLException
SQLException
protected static void validateConnectionFactory(PoolableConnectionFactory connectionFactory) throws Exception
Exception
protected void log(String message)
public Logger getParentLogger() throws SQLFeatureNotSupportedException
getParentLogger
in interface CommonDataSource
SQLFeatureNotSupportedException
Copyright © 2017. All rights reserved.