com.waveset.util
Class ConnectionPool

java.lang.Object
  extended bycom.waveset.util.ConnectionPool
Direct Known Subclasses:
KiddiePool

public class ConnectionPool
extends java.lang.Object

A general purpose JDBC connection pool. There is normally a singleton pool object for an application, but you can have more than one if you need to manage a private pool.

JDBC drivers must be registered before you can establish a connection. There are three ways to do this: 1) the application handles it 2) the application calls ConnectionPool.registerDriver 3) the application calls getConnection and passes the driver info

If you only use one driver, the easiest thing is to call ConnectionPool.registerDriver during static initialization. If the driver can be specified at runtime as is the case with some resource adapters, then you can call the getConnection method that takes driver information.


Nested Class Summary
protected static class ConnectionPool.Bucket
          A helper class that maintains a list of connections for a particular key.
 
Field Summary
protected  boolean _disabled
          Flag to disable connection pooling.
static java.lang.String code_id
           
 
Constructor Summary
ConnectionPool()
          Create a private connection pool
 
Method Summary
protected  void addPooledConnection(PooledConnection con)
          Add a connection back to the pool.
 void destroy(java.sql.Connection con)
          Release a connection that has been misbehaving.
 void dump()
          Dump information about the connections in the pool.
 void dump(java.lang.StringBuffer sb)
           
protected  void dumpConnections(java.lang.StringBuffer sb)
          Dumps the connection keys (JDBC URL,username) and the number of free connections into the provided buffer
static void dumpGlobal()
           
static void dumpGlobal(java.lang.StringBuffer sb)
           
static void dumpPrivate(java.lang.StringBuffer sb)
           
 void flush()
          Close all connections in the pool.
static void flushConnections()
          Flush all connections in the global connection pool.
static void flushPrivate()
           
 java.sql.Connection getConnection(java.lang.String url, java.lang.String user, java.lang.String password)
          Obtain a connection.
 java.sql.Connection getConnection(java.lang.String url, java.lang.String user, java.lang.String password, boolean checkConnection)
          Obtain a connection.
 java.sql.Connection getConnection(java.lang.String driverClass, java.lang.String driverPrefix, java.lang.String url, java.lang.String user, java.lang.String password)
          Obtain a connection.
 java.sql.Connection getConnection(java.lang.String driverClass, java.lang.String driverPrefix, java.lang.String url, java.lang.String user, java.lang.String password, boolean checkConnection)
          Obtain a connection.
static ConnectionPool getConnectionPool()
          Get the global connection pool.
protected  int getFreeConnectionCount()
           
 int getVersion()
          Get the pool version number
 void incCreateStats()
          Bump the active and total counts.
 void incDestroyStats()
          Called by destroy below.
protected static void println(java.lang.Object o)
           
static void registerDriver(java.lang.String driverClass)
           
static void registerDriver(java.lang.String driverClass, java.lang.String prefix)
          Helper method to register a driver.
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Field Detail

code_id

public static final java.lang.String code_id
See Also:
Constant Field Values

_disabled

protected boolean _disabled
Flag to disable connection pooling. Initialized to the value of the connectionPool.disable property in Waveset.properties.

Constructor Detail

ConnectionPool

public ConnectionPool()
Create a private connection pool

Method Detail

getConnectionPool

public static ConnectionPool getConnectionPool()
Get the global connection pool.


flushConnections

public static void flushConnections()
Flush all connections in the global connection pool. A diagnostic method that might be used to rid oursleves of corrupt connections.


getVersion

public int getVersion()
Get the pool version number


registerDriver

public static void registerDriver(java.lang.String driverClass)
                           throws WavesetException
Throws:
WavesetException

registerDriver

public static void registerDriver(java.lang.String driverClass,
                                  java.lang.String prefix)
                           throws ConfigurationError
Helper method to register a driver. The application can do this themselves, but since you're likely to do the same little error-handling dance below, you might as well call this.

UPDATE: We're trying to reduce dependence on maintaining the prefix in the resource adapters. Instead, we now maintain a map of drivers we've already registerd which will keep us from hitting the DriverManager so often. I'm not sure if that's even a problem, but its safest to avoid if we can.

Throws:
ConfigurationError

addPooledConnection

protected void addPooledConnection(PooledConnection con)
Add a connection back to the pool. This is synchronized so keep it short.


incDestroyStats

public void incDestroyStats()
Called by destroy below. Instead of returning it to the pool, we adjust some statistics.


incCreateStats

public void incCreateStats()
Bump the active and total counts. Called by getConnection when it has to create a new connection.


getConnection

public java.sql.Connection getConnection(java.lang.String url,
                                         java.lang.String user,
                                         java.lang.String password)
                                  throws WavesetException
Obtain a connection. This interface assumes that the driver has already been registered. Connections retrieved from the pool will be checked to see if they are still OK.

Throws:
WavesetException

getConnection

public java.sql.Connection getConnection(java.lang.String url,
                                         java.lang.String user,
                                         java.lang.String password,
                                         boolean checkConnection)
                                  throws WavesetException
Obtain a connection. This interface assumes that the driver has already been registered. If checkConnection is true, then connections retrieved from the pool will be checked to see if they are still OK.

Throws:
WavesetException

getConnection

public java.sql.Connection getConnection(java.lang.String driverClass,
                                         java.lang.String driverPrefix,
                                         java.lang.String url,
                                         java.lang.String user,
                                         java.lang.String password)
                                  throws WavesetException
Obtain a connection. This interface allows you to specify a driver. Driver prefix is optional, if supplied we will ask the DriverManager if it already has a driver registered for this prefix before registering the new one. This may be a slight performance win, but I'm not sure. Connections retrieved from the pool will be checked to see if they are still OK.

Throws:
WavesetException

getConnection

public java.sql.Connection getConnection(java.lang.String driverClass,
                                         java.lang.String driverPrefix,
                                         java.lang.String url,
                                         java.lang.String user,
                                         java.lang.String password,
                                         boolean checkConnection)
                                  throws WavesetException
Obtain a connection. This interface allows you to specify a driver. Driver prefix is optional, if supplied we will ask the DriverManager if it already has a driver registered for this prefix before registering the new one. This may be a slight performance win, but I'm not sure. If checkConnection is true, then connections retrieved from the pool will be checked to see if they are still OK.

Throws:
WavesetException

flush

public void flush()
Close all connections in the pool.


destroy

public void destroy(java.sql.Connection con)
Release a connection that has been misbehaving. You would call this if you have reason to believe that the connection is corrupt and don't want to return it to the pool.

I don't know of a way to reliably test this within ConnectionPool other than to try to call some harmless request such as rollback(), but that would cause another server round trip. For now, I'm assuming the application can handle that.


dump

public void dump()
Dump information about the connections in the pool.


dump

public void dump(java.lang.StringBuffer sb)

getFreeConnectionCount

protected int getFreeConnectionCount()
Returns:
total number of free connections in the pools

dumpConnections

protected void dumpConnections(java.lang.StringBuffer sb)
Dumps the connection keys (JDBC URL,username) and the number of free connections into the provided buffer

Parameters:
sb - buffer to write messages to

println

protected static void println(java.lang.Object o)

dumpGlobal

public static void dumpGlobal()

dumpGlobal

public static void dumpGlobal(java.lang.StringBuffer sb)

dumpPrivate

public static void dumpPrivate(java.lang.StringBuffer sb)

flushPrivate

public static void flushPrivate()