edu.northwestern.at.utils.db.jdbc
Class SimpleConnectionPool

java.lang.Object
  extended by java.util.Observable
      extended by edu.northwestern.at.utils.db.jdbc.SimpleConnectionPool

public class SimpleConnectionPool
extends java.util.Observable

A simple JDBC connection pool.

Pool parameters are specified via a Properties object passed to the constructor. The following properties are used:

The pool starts out empty (no connections). When a connection is requested from the pool, if maxPoolSize connections have already been checked out, an exception is thrown. Otherwise, if the pool is not empty, the most recently used connection is checked out from the pool, tested, and returned. If the pool is empty, a new connection is created and returned.

Connections are tested when they are checked out. If a connection is bad, it is closed and a new one is created. The test query statement is "select 1".

Simple connection pools are observable. Observers are notified whenever a bad connection is detected in the test and whenever it takes more than one try to obtain a new connection. The parameter passed to the observer is an array of objects of length 2. For a bad connection, the first element is the Integer 1 and the second element is the exception describing the error, if any. For more than one try, the first element is the Integer 2 and the second element is the number of tries.

The pool is examined every minute. Any connections in the pool which have not been used for idleTimout seconds are closed and removed from the pool.

Simple connection pools are thread-safe.


Constructor Summary
SimpleConnectionPool(java.util.Properties poolProperties)
          Creates a connection pool.
 
Method Summary
 void close()
          Closes the pool.
 java.sql.Connection getConnection()
          Gets a connection from the pool.
 void releaseConnection(java.sql.Connection connection)
          Releases a connection back to the pool.
 
Methods inherited from class java.util.Observable
addObserver, clearChanged, countObservers, deleteObserver, deleteObservers, hasChanged, notifyObservers, notifyObservers, setChanged
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Constructor Detail

SimpleConnectionPool

public SimpleConnectionPool(java.util.Properties poolProperties)
                     throws java.sql.SQLException
Creates a connection pool.

Parameters:
poolProperties - Properties for pool.
Throws:
java.sql.SQLException
Method Detail

getConnection

public java.sql.Connection getConnection()
                                  throws java.sql.SQLException
Gets a connection from the pool.

Returns:
A connection.
Throws:
java.sql.SQLException

releaseConnection

public void releaseConnection(java.sql.Connection connection)
Releases a connection back to the pool.

Parameters:
connection - The connection to return to the pool.

close

public void close()
Closes the pool.

The pool may not be reused after it is closed. All connections are closed.