All Packages  Class Hierarchy  This Package  Previous  Next  Index

Class com.bitmechanic.sql.ConnectionPoolManager

com.bitmechanic.sql.ConnectionPoolManager

public class ConnectionPoolManager
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)
   // Later in your code, use the JDBC DriverManager to obtain a
   // connection manually
   Connection conn = DriverManager(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.5 1998/12/03 09:11:57 pixel Exp $
Author:
James Cooper
See Also:
ConnectionPool

Variable Index

 o URL_PREFIX

Constructor Index

 o ConnectionPoolManager()
Creates a pool that doesn't monitor itself to check for idle/stale connections.
 o ConnectionPoolManager(int)
Creates a pool with a monitoring thread that checks the pool to make sure no connections are stale or idle.

Method Index

 o acceptsURL(String)
Returns true of url starts with URL_PREFIX
 o addAlias(ConnectionPool)
Adds an alias to the pool using a supplied ConnectionPool object.
 o addAlias(String, String, String, String, String, int, int, int)
Adds an alias to the pool.
 o addAlias(String, String, String, String, String, int, int, int, int)
Adds an alias to the pool.
 o connect(String, Properties)
Returns a connection from pool
 o getMajorVersion()
 o getMinorVersion()
 o getPool(String)
Returns the pool with the supplied alias
 o getPools()
Returns an Enumeration of the ConnectionPool objects currently created
 o getPropertyInfo(String, Properties)
 o jdbcCompliant()
Always returns false since I haven't sent this code to Sun for approval
 o removeAlias(String)
Closes all Connections in the pool with the supplied alias
 o run()
Monitors each ConnectionPool and makes sure that no connection has gone idle or has been checked out for too long.
 o setTracing(boolean)
Turns tracing on or off.

Variables

 o URL_PREFIX
 public static final String URL_PREFIX

Constructors

 o ConnectionPoolManager
 public ConnectionPoolManager() throws 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.

 o ConnectionPoolManager
 public ConnectionPoolManager(int monitorInterval) throws 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

Methods

 o addAlias
 public void addAlias(String alias,
                      String driver,
                      String url,
                      String username,
                      String password,
                      int maxConn,
                      int idleTimeout,
                      int checkoutTimeout) throws ClassNotFoundException, InstantiationException, 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
 o addAlias
 public void addAlias(String alias,
                      String driver,
                      String url,
                      String username,
                      String password,
                      int maxConn,
                      int idleTimeout,
                      int checkoutTimeout,
                      int maxCheckout) throws ClassNotFoundException, InstantiationException, 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()
 o addAlias
 public synchronized 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)

 o removeAlias
 public synchronized void removeAlias(String alias) throws SQLException
Closes all Connections in the pool with the supplied alias

 o getPools
 public Enumeration getPools()
Returns an Enumeration of the ConnectionPool objects currently created

 o getPool
 public ConnectionPool getPool(String alias) throws SQLException
Returns the pool with the supplied alias

 o run
 public void run()
Monitors each ConnectionPool and makes sure that no connection has gone idle or has been checked out for too long.

 o 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.

 o connect
 public Connection connect(String url,
                           Properties props) throws SQLException
Returns a connection from pool

Returns:
s JDBC Connection from pool, or null if url does not start with URL_PREFIX
 o acceptsURL
 public boolean acceptsURL(String url)
Returns true of url starts with URL_PREFIX

 o getMajorVersion
 public int getMajorVersion()
 o getMinorVersion
 public int getMinorVersion()
 o getPropertyInfo
 public DriverPropertyInfo[] getPropertyInfo(String str,
                                             Properties props)
 o jdbcCompliant
 public boolean jdbcCompliant()
Always returns false since I haven't sent this code to Sun for approval


All Packages  Class Hierarchy  This Package  Previous  Next  Index