public class DBConnectionPool extends java.lang.Object implements Connectable, javax.sql.DataSource, CloseableComponent
Basically a thread owns a connection until he explicitly releases it with
the releaseConnection()
call. Thus If a thread tries to fetch a
connection twice he will get exactly the same one he has fetched before.
Transacting connections however are NOT released that way but solely by
calling commit()
or rollback()
.
Note that you can specify the min,max numbers for connections in the pool. The pool ensures, that at least min connections are always available. If i.e. a client has closed a connection the pool will recreate one to ensure the min value. If more than min connections are requested by clients, then the pool is allowed to create new connections up to max, which are automatically closed down to min again if they aren't needed any more.
There are three ways to get a connection:
getConnection()
getConnection(int)
ConnectionNotAvailableException
if no connection has become available within that time.
getConnectionNoWait()
ConnectionNotAvailableException
otherwise.
close()
call if no more needed.
This cleanly closes all database connections. Any getConnection()
requests during shutdown will result in an according ShuttingDownException being thrown.Constructor and Description |
---|
DBConnectionPool()
Creates an empty DBConnectionPool which is not connected yet to the database.
|
DBConnectionPool(Credential cred)
Main constructor for the DBConnectionPool
|
DBConnectionPool(java.lang.String jdbcDriver,
java.lang.String jdbcURL,
java.lang.String jdbcUser,
java.lang.String jdbcPassword) |
DBConnectionPool(java.lang.String jdbcDriver,
java.lang.String jdbcURL,
java.lang.String jdbcUser,
java.lang.String jdbcPassword,
int minConns,
int maxConns) |
DBConnectionPool(java.lang.String jdbcDriver,
java.lang.String jdbcURL,
java.lang.String jdbcUser,
java.lang.String jdbcPassword,
java.lang.String dbSchema) |
DBConnectionPool(java.lang.String jdbcDriver,
java.lang.String jdbcURL,
java.lang.String jdbcUser,
java.lang.String jdbcPassword,
java.lang.String dbSchema,
int minConns,
int maxConns)
Main constructor for the DBConnectionPool
|
Modifier and Type | Method and Description |
---|---|
void |
addConnectionListener(DBConnectionEvent.Listener l)
Listeners are notified about a connect and a disconnect
|
int |
begin()
Acquires a transacting connection for the current thread.
|
int |
begin(int txIsolationLevel)
Same as begin() but sets the isolation level to TRANSACTION_REPEATABLE_READ.
|
void |
close()
This method tells the connection pool to shutdown immediately.
|
void |
close(int shutdownDelay)
This method implements the "shutting down" process.
|
int |
commit()
Releases the transacting connection of the current thread.
|
void |
connect(Credential cred)
Connects to the database with the given credential information
|
void |
disconnect()
Same as close()
|
void |
dumpPoolParams(java.io.OutputStream os)
Prints the runtime parameters of this connection pool into the given stream.
|
void |
dumpPoolStatus(java.io.PrintStream ps)
Prints the use-time of each connection in the pool.
|
void |
dumpUsageSummary(java.io.PrintStream ps)
Prints the use-time of each connection in the pool.
|
DBPoolConnection |
getConnection()
Returns the next available connection.
|
DBPoolConnection |
getConnection(int timeout)
This method returns the next available connection.
|
java.sql.Connection |
getConnection(java.lang.String username,
java.lang.String password)
Delegates directly to getConnection().
|
int |
getConnectionCount()
Returns the total number of connections in the pool (free plus used plus
transacting connections).
|
DBPoolConnection |
getConnectionNoWait()
Immediately returns the next available connection.
|
Credential |
getCredential() |
DBSupport |
getDBSupport()
Returns the
DBSupport which provides generic database access routines. |
java.lang.String |
getDriverName() |
DumpString |
getDumpString(DumpString s)
Fills the given DumpString with the runtime parameters of this connection pool.
|
int |
getFreeCount()
Returns the number of free connections.
|
java.lang.String |
getJdbcDriver() |
java.lang.String |
getJdbcPassword() |
java.lang.String |
getJdbcURL() |
java.lang.String |
getJdbcUser() |
int |
getLoginTimeout()
Returns the login timeout in seconds
|
java.io.PrintWriter |
getLogWriter() |
int |
getMaxConns() |
int |
getMinConns() |
java.util.logging.Logger |
getParentLogger() |
RdbmsInfo |
getRdbmsInfo()
Returns the the
RdbmsInfo object describing the database. |
RdbmsSupport |
getRdbmsSupport()
Returns the the
RdbmsSupport which encapsulates rdbms specific features. |
java.lang.String |
getStatusString()
Returns the status of connections as a string.
|
int |
getTransactingCount()
Returns the number of connections in transaction.
|
DBConnectionMap |
getTransactionMap() |
int |
getUsedCount()
Returns the number of connections in use.
|
DBConnectionMap |
getUsedMap() |
protected DBPoolConnection |
initConnection(DBPoolConnection newConn)
Is called for each connection that has been newly created.
|
boolean |
isAlive()
Returns true as long as the connection pool has not yet been closed.
|
boolean |
isBusy()
Returns true as long as the connection pool has not yet been closed.
|
boolean |
isConnected()
Returns true as long as the connection pool has connections.
|
static boolean |
isDebug()
Tells whether the DEBUG flag is set in the pool.
|
boolean |
isTransacting()
Returns true if the current thread is running a transaction.
|
boolean |
isWrapperFor(java.lang.Class<?> iface) |
void |
releaseConnection()
Returns the connection of the current thread to the pool.
|
void |
removeConnectionListener(DBConnectionEvent.Listener l) |
boolean |
rollback()
Releases the transacting connection of the current thread.
|
static void |
setDebug(boolean state)
Sets the DEBUG flag.
|
void |
setLoginTimeout(int seconds) |
void |
setLogWriter(java.io.PrintWriter out) |
<T> T |
unwrap(java.lang.Class<T> iface) |
public DBConnectionPool()
connect(Credential)
in order to create the connection. Note that
you may register as a ConnectionEvent.Listener if you want to be notified about
connection state changes.public DBConnectionPool(Credential cred) throws java.lang.Exception
cred
- a Credential object with the connection parametersjava.lang.Exception
- Thrown if the connection to the database failedpublic DBConnectionPool(java.lang.String jdbcDriver, java.lang.String jdbcURL, java.lang.String jdbcUser, java.lang.String jdbcPassword) throws java.lang.Exception
java.lang.Exception
DBConnectionPool(String jdbcDriver, String jdbcURL, String jdbcUser, String jdbcPassword, String dbSchema, int minConns, int maxConns)
public DBConnectionPool(java.lang.String jdbcDriver, java.lang.String jdbcURL, java.lang.String jdbcUser, java.lang.String jdbcPassword, java.lang.String dbSchema) throws java.lang.Exception
java.lang.Exception
DBConnectionPool(String jdbcDriver, String jdbcURL, String jdbcUser, String jdbcPassword, String dbSchema, int minConns, int maxConns)
public DBConnectionPool(java.lang.String jdbcDriver, java.lang.String jdbcURL, java.lang.String jdbcUser, java.lang.String jdbcPassword, int minConns, int maxConns) throws java.lang.Exception
java.lang.Exception
DBConnectionPool(String jdbcDriver, String jdbcURL, String jdbcUser, String jdbcPassword, String dbSchema, int minConns, int maxConns)
public DBConnectionPool(java.lang.String jdbcDriver, java.lang.String jdbcURL, java.lang.String jdbcUser, java.lang.String jdbcPassword, java.lang.String dbSchema, int minConns, int maxConns) throws java.lang.Exception
jdbcDriver
- The JDBC fully-qualified Driver class namejdbcURL
- The JDBC connection URLjdbcUser
- Database login namejdbcPassword
- Database passworddbSchema
- Database schemaminConns
- Minimum number of pool connections to start withmaxConns
- Maximum number of connections that the pool can grow up tojava.lang.Exception
- Thrown if the connection to the database failedpublic DBConnectionMap getUsedMap()
public DBConnectionMap getTransactionMap()
public void connect(Credential cred) throws java.lang.Exception
connect
in interface Connectable
java.lang.Exception
public RdbmsSupport getRdbmsSupport()
RdbmsSupport
which encapsulates rdbms specific features.public RdbmsInfo getRdbmsInfo()
RdbmsInfo
object describing the database.public void addConnectionListener(DBConnectionEvent.Listener l)
addConnectionListener
in interface DBConnectionEvent.Source
public void removeConnectionListener(DBConnectionEvent.Listener l)
removeConnectionListener
in interface DBConnectionEvent.Source
public DBSupport getDBSupport()
DBSupport
which provides generic database access routines.public Credential getCredential()
public java.lang.String getJdbcDriver()
public java.lang.String getJdbcURL()
public java.lang.String getJdbcUser()
public java.lang.String getJdbcPassword()
public int getMinConns()
public int getMaxConns()
public java.lang.String getDriverName()
public DBPoolConnection getConnectionNoWait() throws ConnectionNotAvailableException, ShuttingDownException
ConnectionNotAvailableException
- Thrown if no connection is available immediatelyShuttingDownException
- Thrown if the connection pool is shutting downpublic DBPoolConnection getConnection() throws ShuttingDownException
getConnection
in interface javax.sql.DataSource
ShuttingDownException
- Thrown if the connection pool is
shutting down.public DBPoolConnection getConnection(int timeout) throws ShuttingDownException, ConnectionNotAvailableException
ConnectionNotAvailableException
- Thrown if no connection is availableShuttingDownException
- Thrown if the connection pool is shutting downpublic void releaseConnection() throws ConnNeverTakenException
public void disconnect()
disconnect
in interface Connectable
public void close()
close
in interface java.lang.AutoCloseable
public void close(int shutdownDelay)
If there are still connections in use this process will allow any running transactions a shutdownDelay "courtesy timeout" in order to give them a chance to complete.
shutdownDelay
- millis before shutting down, '-1' = immediate (kill
all user threads) , '0' = wait unlimited until all users are gone, '>0' =
wait specified amount of millis for users to complete their work.public int getConnectionCount()
public boolean isAlive()
public boolean isBusy()
public boolean isConnected()
isConnected
in interface Connectable
public boolean isTransacting()
public int getTransactingCount()
public int getUsedCount()
public int getFreeCount()
public java.lang.String getStatusString()
public void dumpPoolStatus(java.io.PrintStream ps)
public void dumpUsageSummary(java.io.PrintStream ps)
public void dumpPoolParams(java.io.OutputStream os)
public DumpString getDumpString(DumpString s)
public int begin() throws java.sql.SQLException
Note that nested transactions are supported. Thus begin() may be called n times by the same thread, each call increasing the nesting level. Accordingly commit() must be called n times decreasing the nesting level again. If nesting level 0 is reached by a commit() call then the database COMMIT is actually performed. A rollback() call at any nesting level will immediately break the transaction and fall back to nesting level 0.
java.sql.SQLException
public int begin(int txIsolationLevel) throws java.sql.SQLException
java.sql.SQLException
public int commit() throws java.sql.SQLException, NotInTransactionException
java.sql.SQLException
NotInTransactionException
public boolean rollback()
Note that this method does intentionally NOT throw an Exception. If the rollback fails in the database then there is nothing we can do about it anyway.
public static void setDebug(boolean state)
public static boolean isDebug()
protected DBPoolConnection initConnection(DBPoolConnection newConn) throws java.lang.Exception
java.lang.Exception
public java.sql.Connection getConnection(java.lang.String username, java.lang.String password) throws java.sql.SQLException
getConnection
in interface javax.sql.DataSource
java.sql.SQLException
public int getLoginTimeout()
getLoginTimeout
in interface javax.sql.CommonDataSource
public java.io.PrintWriter getLogWriter()
getLogWriter
in interface javax.sql.CommonDataSource
public void setLoginTimeout(int seconds)
setLoginTimeout
in interface javax.sql.CommonDataSource
public void setLogWriter(java.io.PrintWriter out)
setLogWriter
in interface javax.sql.CommonDataSource
public boolean isWrapperFor(java.lang.Class<?> iface) throws java.sql.SQLException
isWrapperFor
in interface java.sql.Wrapper
java.sql.SQLException
public <T> T unwrap(java.lang.Class<T> iface) throws java.sql.SQLException
unwrap
in interface java.sql.Wrapper
java.sql.SQLException
public java.util.logging.Logger getParentLogger() throws java.sql.SQLFeatureNotSupportedException
getParentLogger
in interface javax.sql.CommonDataSource
java.sql.SQLFeatureNotSupportedException
Copyright © 2014 EsprIT-Systems. All Rights Reserved.