bb.jdbc
Class DBConnManager

java.lang.Object
  extended by bb.jdbc.DBConnManager

public class DBConnManager
extends Object

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.

Author:
Brent Boyer

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

jdcDriver

private static final String jdcDriver
See Also:
Constant Field Values

jdbcUrl

private static final String jdbcUrl
See Also:
Constant Field Values

poolSize

private static final int poolSize
See Also:
Constant Field Values

pool

private static final BlockingQueue<Connection> pool
Constructor Detail

DBConnManager

private DBConnManager()
This sole private constructor suppresses the default (public) constructor, ensuring non-instantiability outside of this class.

Method Detail

newConnection

private static Connection newConnection()
                                 throws SQLException
Returns a newly constructed Connection instance.

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.

Throws:
SQLException - if a database access error occurs

getConnection

public static Connection getConnection()
                                throws InterruptedException
Returns a Connection instance.

Contract: the result is never null, has auto commit turned off, and is clear of all warnings.

Throws:
InterruptedException - if the calling Thread is interrupted while waiting for a Connection to become available

returnConnection

public static void returnConnection(Connection connection)
Every Connection returned by 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 ensurePristine(connection) back to pool.


ensurePristine

private static Connection ensurePristine(Connection connection)
                                  throws IllegalArgumentException,
                                         SQLException
Returns newConnection if connection has already been closed.

Otherwise, cleans up connection as follows:

  1. turns off its auto commit
  2. calls its commit method
  3. clears its warnings

If all these steps succeed, then onnection is returned. Else an attempt is made to close connection and return newConnection.

Throws:
IllegalArgumentException - if connection is null
SQLException - if a database access error occurs