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 |
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 |
releaseAsync()
Asynchronously releases the delegate
ConnectionPool instance. |
void |
releaseConnectionPool()
Releases (cleans up resources of) the internal
ConnectionPool
instance. |
void |
releaseForcibly()
Forcibly releases the delegate
ConnectionPool instance. |
void |
removeShutdownHook()
Unregisters a registered shutdown hook for this ConnectionPoolManager instance.
|
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 - protected 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 occurspublic void releaseConnectionPool()
ConnectionPool
instance. This method should be called to close all the pooled connections.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 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 releaseAsync()
ConnectionPool instance.public void releaseForcibly()
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 © 2013. All Rights Reserved.