|
Posted on 2009-05-16 13:43 沙漠中的鱼 阅读(279) 评论(0) 编辑 收藏 所属分类: 系统架构 、 Java基础
ConnectionPool.java:
![](/Images/OutliningIndicators/ExpandedBlockStart.gif) public interface ConnectionPool {
Connection getConnection()
throws test.res.ResourceNotAvailableException, SQLException;
Connection getConnection(long timeout)
throws test.res.ResourceTimeOutException, SQLException;
void clear();
}
ConnectionHome.java:
![](/Images/OutliningIndicators/ExpandedBlockStart.gif) public interface ConnectionHome {
void releaseConnection(Connection conn);
}
ConnectionPooling.java:
![](/Images/OutliningIndicators/ExpandedBlockStart.gif) 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:
![](/Images/OutliningIndicators/ExpandedBlockStart.gif) public interface ConnectionFactory {
public Connection createConnection()throws SQLException;
}
PooledConnection.java: (for Connection in Java 1.1.8)
![](/Images/OutliningIndicators/ExpandedBlockStart.gif) public final class PooledConnection implements Connection {
![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif) private interface ConnectionState {
ConnectionState close();
boolean isClosed();
Connection getOpenConnection()
throws SQLException;
}
![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif) private static class ClosedConnection implements ConnectionState {
![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif) public final ConnectionState close() {return this;}
public final Connection getOpenConnection()
![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif) throws SQLException {
throw new SQLException("Connection closed");
}
![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif) public final boolean isClosed() {return true;}
![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif) private ClosedConnection() {}
private static final ConnectionState _instance = new ClosedConnection();
![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif) static ConnectionState instance(Connection conn, ConnectionHome home) {return _instance;}
}
![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif) private static class OpenConnection implements ConnectionState {
private final ConnectionHome home;
private final Connection conn;
![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif) public final ConnectionState close() {
home.releaseConnection(conn);
return ClosedConnection.instance(conn, home);
}
public final Connection getOpenConnection()
![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif) {return conn;}
![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif) public final boolean isClosed() {return false;}
![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif) OpenConnection(Connection conn, ConnectionHome home) {
this.conn = conn; this.home = home;
}
![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif) static ConnectionState instance(Connection conn, ConnectionHome home) {
return new OpenConnection(conn, home);
}
}
private ConnectionState state;
public static Connection decorate(Connection conn, ConnectionHome home)
![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif) throws SQLException {
return new PooledConnection(conn, home);
}
private PooledConnection(Connection conn, ConnectionHome home)
![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif) throws SQLException {
![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif) if(conn.isClosed()) {
state = ClosedConnection.instance(conn, home);
}
![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif) else {
state = OpenConnection.instance(conn, home);
}
}
![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif) public final boolean isClosed() {
return state.isClosed();
}
![](/Images/OutliningIndicators/InBlock.gif)
![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif) public final void close() {
state = state.close();
}
![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif) protected void finalize() {
close();
}
private final Connection getOpenConnection()
throws SQLException
![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif) {return state.getOpenConnection();}
![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif) /** *//*****then, delegate all the other methods****/
public final Statement createStatement()
![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif) throws SQLException {
return getOpenConnection().createStatement();
}
// .
![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif) public final void clearWarnings()throws SQLException {
getOpenConnection().clearWarnings();
}
![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif) public final void commit()throws SQLException {
getOpenConnection().commit();
}
![](/Images/OutliningIndicators/InBlock.gif)
![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif) /**//*
public final Statement createStatement(int resultSetType,
int resultSetConcurrency)
throws SQLException{
return getOpenConnection().createStatement(resultSetType, resultSetConcurrency);
}*/
![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif) /**//*
public final Statement createStatement(int resultSetType,
int resultSetConcurrency, int resultSetHoldability)
throws SQLException{
return getOpenConnection().createStatement(resultSetType,
resultSetConcurrency, resultSetHoldability);
}*/
![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif) public final boolean getAutoCommit()throws SQLException {
return getOpenConnection().getAutoCommit();
}
![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif) public final String getCatalog()throws SQLException {
return getOpenConnection().getCatalog();
}
![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif) /**//*
public final int getHoldability()throws SQLException{
return getOpenConnection().getHoldability();
}*/
![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif) public final DatabaseMetaData getMetaData()throws SQLException {
return getOpenConnection().getMetaData();
}
![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif) public final int getTransactionIsolation()throws SQLException {
return getOpenConnection().getTransactionIsolation();
}
![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif) /**//*
public final Map getTypeMap()throws SQLException{
return getOpenConnection().getTypeMap();
}*/
![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif) public final SQLWarning getWarnings()throws SQLException {
return getOpenConnection().getWarnings();
}
![](/Images/OutliningIndicators/InBlock.gif)
![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif) public final boolean isReadOnly()throws SQLException {
return getOpenConnection().isReadOnly();
}
![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif) public final String nativeSQL(String sql)throws SQLException {
return getOpenConnection().nativeSQL(sql);
}
![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif) public final CallableStatement prepareCall(String sql)throws SQLException {
return getOpenConnection().prepareCall(sql);
}
![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif) /**//*
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)
![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif) throws SQLException {
return getOpenConnection().prepareStatement(sql);
}
![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif) /**//*
public final PreparedStatement prepareStatement(String sql, int autoGeneratedKeys)
throws SQLException{
return getOpenConnection().prepareStatement(sql, autoGeneratedKeys);
}
![](/Images/OutliningIndicators/InBlock.gif)
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 savepoint)throws SQLException{
getOpenConnection().releaseSavepoint(savepoint);
}*/
![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif) public final void rollback()throws SQLException {
getOpenConnection().rollback();
}
![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif) /**//*
public final void rollback(Savepoint savepoint)
throws SQLException{
getOpenConnection().rollback(savepoint);
}*/
public final void setAutoCommit(boolean autoCommit)
![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif) throws SQLException {
getOpenConnection().setAutoCommit(autoCommit);
}
public final void setCatalog(String catalog)
![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif) throws SQLException {
getOpenConnection().setCatalog(catalog);
}
![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif) /**//*
public final void setHoldability(int holdability)
throws SQLException{
getOpenConnection().setHoldability(holdability);
}*/
public final void setReadOnly(boolean readOnly)
![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif) throws SQLException {
getOpenConnection().setReadOnly(readOnly);
}
![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif) /**//*
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)
![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif) throws SQLException {
getOpenConnection().setTransactionIsolation(level);
}
![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif) /**//*public final void setTypeMap(Map map)throws SQLException{
getOpenConnection().setTypeMap(map);
}*/
![](/Images/OutliningIndicators/InBlock.gif)
![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif) /** *//*************************************************************************************************/
![](/Images/OutliningIndicators/InBlock.gif)
}
ConnectionPooling2Pool.java:
![](/Images/OutliningIndicators/ExpandedBlockStart.gif) public class ConnectionPooling2Pool implements ConnectionPool {
public final Connection getConnection()
![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif) throws test.res.ResourceNotAvailableException, SQLException {
return PooledConnection.decorate(pooling.getConnection(), pooling);
}
public final Connection getConnection(long timeout)
![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif) throws test.res.ResourceTimeOutException, SQLException {
return PooledConnection.decorate(pooling.getConnection(timeout), pooling);
}
![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif) public final void clear() {
pooling.clear();
}
private final ConnectionPooling pooling;
![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif) private ConnectionPooling2Pool(ConnectionPooling pooling) {
this.pooling = pooling;
}
![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif) public static ConnectionPool decorate(ConnectionPooling pooling) {
return new ConnectionPooling2Pool(pooling);
}
}
ConnectionPoolingImpl.java: (a simple implementation of ConnectionMan)
public class ConnectionPoolingImpl implements ConnectionPooling
![](/Images/OutliningIndicators/ExpandedBlockStart.gif) ![](/Images/OutliningIndicators/ContractedBlock.gif) {
private int client=0;
private final Vector freeConn = new Vector();
private final int maxConn;
private final ConnectionFactory factory;
![](/Images/OutliningIndicators/InBlock.gif)
![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif) static public ConnectionPooling instance(ConnectionFactory factory, int max) {
return new ConnectionPoolingImpl(factory, max);
}
![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif) private ConnectionPoolingImpl(ConnectionFactory factory, int max) {
this.factory = factory;
this.maxConn = max;
}
![](/Images/OutliningIndicators/InBlock.gif)
public final synchronized void releaseConnection(Connection conn)
![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif) {
freeConn.addElement(conn);
client--;
notify();
}
![](/Images/OutliningIndicators/InBlock.gif)
public final synchronized Connection getConnection()
throws ResourceNotAvailableException, SQLException
![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif) {
Connection conn = null;
if(freeConn.size() > 0)
![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif) {
conn = (Connection)freeConn.lastElement();
freeConn.removeElementAt(freeConn.size()-1);
}
else if(client < maxConn)
![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif) {
conn = factory.createConnection();
}
if(conn != null)
![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif) {
client++;
return conn;
}
else
![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif) {
throw new ResourceNotAvailableException();
}
}
![](/Images/OutliningIndicators/InBlock.gif)
public final synchronized Connection getConnection(long timeout)
throws ResourceTimeOutException, SQLException
![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif) {
for(long startTime = new java.util.Date().getTime();;)
![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif) {
try
![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif) {
return getConnection();
}
catch(ResourceNotAvailableException e1)
![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif) {
try
![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif) {
wait(timeout);
}
catch(InterruptedException e2)
![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif) {}
if((new java.util.Date().getTime() - startTime) >= timeout)
![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif) {
throw new ResourceTimeOutException();
}
}
}
}
![](/Images/OutliningIndicators/InBlock.gif)
![](/Images/OutliningIndicators/InBlock.gif)
![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif) public final synchronized int getfreeconn() {
return freeConn.size();
}
![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif) public final int getmaxConn() {
return maxConn;
}
![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif) public final synchronized int getclient() {
return client;
}
![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif) public final synchronized void setclient() {
client=0;
}
![](/Images/OutliningIndicators/InBlock.gif)
![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif) public final synchronized void clear() {
closeAll();
freeConn.removeAllElements();
}
![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif) private final void closeAll() {
for(int i=0; i<freeConn.size();i++)
![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif) {
final Connection conn = (Connection)freeConn.elementAt(i);
![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif) try {
conn.close();
}
![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif) catch(SQLException sqlException) {}
}
}
![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif) protected void finalize() {
closeAll();
}
}
![](/Images/OutliningIndicators/None.gif)
ConnectionFactoryImpl.java:
public class ConnectionFactoryImpl
![](/Images/OutliningIndicators/ExpandedBlockStart.gif) ![](/Images/OutliningIndicators/ContractedBlock.gif) {
![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif) private ConnectionFactoryImpl() {}
static public ConnectionFactory instance(final String driver, final String url,
final String user, final String pwd)
![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif) throws SQLException, ClassNotFoundException {
final Class driverClass = Class.forName(driver);
![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif) return new ConnectionFactory() {
private final Class keeper = driverClass;
public final Connection createConnection()
![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif) throws SQLException {
return DriverManager.getConnection(url,user,pwd);
}
};
}
static public ConnectionFactory instance(final String driver, final String url)
![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif) throws SQLException, ClassNotFoundException {
final Class driverClass = Class.forName(driver);
![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif) return new ConnectionFactory() {
private final Class keeper = driverClass;
public final Connection createConnection()
![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif) throws SQLException {
return DriverManager.getConnection(url);
}
};
}
}
![](/Images/OutliningIndicators/None.gif)
TestConnectionPool.java:
![](/Images/OutliningIndicators/ExpandedBlockStart.gif) public class TestConnectionPool {
public static void test(String driver, String url, String user, String pwd)
![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif) 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)
);
}
}
![](/Images/OutliningIndicators/None.gif)
![](/Images/OutliningIndicators/None.gif)
ResourceNotAvailableException.java:
![](/Images/OutliningIndicators/ExpandedBlockStart.gif) public class ResourceNotAvailableException extends RuntimeException {
![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif) public ResourceNotAvailableException(String msg) {super(msg);}
![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif) public ResourceNotAvailableException() {}
}
![](/Images/OutliningIndicators/None.gif)
![](/Images/OutliningIndicators/None.gif)
ResourceTimeOutException.java:
![](/Images/OutliningIndicators/ExpandedBlockStart.gif) public class ResourceTimeOutException extends Exception {
![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif) public ResourceTimeOutException(String msg) {super(msg);}
![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif) public ResourceTimeOutException() {}
}
![](/Images/OutliningIndicators/None.gif)
|