|
||||||||||
PREV CLASS NEXT CLASS | FRAMES NO FRAMES | |||||||||
SUMMARY: NESTED | FIELD | CONSTR | METHOD | DETAIL: FIELD | CONSTR | METHOD |
java.lang.Objectbb.jdbc.DBConnManager
public class DBConnManager
This class manages access to database Connections.
The public api is just the getConnection
and returnConnection
methods.
This class is multithread safe: most of its state is immutable (both its immediate state, as well as the deep state of its fields).
The sole exception is the pool
field, which is guarded by synchronized blocks on itself.
Field Summary | |
---|---|
private static String |
jdbcUrl
|
private static String |
jdcDriver
|
private static BlockingQueue<Connection> |
pool
|
private static int |
poolSize
|
Constructor Summary | |
---|---|
private |
DBConnManager()
This sole private constructor suppresses the default (public) constructor, ensuring non-instantiability outside of this class. |
Method Summary | |
---|---|
private static Connection |
ensurePristine(Connection connection)
Returns newConnection if connection has already been closed. |
static Connection |
getConnection()
Returns a Connection instance. |
private static Connection |
newConnection()
Returns a newly constructed Connection instance. |
static void |
returnConnection(Connection connection)
Every Connection returned by getConnection must be sent back to this class by calling this method. |
Methods inherited from class java.lang.Object |
---|
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait |
Field Detail |
---|
private static final String jdcDriver
private static final String jdbcUrl
private static final int poolSize
private static final BlockingQueue<Connection> pool
Constructor Detail |
---|
private DBConnManager()
Method Detail |
---|
private static Connection newConnection() throws SQLException
Note: the result has auto commit turned off and (since is new) should have nothing to be committed and no warnings present.
Note: this method assumes that the driver class has already been loaded.
SQLException
- if a database access error occurspublic static Connection getConnection() throws InterruptedException
Contract: the result is never null, has auto commit turned off, and is clear of all warnings.
InterruptedException
- if the calling Thread is interrupted while waiting for a Connection to become availablepublic static void returnConnection(Connection connection)
getConnection
must be sent back to this class by calling this method.
Failure to call this method will ultimately deprive this class of any Connections to return, which may choke your application. Therefore, the caller of getConnection must ensure that this method receives back all Connections. Here is typical usage:
Connection connection = null;
try {
connection = DBConnManager.getConnection();
// do something with connection
}
finally {
DBConnManager.returnConnection(connection);
}
The implementation here immediately returns if connection is null.
Otherwise, it adds
back to ensurePristine
(connection)pool
.
private static Connection ensurePristine(Connection connection) throws IllegalArgumentException, SQLException
newConnection
if connection has already been closed.
Otherwise, cleans up connection as follows:
If all these steps succeed, then onnection is returned. Else an attempt is made to close connection and return newConnection.
IllegalArgumentException
- if connection is null
SQLException
- if a database access error occurs
|
||||||||||
PREV CLASS NEXT CLASS | FRAMES NO FRAMES | |||||||||
SUMMARY: NESTED | FIELD | CONSTR | METHOD | DETAIL: FIELD | CONSTR | METHOD |