|
|||||||||
PREV CLASS NEXT CLASS | FRAMES NO FRAMES | ||||||||
SUMMARY: INNER | FIELD | CONSTR | METHOD | DETAIL: FIELD | CONSTR | METHOD |
java.lang.Object | +--com.bitmechanic.sql.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) 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.");
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 |
public static final java.lang.String URL_PREFIX
Constructor Detail |
public ConnectionPoolManager() throws java.sql.SQLException
public ConnectionPoolManager(int monitorInterval) throws java.sql.SQLException
monitorInterval
- number of seconds between checks on the poolMethod Detail |
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
idleTimeout and checkoutTimeout are expressed in seconds
maxCheckouts is unlimited
cacheStatements is set to 'true' by default
alias
- Name of the pooldriver
- Classname of JDBC driver to useurl
- JDBC URL to connect tousername
- JDBC username to connect aspassword
- username's password in the databasemaxConn
- Maximum number of connections to open; When this limit
is reached, threads requesting a connection are queued
until a connection becomes availableidleTimeout
- Maximum number of seconds a Connection can go
unused before it is closedcheckoutTimeout
- 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
indefinatelypublic 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
idleTimeout and checkoutTimeout are expressed in seconds
cacheStatements is set to 'true' by default
alias
- Name of the pooldriver
- Classname of JDBC driver to useurl
- JDBC URL to connect tousername
- JDBC username to connect aspassword
- username's password in the databasemaxConn
- Maximum number of connections to open; When this limit
is reached, threads requesting a connection are queued
until a connection becomes availableidleTimeout
- Maximum number of seconds a Connection can go
unused before it is closedcheckoutTimeout
- 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
indefinatelymaxCheckout
- 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()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
idleTimeout and checkoutTimeout are expressed in seconds
alias
- Name of the pooldriver
- Classname of JDBC driver to useurl
- JDBC URL to connect tousername
- JDBC username to connect aspassword
- username's password in the databasemaxConn
- Maximum number of connections to open; When this limit
is reached, threads requesting a connection are queued
until a connection becomes availableidleTimeout
- Maximum number of seconds a Connection can go
unused before it is closedcheckoutTimeout
- 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
indefinatelymaxCheckout
- 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.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
idleTimeout and checkoutTimeout are expressed in seconds
cacheStatements is set to 'true' by default
alias
- Name of the pooldriver
- Classname of JDBC driver to useurl
- JDBC URL to connect tousername
- JDBC username to connect aspassword
- username's password in the databasemaxConn
- Maximum number of connections to open; When this limit
is reached, threads requesting a connection are queued
until a connection becomes availableidleTimeout
- Maximum number of seconds a Connection can go
unused before it is closedcheckoutTimeout
- 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
indefinatelymaxCheckout
- 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 defaultpublic 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
idleTimeout and checkoutTimeout are expressed in seconds
alias
- Name of the pooldriver
- Classname of JDBC driver to useurl
- JDBC URL to connect tousername
- JDBC username to connect aspassword
- username's password in the databasemaxConn
- Maximum number of connections to open; When this limit
is reached, threads requesting a connection are queued
until a connection becomes availableidleTimeout
- Maximum number of seconds a Connection can go
unused before it is closedcheckoutTimeout
- 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
indefinatelymaxCheckout
- 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 defaultcacheStatements
- If set to true, the PooledConnection will reuse
the same Statement object when conn.createStatement() is called.
It will also cache PreparedStatements.public 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 void removeAlias(java.lang.String alias) throws java.sql.SQLException
public java.util.Enumeration getPools()
public ConnectionPool getPool(java.lang.String alias) throws java.sql.SQLException
public void run()
public void setTracing(boolean on)
public java.sql.Connection connect(java.lang.String url, java.util.Properties props) throws java.sql.SQLException
public boolean acceptsURL(java.lang.String url)
public int getMajorVersion()
public int getMinorVersion()
public java.sql.DriverPropertyInfo[] getPropertyInfo(java.lang.String str, java.util.Properties props)
public boolean jdbcCompliant()
public java.lang.String dumpInfo()
|
|||||||||
PREV CLASS NEXT CLASS | FRAMES NO FRAMES | ||||||||
SUMMARY: INNER | FIELD | CONSTR | METHOD | DETAIL: FIELD | CONSTR | METHOD |