All Packages Class Hierarchy This Package Previous Next Index
com.bitmechanic.sql.ConnectionPoolManager
// 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.");
public static final String URL_PREFIX
public ConnectionPoolManager() throws SQLException
public ConnectionPoolManager(int monitorInterval) throws SQLException
public void addAlias(String alias,
String driver,
String url,
String username,
String password,
int maxConn,
int idleTimeout,
int checkoutTimeout) throws ClassNotFoundException, InstantiationException, IllegalAccessException
idleTimeout and checkoutTimeout are expressed in seconds
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
idleTimeout and checkoutTimeout are expressed in seconds
public synchronized void addAlias(ConnectionPool pool)
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)
public synchronized void removeAlias(String alias) throws SQLException
public Enumeration getPools()
public ConnectionPool getPool(String alias) throws SQLException
public void run()
public void setTracing(boolean on)
public Connection connect(String url,
Properties props) throws SQLException
public boolean acceptsURL(String url)
public int getMajorVersion()
public int getMinorVersion()
public DriverPropertyInfo[] getPropertyInfo(String str,
Properties props)
public boolean jdbcCompliant()
All Packages Class Hierarchy This Package Previous Next Index