ConnectionPool.java:
public interface ConnectionPool {Connection getConnection () throws test.res.ResourceNotAvailableException, SQLException; Connection getConnection (long timeout) throws test.res.ResourceTimeOutException, SQLException; void clear ();}
ConnectionHome.java:
Public Interface ConnectionHome {Void ReleaseConnection (Connection Conn);
ConnectionPooling.java:
public interface ConnectionPooling extends ConnectionHome {Connection getConnection () throws test.res.ResourceNotAvailableException, SQLException; Connection getConnection (long timeout) throws test.res.ResourceTimeOutException, SQLException; void clear (); void releaseConnection (Connection conn);}
ConnectionFactory.java:
Public interface connectFactory {public connection createconnection () throws sqlexception;
PooledConnection.java: (for Connection in Java 1.1.8)
public final class PooledConnection implements Connection {private interface ConnectionState {ConnectionState close (); boolean isClosed (); Connection getOpenConnection () throws SQLException;} private static class ClosedConnection implements ConnectionState {public final ConnectionState close () {return this;} public final Connection getOpenConnection () throws SQLException {throw new SQLException ( "Connection closed");} public final boolean isClosed () {return true;} private ClosedConnection () {} private static final ConnectionState _instance = new ClosedConnection (); static ConnectionState instance (Connection conn, ConnectionHome home) {return _instance;}} private static class OpenConnection implements ConnectionState {private final ConnectionHome home; private final Connection conn; public final ConnectionState close () {home.releaseConnection (c onn); return ClosedConnection.instance (conn, home);} public final Connection getOpenConnection () {return conn;} public final boolean isClosed () {return false;} OpenConnection (Connection conn, ConnectionHome home) {this.conn = conn ; this.home = home;} static ConnectionState instance (Connection conn, ConnectionHome home) {return new OpenConnection (conn, home);}} private ConnectionState state; public static Connection decorate (Connection conn, ConnectionHome home) throws SQLException {return new PooledConnection (CONN, HOME);
} Private PooledConnection (Connection conn, ConnectionHome home) throws SQLException {if (conn.isClosed ()) {state = ClosedConnection.instance (conn, home);} else {state = OpenConnection.instance (conn, home);}} public final boolean isClosed () {return state.isClosed ();} public final void close () {state = state.close ();} protected void finalize () {close ();} private final Connection getOpenConnection () throws SQLException { return state.getOpenConnection ();} / ***** then, delegate all the other methods **** / public final Statement createStatement () throws SQLException {return getOpenConnection () createStatement ();.} // ... Public Final Void Clearnings () throwenConnection (). ClearWarnings ();} scient final void commit () throws sqlexception {getopenConnection (). Commist ();
/ * Public final Statement createStatement (int resultSetType, int resultSetConcurrency) throws SQLException {return getOpenConnection () createStatement (resultSetType, resultSetConcurrency);.} * / / * Public final Statement createStatement (int resultSetType, int resultSetConcurrency, int resultSetHoldability) throws SQLException { return getOpenConnection () createStatement (resultSetType, resultSetConcurrency, resultSetHoldability);.} * / public final boolean getAutoCommit () throws SQLException {return getOpenConnection () getAutoCommit ();.} public final String getCatalog () throws SQLException {return getOpenConnection (). getCatalog ();} / * public final int getHoldability () throws SQLException {return getOpenConnection () getHoldability ();.} * / public final DatabaseMetaData getMetaData () throws SQLException {return getOpenConnection () getMetaData ();.} public final i nt getTransactionIsolation () throws SQLException {return getOpenConnection () getTransactionIsolation ();.} / * public final Map getTypeMap () throws SQLException {return getOpenConnection () getTypeMap ();.} * / public final SQLWarning getWarnings () throws SQLException {return GetopenConnection (). getwarnings ();
public final boolean isReadOnly () throws SQLException {return getOpenConnection () isReadOnly ();.} public final String nativeSQL (String sql) throws SQLException {return getOpenConnection () nativeSQL (sql);.} public final CallableStatement prepareCall (String sql) throws SQLException {return getOpenConnection () prepareCall (sql);.} / * public final CallableStatement prepareCall (String sql, int resultSetType, int resultSetConcurrency) throws SQLException {return getOpenConnection () prepareCall (sql, resultSetType, resultSetConcurrency);.} public final CallableStatement prepareCall (String sql, int resultSetType, int resultSetConcurrency, int resultSetHoldability) throws SQLException {return getOpenConnection () prepareCall (sql, resultSetType, resultSetConcurrency, resultSetHoldability);.} * / public final PreparedStatement prepareStatement (String sql) throws SQLException {return getOpenConnection () prepareStatement (sql);.} / * public final PreparedStatement prepareStatement (String sql, int autoGeneratedKeys) throws SQLException {return getOpenConnection () prepareStatement (sql, autoGeneratedKeys);.}
public final PreparedStatement prepareStatement (String sql, int [] columnIndexes) throws SQLException {return getOpenConnection () prepareStatement (sql, columnIndexes);.} public final PreparedStatement prepareStatement (String sql, int resultSetType, int resultSetConcurrency) throws SQLException {return getOpenConnection () .prepareStatement (sql, resultSetType, resultSetConcurrency);} public final PreparedStatement prepareStatement (String sql, int resultSetType, int resultSetConcurrency, int resultSetHoldability) throws SQLException {return getOpenConnection () prepareStatement (sql, resultSetType, resultSetConcurrency, resultSetHoldability);.} public final PreparedStatement prepareStatement (String sql, String [] columnNames) throws SQLException {return getOpenConnection () prepareStatement (sql, columnNames);.} public final void releaseSavepoint (Savepoint savepo int) throws SQLException {getOpenConnection () releaseSavepoint (savepoint);.} * / public final void rollback () throws SQLException {getOpenConnection () rollback ();.} / * public final void rollback (Savepoint savepoint) throws SQLException {getOpenConnection ( ) .rollback (savepoint);} * / public final void setAutoCommit (boolean autoCommit) throws SQLException {getOpenConnection () setAutoCommit (autoCommit);.} public final void setCatalog (String catalog) throws SQLException {getOpenConnection () setCatalog (catalog). } / * Public final void setholdability (int holdability) throws sqlexception {getopenConnection (). StholdAbility;
} * / Public final void setReadOnly (boolean readOnly) throws SQLException {getOpenConnection () setReadOnly (readOnly);.} / * Public final Savepoint setSavepoint () throws SQLException {return getOpenConnection () setSavepoint ();.} Public final Savepoint setSavepoint ( String name) throws SQLException {return getOpenConnection () setSavepoint (name);.} * / public final void setTransactionIsolation (int level) throws SQLException {getOpenConnection () setTransactionIsolation (level);.} / * public final void setTypeMap (Map map) THROWS SQLEXCENECTION (). settypemap (map);} * // *********************************************** *********************************************************** ************* /
}
ConnectionPooling2Pool.java:
public class ConnectionPooling2Pool implements ConnectionPool {public final Connection getConnection () throws test.res.ResourceNotAvailableException, SQLException {return PooledConnection.decorate (pooling.getConnection (), pooling);} public final Connection getConnection (long timeout) throws test.res.ResourceTimeOutException , SQLException {return PooledConnection.decorate (pooling.getConnection (timeout), pooling);} public final void clear () {pooling.clear ();} private final ConnectionPooling pooling; private ConnectionPooling2Pool (ConnectionPooling pooling) {this.pooling = pooling } Public static connectionPool Decorate (ConnectionPooling Pooling) {Return New ConnectionPooling2Pool (Pool);}}
ConnectionPoolingImpl.java: (a Simple Implementation of ConnectionMAN)
public class ConnectionPoolingImpl implements ConnectionPooling {private int client = 0; private final Vector freeConn = new Vector (); private final int maxConn; private final ConnectionFactory factory; static public ConnectionPooling instance (ConnectionFactory factory, int max) {return new ConnectionPoolingImpl (factory, Max);
Public Final Synchronized void releaseconnection (connection conn) {freeconn.addelement (conn); client-; notify ();
public final synchronized Connection getConnection () throws ResourceNotAvailableException, SQLException {Connection conn = null; if (freeConn.size ()> 0) {conn = (Connection) freeConn.lastElement (); freeConn.removeElementAt (freeConn.size () - 1 }} Else if (client public final synchronized Connection getConnection (long timeout) throws ResourceTimeOutException, SQLException {for (. long startTime = new java.util.Date () getTime () ;;) {try {return getConnection ();} catch (ResourceNotAvailableException e1) {try {wait (timeout);} catch (InterruptedException e2) {} if ((new java.util.Date () getTime () - startTime)> = timeout.) {throw new ResourceTimeOutException ();}}}} public final synchronized int getfreeconn () {return freeConn.size ();} public final int getmaxConn () {return maxConn;} public final synchronized int getclient () {return client;} public final synchronized void setclient () {client = 0;} Public final synchronized void clear () {closeall (); freeconn.removeallelements ();} private final void closeall ()} private final void closeall ()} private final void closeall ()} private final void closeall ()} private final void closeall ()} private final void closeall ()} private final void closeall ()} private final void allements () Freeconn.elementat (i); try {conn.close ();} catch (sqlexception sqlexception) {}}} protected}}} protected}}} protected ConnectionFactoryImpl.java: public class ConnectionFactoryImpl {private ConnectionFactoryImpl () {} static public ConnectionFactory instance (final String driver, final String url, final String user, final String pwd) throws SQLException, ClassNotFoundException {final Class driverClass = Class.forName (driver); return new ConnectionFactory () {private final Class keeper = driverClass; public final Connection createConnection () throws SQLException {return DriverManager.getConnection (url, user, pwd);}};} static public ConnectionFactory instance (final String driver, final String url) throws SQLException , ClassNotFoundException {final Class driverClass = Class.forName (driver); return new ConnectionFactory () {private final Class keeper = driverClass; public final Connection createConnection () throws SQLException {return DriverManager.getConnection (url);}};}} TestConnectionPool .java: public class TestConnectionPool {public static void test (String driver, String url, String user, String pwd) throws java.sql.SQLException, test.res.ResourceNotAvailableException, test.res.ResourceTimeOutException, ClassNotFoundException {final ConnectionPool pool = ConnectionPooling2Pool.decorate ( ConnectionPoolingImpl.instance (ConnectionFactoryImpl.Instance (Driver, URL, User, PWD), 1000));}} ResourceNotavailableException.java: Public Class ResourceNotavailableException Extends RuntimeException {Public ResourceNotavailableException (String MSG) {Super (msg);} public resourceNotavailableException ()}} Resource TiMeOUTException.java: Public Class ResourceTimeOUtexception Excends Exception {Public ResourceTimeOUtexception (String MSG) {Super (MSG);} public resourceetimeoutException () {}}