Google

: Class ConnectionPoolManager

com.bitmechanic.sql
Class ConnectionPoolManager

java.lang.Object
  |
  +--com.bitmechanic.sql.ConnectionPoolManager

public class ConnectionPoolManager
extends java.lang.Object
implements java.sql.Driver, java.lang.Runnable

Implements java.sql.Driver and controls access to connections in the pool. Here's how to use it:

   // When your application starts, initialize the manager.  If you want
   // the pool to reap unused connections for you (recommended), then pass
   // an int to the constructor.  this tells the pool how many seconds to
   // wait between reaping connections in the pool.
   ConnectionPoolManager mgr = new ConnectionPoolManager(300);
 
   // Load the driver into the VM like you would do w/o the pool
   Class.forName("exgwe.sql.gweMysqlDriver").newInstance();
 
   // Add one alias to the pool for each JDBC datasource you wish to connect
   // to.  From this point forward any objects that need connection handles
   // do not need to know the url, username, or password of the databse.
   // They simply need the "alias" you named the pool with here
   mgr.addAlias("myalias", "exgwe.sql.gweMysqlDriver",
                "jdbc:mysql://localhost:3306/mydb",
                "username", "password",
                10,  // max connections to open
                300, // seconds a connection can be idle before it is closed
                120, // seconds a connection can be checked out by a thread
                     // before it is returned back to the pool
                30,  // number of times a connection can be re-used before 
                     // connection to database is closed and re-opened
                     // (optional parameter)
             false); // specifies whether to cache statements
                        (optional parameter.  set to 'true' by default.)
  
   // Later in your code, use the JDBC DriverManager to obtain a
   // connection manually
   Connection conn = DriverManager.getConnection(ConnectionPoolManager.URL_PREFIX +
                                   "myalias", null, null);

   // Calling conn.close() returns the connection back to the pool
   conn.close();
 
