public class DatabaseConnectionPool
extends java.lang.Object
Connections
. In certain
JDBC implementations, database Connections may be expensive and
time-consuming to open. This pool maintains a LIFO stack of open
Connections in an attempt to re-use existing Connections to the database.
This class requires a JDBC driver that supports the isValid
method of java.sql.Connection
, part of the JDBC 4.0
specification in Java 6.
Constructor and Description |
---|
DatabaseConnectionPool(javax.sql.DataSource dataSource)
Constructs a pool to hold cached
Connections
to the suppied JDBC DataSource . |
Modifier and Type | Method and Description |
---|---|
void |
closeConnections()
Empties the connection pool, closing all its
Connections . |
java.sql.Connection |
getConnection()
Returns a
Connection from the connection pool. |
javax.sql.DataSource |
getDataSource()
Returns the JDBC
DataSource that owns these Connections . |
void |
releaseConnection(java.sql.Connection connection)
Releases a
Connection , returning it to the connection pool
for later re-use. |
public DatabaseConnectionPool(javax.sql.DataSource dataSource)
Connections
to the suppied JDBC DataSource
. The pool is initially empty.dataSource
- a JDBC DataSource
public javax.sql.DataSource getDataSource()
DataSource
that owns these Connections
.DataSource
public java.sql.Connection getConnection() throws java.sql.SQLException
Connection
from the connection pool.
If the pool is empty, a new Connection
is
obtained from the DataSource
.Connection
to the DataSource
java.sql.SQLException
- if a Connection cannot be obtainedpublic void releaseConnection(java.sql.Connection connection)
Connection
, returning it to the connection pool
for later re-use.connection
- a Connection to to return to the poolpublic void closeConnections()
Connections
.