public class DBPoolDataSource extends Object implements DataSource, ConnectionPoolListener
DataSource implementation which produces Connection
instances. This implementation internally references a ConnectionPool
which it uses to provide transparent connection pooling to the client.| Modifier and Type | Field and Description |
|---|---|
protected Driver |
driver
Driver to use for database access. |
protected static org.slf4j.Logger |
logger
SLF4J shared instance for writing log entries.
|
protected PrintWriter |
logWriter
PrintWriter instance used for logging. |
protected ConnectionPool |
pool
ConnectionPool instance used to source connections. |
protected static String |
POOL_NAME_PREFIX
Name prefix to use for
ConnectionPool instance (JNDI name is appended). |
| Constructor and Description |
|---|
DBPoolDataSource()
Creates a new
DBPoolDataSource instance. |
| Modifier and Type | Method and Description |
|---|---|
protected void |
createConnectionPool()
Creates a new
DBPoolDataSource instance. |
Connection |
getConnection()
Attempts to establish a connection with the database.
|
Connection |
getConnection(String username,
String password)
Attempts to establish a connection with the database.
|
String |
getConnectionProperty(String key)
Returns the driver property for the specified key.
|
String |
getDescription()
Returns the description of this DataSource.
|
String |
getDriverClassName()
Returns the fully-qualified class name for the JDBC driver to use.
|
int |
getIdleTimeout()
Returns the idle timeout (seconds) for connections in the underlying
ConnectionPool. |
int |
getLoginTimeout() |
PrintWriter |
getLogWriter()
Retrieves the log writer for this DataSource.
|
int |
getMaxPool()
Returns the maximum number of pooled connections in the underlying
ConnectionPool. |
int |
getMaxSize()
Returns the maximum number of connections in the underlying
ConnectionPool. |
int |
getMinPool()
Returns the minimum number of pooled connections in the underlying
ConnectionPool. |
String |
getName()
Returns the JNDI name of the
DBPoolDataSource instance. |
Logger |
getParentLogger() |
String |
getPassword()
Returns the password to use with this DataSource.
|
String |
getPasswordDecoderClassName()
Returns the fully-qualified class name of the
PasswordDecoder. |
String |
getSelectionStrategy()
Returns a string denoting the pool selection strategy to use (one of LIFO/FIFO/RANDOM).
|
String |
getUrl()
Returns the JDBC URL to use with this DataSource.
|
String |
getUser()
Returns the username to use with this DataSource.
|
String |
getValidationQuery()
Returns the SQL query string to issue for connection validation.
|
String |
getValidatorClassName()
Returns the fully-qualified class name of the
ConnectionValidator. |
boolean |
isWrapperFor(Class<?> iface) |
protected void |
log(String message)
Writes a message to the log.
|
protected void |
log(String message,
Throwable throwable)
Writes a message with a
Throwable to the log file. |
void |
maxPoolLimitExceeded(ConnectionPoolEvent evt)
Called when a check-out request causes the maxPool limit to be exceeded.
|
void |
maxPoolLimitReached(ConnectionPoolEvent evt)
Called when a check-out request causes the maxPool limit to be reached.
|
void |
maxSizeLimitError(ConnectionPoolEvent evt)
Called when a check-out request attempts to exceed the maxSize limit.
|
void |
maxSizeLimitReached(ConnectionPoolEvent evt)
Called when a check-out request causes the maxSize limit to be reached.
|
void |
poolCheckIn(ConnectionPoolEvent evt)
Called when a connection is checked back in to the pool.
|
void |
poolCheckOut(ConnectionPoolEvent evt)
Called when a connection is checked out of the pool.
|
void |
poolFlushed(ConnectionPoolEvent evt)
Called when the pool is flushed of free/unused connections.
|
void |
poolInitCompleted(ConnectionPoolEvent evt)
Called when the pool's
ObjectPool.init(int) method has completed. |
void |
poolParametersChanged(ConnectionPoolEvent evt)
Called when the pool's parameters are changed.
|
void |
poolReleased(ConnectionPoolEvent evt)
Called when the pool is released (no more events are fired by the pool after this event).
|
void |
registerShutdownHook()
Registers a shutdown hook for this ConnectionPoolManager instance
to ensure it is released if the JVM exits
|
void |
release()
Releases the delegate
ConnectionPool instance. |
void |
release(long timeout)
Releases the delegate
ConnectionPool instance allowing the
specified timeout before forcibly destroying connections. |
void |
releaseAsync()
Asynchronously releases the delegate
ConnectionPool instance. |
void |
releaseAsync(long timeout)
Asynchronously releases the delegate
ConnectionPool instance. |
void |
releaseConnectionPool()
Deprecated.
Use
releaseImmediately() instead |
void |
releaseForcibly()
Deprecated.
Use
releaseImmediately() instead |
void |
releaseImmediately()
Immediately releases the delegate
ConnectionPool instance. |
void |
removeShutdownHook()
Unregisters a registered shutdown hook for this ConnectionPoolManager instance.
|
void |
setConnectionProperty(String key,
String value)
Sets the driver property for the specified key.
|
void |
setDescription(String description)
Sets the description of this DataSource.
|
void |
setDriverClassName(String driverClassName)
Sets the class name for the JDBC driver to use.
|
void |
setIdleTimeout(int idleTimeout)
Sets the idle timeout (seconds) for connections in the underlying
ConnectionPool. |
void |
setLoginTimeout(int seconds)
Sets the maximum time in seconds that this DataSource will wait while
attempting to connect to a database.
|
void |
setLogWriter(PrintWriter out)
Sets the log writer for this DataSource to the given
PrintWriter. |
void |
setMaxPool(int maxPool)
Sets the maximum number of pooled connections in the underlying
ConnectionPool. |
void |
setMaxSize(int maxSize)
Sets the maximum number of connections in the underlying
ConnectionPool. |
void |
setMinPool(int minPool)
Sets the minimum number of pooled connections in the underlying
ConnectionPool. |
void |
setName(String name)
Sets the JNDI name of the
DBPoolDataSource instance. |
void |
setPassword(String password)
Sets the password to use with this DataSource.
|
void |
setPasswordDecoderClassName(String decoderClassName)
Sets the class name of the
PasswordDecoder to use. |
void |
setSelectionStrategy(String selection)
Sets the class name for the JDBC driver to use.
|
void |
setUrl(String url)
Sets the JDBC URL to use with this DataSource.
|
void |
setUser(String username)
Sets the username to use with this DataSource.
|
void |
setValidationQuery(String validationQuery)
Sets the SQL query string to issue for connection validation.
|
void |
setValidatorClassName(String validatorClassName)
Sets the class name of the
ConnectionValidator to use. |
String |
toString() |
<T> T |
unwrap(Class<T> iface) |
void |
validationError(ConnectionPoolEvent evt)
Called when a connection is found to be invalid.
|
protected static final String POOL_NAME_PREFIX
ConnectionPool instance (JNDI name is appended).protected static final org.slf4j.Logger logger
protected transient Driver driver
Driver to use for database access.protected transient ConnectionPool pool
ConnectionPool instance used to source connections.protected transient PrintWriter logWriter
PrintWriter instance used for logging.public DBPoolDataSource()
DBPoolDataSource instance.public void registerShutdownHook()
public void removeShutdownHook()
protected void log(String message)
message - message to writeprotected void log(String message, Throwable throwable)
Throwable to the log file.message - message to writethrowable - Throwable instance to logprotected void createConnectionPool()
throws SQLException
DBPoolDataSource instance.SQLException - if required resources cannot be located/establishedpublic Connection getConnection() throws SQLException
getConnection in interface DataSourceConnection instance, or null if unable to connectSQLException - if a database access error occurspublic Connection getConnection(String username, String password) throws SQLException
getConnection in interface DataSourceusername - the database user on whose behalf the connection is being madepassword - the user's passwordConnection instance, or null if unable to connectSQLException - if a database access error occurs@Deprecated public void releaseConnectionPool()
releaseImmediately() insteadreleaseImmediately() retained for backward
compatibility; will be removed.public String getDescription()
public void setDescription(String description)
description - descriptionpublic String getUser()
public void setUser(String username)
username - usernamepublic String getPassword()
public void setPassword(String password)
password - passwordpublic String getName()
DBPoolDataSource instance.DBPoolDataSource instancepublic void setName(String name)
DBPoolDataSource instance.name - JNDI namepublic String getDriverClassName()
public void setDriverClassName(String driverClassName)
driverClassName - fully-qualified driver class name to usepublic String getPasswordDecoderClassName()
PasswordDecoder.PasswordDecoderpublic void setPasswordDecoderClassName(String decoderClassName)
PasswordDecoder to use.
The specified class should implement the PasswordDecoder interface,
and have a no-argument constructor which can be used to instantiate it for use.decoderClassName - fully-qualified class name of password decoder to usepublic String getValidatorClassName()
ConnectionValidator.ConnectionValidatorpublic void setValidatorClassName(String validatorClassName)
ConnectionValidator to use.
The specified class should implement the ConnectionValidator
interface, and have a no-argument constructor which can be used to
instantiate it for use.validatorClassName - fully-qualified class name of validator to usepublic String getValidationQuery()
validationClassName has not
been explicitly set, in which case this query string is used with an
instance of SimpleQueryValidator.public void setValidationQuery(String validationQuery)
validationClassName has not
been explicitly set, in which case this query string is used with an
instance of SimpleQueryValidator.validationQuery - SQL query to use for validationpublic String getUrl()
public void setUrl(String url)
url - JDBC URL to usepublic int getMinPool()
ConnectionPool.ConnectionPoolpublic void setMinPool(int minPool)
ConnectionPool.minPool - minimum number of pooled connectionspublic int getMaxPool()
ConnectionPool.ConnectionPoolpublic void setMaxPool(int maxPool)
ConnectionPool.maxPool - maximum number of pooled connectionspublic int getMaxSize()
ConnectionPool.ConnectionPoolpublic void setMaxSize(int maxSize)
ConnectionPool.maxSize - maximum number of connectionspublic int getIdleTimeout()
ConnectionPool.ConnectionPoolpublic void setIdleTimeout(int idleTimeout)
ConnectionPool.idleTimeout - idle timeout (seconds)public String getConnectionProperty(String key)
key - property keypublic void setConnectionProperty(String key, String value)
key - property keyvalue - value of propertypublic String getSelectionStrategy()
public void setSelectionStrategy(String selection)
selection - string representing pool selection strategy to use (one of LIFO/FIFO/RANDOM)public PrintWriter getLogWriter()
getLogWriter in interface CommonDataSourcepublic void setLogWriter(PrintWriter out)
PrintWriter.setLogWriter in interface CommonDataSourceout - the new log writer; to disable logging, set to nullpublic void setLoginTimeout(int seconds)
DataSource object is created, the login timeout is
initially zero.setLoginTimeout in interface CommonDataSourceseconds - the DataSource login time limitpublic int getLoginTimeout()
getLoginTimeout in interface CommonDataSourcepublic void release()
ConnectionPool instance.public void release(long timeout)
ConnectionPool instance allowing the
specified timeout before forcibly destroying connections.
A negative timeout is equivalent to no timeout, and the method will wait
for items to be checked in before destruction. If timeout >= 0 then
items will be forcibly destroyed after the specified time has elapsed.timeout - timeout after which to forcibly destroy items (-1 for no timeout)public void releaseAsync()
ConnectionPool instance.public void releaseAsync(long timeout)
ConnectionPool instance.timeout - timeout after which to forcibly destroy items (-1 for no timeout)@Deprecated public void releaseForcibly()
releaseImmediately() insteadreleaseImmediately() retained for backward
compatibility; will be removed.public void releaseImmediately()
ConnectionPool instance.public boolean isWrapperFor(Class<?> iface) throws SQLException
isWrapperFor in interface WrapperSQLExceptionpublic <T> T unwrap(Class<T> iface) throws SQLException
unwrap in interface WrapperSQLExceptionpublic void poolInitCompleted(ConnectionPoolEvent evt)
ConnectionPoolListenerObjectPool.init(int) method has completed.poolInitCompleted in interface ConnectionPoolListenerevt - event instancepublic void poolCheckOut(ConnectionPoolEvent evt)
ConnectionPoolListenerpoolCheckOut in interface ConnectionPoolListenerevt - event instancepublic void poolCheckIn(ConnectionPoolEvent evt)
ConnectionPoolListenerpoolCheckIn in interface ConnectionPoolListenerevt - event instancepublic void validationError(ConnectionPoolEvent evt)
ConnectionPoolListenervalidationError in interface ConnectionPoolListenerevt - event instancepublic void maxPoolLimitReached(ConnectionPoolEvent evt)
ConnectionPoolListenermaxPoolLimitReached in interface ConnectionPoolListenerevt - event instancepublic void maxPoolLimitExceeded(ConnectionPoolEvent evt)
ConnectionPoolListenermaxPoolLimitExceeded in interface ConnectionPoolListenerevt - event instancepublic void maxSizeLimitReached(ConnectionPoolEvent evt)
ConnectionPoolListenermaxSizeLimitReached in interface ConnectionPoolListenerevt - event instancepublic void maxSizeLimitError(ConnectionPoolEvent evt)
ConnectionPoolListenermaxSizeLimitError in interface ConnectionPoolListenerevt - event instancepublic void poolFlushed(ConnectionPoolEvent evt)
ConnectionPoolListenerpoolFlushed in interface ConnectionPoolListenerevt - event instancepublic void poolParametersChanged(ConnectionPoolEvent evt)
ConnectionPoolListenerpoolParametersChanged in interface ConnectionPoolListenerevt - event instancepublic void poolReleased(ConnectionPoolEvent evt)
ConnectionPoolListenerpoolReleased in interface ConnectionPoolListenerevt - event instancepublic Logger getParentLogger() throws SQLFeatureNotSupportedException
getParentLogger in interface CommonDataSourceSQLFeatureNotSupportedExceptionCopyright © 2016. All rights reserved.