You can also call methods on the pool directly if you want to gather statistics on the pool during runtime (to see if you need more connections in the pool for example):
   // First get a ref to the pool for the alias
   ConnectionPool pool = mgr.getPool("myalias");

   // Then call methods on it
   System.out.println(pool.getNumWaits() + " threads have had to wait() for
                      connection handles.");
   System.out.println("Connections have been checked out from the pool " +
                       pool.getNumRequests() + " times.");
 

Version:
$Id: ConnectionPoolManager.java,v 1.8 2001/05/25 05:45:12 pixel Exp $
Author:
James Cooper
See Also:
ConnectionPool

Field Summary
static java.lang.String URL_PREFIX
           
 
Constructor Summary
ConnectionPoolManager()
          Creates a pool that doesn't monitor itself to check for idle/stale connections.
ConnectionPoolManager(int monitorInterval)
          Creates a pool with a monitoring thread that checks the pool to make sure no connections are stale or idle.
 
Method Summary
 boolean acceptsURL(java.lang.String url)
          Returns true of url starts with URL_PREFIX
 void addAlias(ConnectionPool pool)
          Adds an alias to the pool using a supplied ConnectionPool object.
 void addAlias(java.lang.String alias, java.lang.String driver, java.lang.String url, java.lang.String username, java.lang.String password, int maxConn, int idleTimeout, int checkoutTimeout)
          Adds an alias to the pool.
 void addAlias(java.lang.String alias, java.lang.String driver, java.lang.String url, java.lang.String username, java.lang.String password, int maxConn, int idleTimeout, int checkoutTimeout, int maxCheckout)
          Adds an alias to the pool.
 void addAlias(java.lang.String alias, java.lang.String driver, java.lang.String url, java.lang.String username, java.lang.String password, int maxConn, int idleTimeout, int checkoutTimeout, int maxCheckout, boolean cacheStatements)
          Adds an alias to the pool.
 void addAlias(java.lang.String alias, java.lang.String driver, java.lang.String url, java.lang.String username, java.lang.String password, int maxConn, int idleTimeout, int checkoutTimeout, int maxCheckout, int rowPrefetch)
          Adds an alias to the pool.
 void addAlias(java.lang.String alias, java.lang.String driver, java.lang.String url, java.lang.String username, java.lang.String password, int maxConn, int idleTimeout, int checkoutTimeout, int maxCheckout, int rowPrefetch, boolean cacheStatements)
          Adds an alias to the pool.
 java.sql.Connection connect(java.lang.String url, java.util.Properties props)
          Returns a connection from pool
 java.lang.String dumpInfo()
          Dump some information about all connections and pools into a String
 int getMajorVersion()
           
 int getMinorVersion()
           
 ConnectionPool getPool(java.lang.String alias)
          Returns the pool with the supplied alias
 java.util.Enumeration getPools()
          Returns an Enumeration of the ConnectionPool objects currently created
 java.sql.DriverPropertyInfo[] getPropertyInfo(java.lang.String str, java.util.Properties props)
           
 boolean jdbcCompliant()
          Always returns false since I haven't sent this code to Sun for approval
 void removeAlias(java.lang.String alias)
          Closes all Connections in the pool with the supplied alias
 void run()
          Monitors each ConnectionPool and makes sure that no connection has gone idle or has been checked out for too long.
 void setTracing(boolean on)
          Turns tracing on or off.
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Field Detail

URL_PREFIX

public static final java.lang.String URL_PREFIX
Constructor Detail

ConnectionPoolManager

public ConnectionPoolManager()
                      throws java.sql.SQLException
Creates a pool that doesn't monitor itself to check for idle/stale connections. Use this constructor only if you know what you're doing and have a good reason to not use the monitor thread.

ConnectionPoolManager

public ConnectionPoolManager(int monitorInterval)
                      throws java.sql.SQLException
Creates a pool with a monitoring thread that checks the pool to make sure no connections are stale or idle.
Parameters:
monitorInterval - number of seconds between checks on the pool
Method Detail

addAlias

public void addAlias(java.lang.String alias,
                     java.lang.String driver,
                     java.lang.String url,
                     java.lang.String username,
                     java.lang.String password,
                     int maxConn,
                     int idleTimeout,
                     int checkoutTimeout)
              throws java.lang.ClassNotFoundException,
                     java.lang.InstantiationException,
                     java.lang.IllegalAccessException
Adds an alias to the pool. This does call Class.forName().newInstance() on the JDBC driver, so you don't have to call that yourself.

idleTimeout and checkoutTimeout are expressed in seconds

maxCheckouts is unlimited

cacheStatements is set to 'true' by default

Parameters:
alias - Name of the pool
driver - Classname of JDBC driver to use
url - JDBC URL to connect to
username - JDBC username to connect as
password - username's password in the database
maxConn - Maximum number of connections to open; When this limit is reached, threads requesting a connection are queued until a connection becomes available
idleTimeout - Maximum number of seconds a Connection can go unused before it is closed
checkoutTimeout - Maximum number of seconds a Thread can checkout a Connection before it is closed and returned to the pool. This is a protection against the Thread dying and leaving the Connection checked out indefinately

addAlias

public void addAlias(java.lang.String alias,
                     java.lang.String driver,
                     java.lang.String url,
                     java.lang.String username,
                     java.lang.String password,
                     int maxConn,
                     int idleTimeout,
                     int checkoutTimeout,
                     int maxCheckout)
              throws java.lang.ClassNotFoundException,
                     java.lang.InstantiationException,
                     java.lang.IllegalAccessException
Adds an alias to the pool. This does call Class.forName().newInstance() on the JDBC driver, so you don't have to call that yourself.

idleTimeout and checkoutTimeout are expressed in seconds

cacheStatements is set to 'true' by default

Parameters:
alias - Name of the pool
driver - Classname of JDBC driver to use
url - JDBC URL to connect to
username - JDBC username to connect as
password - username's password in the database
maxConn - Maximum number of connections to open; When this limit is reached, threads requesting a connection are queued until a connection becomes available
idleTimeout - Maximum number of seconds a Connection can go unused before it is closed
checkoutTimeout - Maximum number of seconds a Thread can checkout a Connection before it is closed and returned to the pool. This is a protection against the Thread dying and leaving the Connection checked out indefinately
maxCheckout - If this is greater than 0, the number of times a Connection may be checked out before it is closed. Used as a safeguard against cursor leak, which occurs if you don't call ResultSet.close() and Statement.close()

addAlias

public void addAlias(java.lang.String alias,
                     java.lang.String driver,
                     java.lang.String url,
                     java.lang.String username,
                     java.lang.String password,
                     int maxConn,
                     int idleTimeout,
                     int checkoutTimeout,
                     int maxCheckout,
                     boolean cacheStatements)
              throws java.lang.ClassNotFoundException,
                     java.lang.InstantiationException,
                     java.lang.IllegalAccessException
Adds an alias to the pool. This does call Class.forName().newInstance() on the JDBC driver, so you don't have to call that yourself.

idleTimeout and checkoutTimeout are expressed in seconds

Parameters:
alias - Name of the pool
driver - Classname of JDBC driver to use
url - JDBC URL to connect to
username - JDBC username to connect as
password - username's password in the database
maxConn - Maximum number of connections to open; When this limit is reached, threads requesting a connection are queued until a connection becomes available
idleTimeout - Maximum number of seconds a Connection can go unused before it is closed
checkoutTimeout - Maximum number of seconds a Thread can checkout a Connection before it is closed and returned to the pool. This is a protection against the Thread dying and leaving the Connection checked out indefinately
maxCheckout - If this is greater than 0, the number of times a Connection may be checked out before it is closed. Used as a safeguard against cursor leak, which occurs if you don't call ResultSet.close() and Statement.close()
cacheStatements - If set to true, the PooledConnection will reuse the same Statement object when conn.createStatement() is called. It will also cache PreparedStatements.

addAlias

public void addAlias(java.lang.String alias,
                     java.lang.String driver,
                     java.lang.String url,
                     java.lang.String username,
                     java.lang.String password,
                     int maxConn,
                     int idleTimeout,
                     int checkoutTimeout,
                     int maxCheckout,
                     int rowPrefetch)
              throws java.lang.ClassNotFoundException,
                     java.lang.InstantiationException,
                     java.lang.IllegalAccessException
Adds an alias to the pool. This does call Class.forName().newInstance() on the JDBC driver, so you don't have to call that yourself.

idleTimeout and checkoutTimeout are expressed in seconds

cacheStatements is set to 'true' by default

Parameters:
alias - Name of the pool
driver - Classname of JDBC driver to use
url - JDBC URL to connect to
username - JDBC username to connect as
password - username's password in the database
maxConn - Maximum number of connections to open; When this limit is reached, threads requesting a connection are queued until a connection becomes available
idleTimeout - Maximum number of seconds a Connection can go unused before it is closed
checkoutTimeout - Maximum number of seconds a Thread can checkout a Connection before it is closed and returned to the pool. This is a protection against the Thread dying and leaving the Connection checked out indefinately
maxCheckout - If this is greater than 0, the number of times a Connection may be checked out before it is closed. Used as a safeguard against cursor leak, which occurs if you don't call ResultSet.close() and Statement.close()
rowPrefetch - If your db driver supports a row prefetch size (Oracle does) use this value for row prefetch, -1 for the default

addAlias

public void addAlias(java.lang.String alias,
                     java.lang.String driver,
                     java.lang.String url,
                     java.lang.String username,
                     java.lang.String password,
                     int maxConn,
                     int idleTimeout,
                     int checkoutTimeout,
                     int maxCheckout,
                     int rowPrefetch,
                     boolean cacheStatements)
              throws java.lang.ClassNotFoundException,
                     java.lang.InstantiationException,
                     java.lang.IllegalAccessException
Adds an alias to the pool. This does call Class.forName().newInstance() on the JDBC driver, so you don't have to call that yourself.

idleTimeout and checkoutTimeout are expressed in seconds

Parameters:
alias - Name of the pool
driver - Classname of JDBC driver to use
url - JDBC URL to connect to
username - JDBC username to connect as
password - username's password in the database
maxConn - Maximum number of connections to open; When this limit is reached, threads requesting a connection are queued until a connection becomes available
idleTimeout - Maximum number of seconds a Connection can go unused before it is closed
checkoutTimeout - Maximum number of seconds a Thread can checkout a Connection before it is closed and returned to the pool. This is a protection against the Thread dying and leaving the Connection checked out indefinately
maxCheckout - If this is greater than 0, the number of times a Connection may be checked out before it is closed. Used as a safeguard against cursor leak, which occurs if you don't call ResultSet.close() and Statement.close()
rowPrefetch - If your db driver supports a row prefetch size (Oracle does) use this value for row prefetch, -1 for the default
cacheStatements - If set to true, the PooledConnection will reuse the same Statement object when conn.createStatement() is called. It will also cache PreparedStatements.

addAlias

public void addAlias(ConnectionPool pool)
Adds an alias to the pool using a supplied ConnectionPool object.

Beware! - this will not call Class.forName() on the JDBC driver for you. Make sure to call that before calling this method or the driver will not work (getConnection() will fail)


removeAlias

public void removeAlias(java.lang.String alias)
                 throws java.sql.SQLException
Closes all Connections in the pool with the supplied alias
Throws:
java.sql.SQLException - if no pool exists for that alias

getPools

public java.util.Enumeration getPools()
Returns an Enumeration of the ConnectionPool objects currently created

getPool

public ConnectionPool getPool(java.lang.String alias)
                       throws java.sql.SQLException
Returns the pool with the supplied alias
Throws:
java.sql.SQLException - if no pool exists for that alias

run

public void run()
Monitors each ConnectionPool and makes sure that no connection has gone idle or has been checked out for too long.
Specified by:
run in interface java.lang.Runnable

setTracing

public void setTracing(boolean on)
Turns tracing on or off. It is off by default. When you turn tracing on, the pool will print verbose messages about what it's doing to STDERR. If your application is hanging when it calls DriverManager.getConnection() this may help diagnose where things go wrong.

connect

public java.sql.Connection connect(java.lang.String url,
                                   java.util.Properties props)
                            throws java.sql.SQLException
Returns a connection from pool
Specified by:
connect in interface java.sql.Driver
Throws:
java.sql.SQLException - if no alias is given in the URL or if no pool exists with that alias name

acceptsURL

public boolean acceptsURL(java.lang.String url)
Returns true of url starts with URL_PREFIX
Specified by:
acceptsURL in interface java.sql.Driver

getMajorVersion

public int getMajorVersion()
Specified by:
getMajorVersion in interface java.sql.Driver

getMinorVersion

public int getMinorVersion()
Specified by:
getMinorVersion in interface java.sql.Driver

getPropertyInfo

public java.sql.DriverPropertyInfo[] getPropertyInfo(java.lang.String str,
                                                     java.util.Properties props)
Specified by:
getPropertyInfo in interface java.sql.Driver

jdbcCompliant

public boolean jdbcCompliant()
Always returns false since I haven't sent this code to Sun for approval
Specified by:
jdbcCompliant in interface java.sql.Driver

dumpInfo

public java.lang.String dumpInfo()
Dump some information about all connections and pools into a String