|
||||||||||
PREV CLASS NEXT CLASS | FRAMES NO FRAMES | |||||||||
SUMMARY: NESTED | FIELD | CONSTR | METHOD | DETAIL: FIELD | CONSTR | METHOD |
java.lang.Objectcom.bristle.javalib.sql.ConnectionPool
public class ConnectionPool
This class encapsulates pools of database connections.
Usage: - The typical scenario for using this class is: - Once, at startup: ConnectionPool pool = new ConnectionPool ("oracle.jdbc.driver.OracleDriver"); - At each point where a connection is needed. Connection conn = pool.getConnection(objConfig); ... Use the connection ... pool.returnConnection(conn); - If an error occurs while using the connection, you can advise the connection pool to close the connection and stop pooling it. Connection conn = pool.getConnection(objConfig); ... Try to use the connection, but decide that there is something wrong with the connection, so you need a different one ... pool.returnConnection(conn, true); Connection conn = pool.getConnection(objConfig); ... Use the connection ... pool.returnConnection(conn); - See the source code of the inner Tester class for more examples. Assumptions: Effects: - Creates and manages database connections. Anticipated Changes: - Add code to close and remove connections that have been held for too long. Client must have forgotten to return them to the pool. Should close them to conserve resources. Don't just return them to the pool. May not be a good idea to let other clients use them since we're not positive the current client is done. - Add code to limit the number of concurrent connections. - Could be rewritten to require the caller to create a separate connection pool for each set of credentials, by moving the credentials from getConnection() to the constructor. Advantages? Notes: Implementation Notes: Portability Issues: Revision History: $Log$
Nested Class Summary | |
---|---|
static interface |
ConnectionPool.DBConfig
This interface must be implemented by any class that expects to serve as a DBConfig object for use with ConnectionPool. |
static class |
ConnectionPool.SimpleDBConfig
Sample class implementing the DBConfig interface. |
static class |
ConnectionPool.Tester
Each class contains a Tester inner class with a main() for easier unit testing. |
Constructor Summary | |
---|---|
ConnectionPool(String strDBDriverClassName)
Constructor. |
|
ConnectionPool(String strDBDriverClassName,
int intMaxTimesToUse)
Constructor. |
Method Summary | |
---|---|
void |
clear()
Clear all connections from the pool, closing them. |
void |
clearAvailable()
Clear all available connections from the pool, closing them. |
void |
clearIdle()
Clear all available connections that have been idle too long from the pool, closing them. |
int |
getAvailableConnectionCount()
Get the count of available connections in the pool. |
Connection |
getConnection(ConnectionPool.DBConfig objConfig)
Get a connection from the pool. |
int |
getConnectionCount()
Get the count of connections in the pool. |
long |
getMaxIdleMillisecs()
Get the max number of milliseconds a database connection will sit idle in the pool before being closed. |
int |
getMaxTimesToUse()
Get the max number of times to use the connection before closing it and opening a new one. |
static ConnectionPool |
getSingleton()
Get the singleton ConnectionPool, if any. |
void |
returnConnection(Connection conn)
Return a connection to the pool for reuse by others. |
void |
returnConnection(Connection conn,
boolean blnClose)
Return a connection to the pool, optionally closing it so it won't be used anymore. |
void |
setMaxIdleMillisecs(long lngVal)
Set the max number of milliseconds a database connection will sit idle in the pool before being closed. |
void |
setMaxTimesToUse(int intVal)
Set the max number of times to use the connection before closing it and opening a new one. |
static void |
setSingleton(ConnectionPool pool)
Set the singleton ConnectionPool. |
Methods inherited from class java.lang.Object |
---|
equals, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait |
Constructor Detail |
---|
public ConnectionPool(String strDBDriverClassName) throws ClassNotFoundException, InstantiationException, IllegalAccessException
strDBDriverClassName
- Name of the database JDBC driver class.
Example: "oracle.jdbc.driver.OracleDriver"
ClassNotFoundException
- When driver class not installed.
InstantiationException
- When driver class can't be instantiated.
IllegalAccessException
- When driver class can't be accessed.public ConnectionPool(String strDBDriverClassName, int intMaxTimesToUse) throws ClassNotFoundException, InstantiationException, IllegalAccessException
strDBDriverClassName
- Name of the database JDBC driver class.
Example: "oracle.jdbc.driver.OracleDriver"intMaxTimesToUse
- Max times to reuse connection before closing
and opening a new one.
ClassNotFoundException
- When driver class not installed.
InstantiationException
- When driver class can't be instantiated.
IllegalAccessException
- When driver class can't be accessed.Method Detail |
---|
public void setMaxTimesToUse(int intVal)
intVal
- The new value.public int getMaxTimesToUse()
public void setMaxIdleMillisecs(long lngVal)
lngVal
- The new valuepublic long getMaxIdleMillisecs()
public Connection getConnection(ConnectionPool.DBConfig objConfig) throws SQLException
objConfig
- Configuration data needed to connect to the database.
SQLException
- When unable to connect to the database.public void returnConnection(Connection conn, boolean blnClose) throws SQLException
conn
- Connection to return.blnClose
- Boolean flag indicating whether to close the
connection.
SQLException
- When unable to rollback the returned connection.public void returnConnection(Connection conn) throws SQLException
conn
- Connection to return.
SQLException
- When unable to rollback the returned connection.public void clearAvailable()
public void clearIdle()
public void clear()
Note: Dangerous. Closes all database connections in the entire pool regardless of whether they are currently allocated by a client. Use clearAvailable() instead when appropriate.
public int getConnectionCount()
public int getAvailableConnectionCount()
public static void setSingleton(ConnectionPool pool)
pool
- The ConnectionPool instance to be stored as the singleton.public static ConnectionPool getSingleton()
|
||||||||||
PREV CLASS NEXT CLASS | FRAMES NO FRAMES | |||||||||
SUMMARY: NESTED | FIELD | CONSTR | METHOD | DETAIL: FIELD | CONSTR | METHOD |