分享
 
 
 

从一个ConnectionPool的实现看design pattern的运用 (source code for Java 1.1)

王朝java/jsp·作者佚名  2006-01-08
窄屏简体版  字體: |||超大  

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 ConnectionFactory{

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(conn);

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 clearWarnings()throws SQLException{

getOpenConnection().clearWarnings();

}

public final void commit()throws SQLException{

getOpenConnection().commit();

}

/*

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 int 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 savepoint)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().setHoldability(holdability);

}*/

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 SQLException{

getOpenConnection().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(pooling);

}

}

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);

}

private ConnectionPoolingImpl(ConnectionFactory factory, int max){

this.factory = factory;

this.maxConn = 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 < maxConn)

{

conn = factory.createConnection();

}

if(conn != null)

{

client++;

return conn;

}

else

{

throw new ResourceNotAvailableException();

}

}

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(){

for(int i=0; i<freeConn.size();i++)

{

final Connection conn = (Connection)freeConn.elementAt(i);

try{

conn.close();

}

catch(SQLException sqlException){}

}

}

protected void finalize(){

closeAll();

}

}

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(){}

}

ResourceTimeOutException.java:

public class ResourceTimeOutException extends Exception{

public ResourceTimeOutException(String msg){super(msg);}

public ResourceTimeOutException(){}

}

 
 
 
免责声明:本文为网络用户发布,其观点仅代表作者个人观点,与本站无关,本站仅提供信息存储服务。文中陈述内容未经本站证实,其真实性、完整性、及时性本站不作任何保证或承诺,请读者仅作参考,并请自行核实相关内容。
2023年上半年GDP全球前十五强
 百态   2023-10-24
美众议院议长启动对拜登的弹劾调查
 百态   2023-09-13
上海、济南、武汉等多地出现不明坠落物
 探索   2023-09-06
印度或要将国名改为“巴拉特”
 百态   2023-09-06
男子为女友送行,买票不登机被捕
 百态   2023-08-20
手机地震预警功能怎么开?
 干货   2023-08-06
女子4年卖2套房花700多万做美容:不但没变美脸,面部还出现变形
 百态   2023-08-04
住户一楼被水淹 还冲来8头猪
 百态   2023-07-31
女子体内爬出大量瓜子状活虫
 百态   2023-07-25
地球连续35年收到神秘规律性信号,网友:不要回答!
 探索   2023-07-21
全球镓价格本周大涨27%
 探索   2023-07-09
钱都流向了那些不缺钱的人,苦都留给了能吃苦的人
 探索   2023-07-02
倩女手游刀客魅者强控制(强混乱强眩晕强睡眠)和对应控制抗性的关系
 百态   2020-08-20
美国5月9日最新疫情:美国确诊人数突破131万
 百态   2020-05-09
荷兰政府宣布将集体辞职
 干货   2020-04-30
倩女幽魂手游师徒任务情义春秋猜成语答案逍遥观:鹏程万里
 干货   2019-11-12
倩女幽魂手游师徒任务情义春秋猜成语答案神机营:射石饮羽
 干货   2019-11-12
倩女幽魂手游师徒任务情义春秋猜成语答案昆仑山:拔刀相助
 干货   2019-11-12
倩女幽魂手游师徒任务情义春秋猜成语答案天工阁:鬼斧神工
 干货   2019-11-12
倩女幽魂手游师徒任务情义春秋猜成语答案丝路古道:单枪匹马
 干货   2019-11-12
倩女幽魂手游师徒任务情义春秋猜成语答案镇郊荒野:与虎谋皮
 干货   2019-11-12
倩女幽魂手游师徒任务情义春秋猜成语答案镇郊荒野:李代桃僵
 干货   2019-11-12
倩女幽魂手游师徒任务情义春秋猜成语答案镇郊荒野:指鹿为马
 干货   2019-11-12
倩女幽魂手游师徒任务情义春秋猜成语答案金陵:小鸟依人
 干货   2019-11-12
倩女幽魂手游师徒任务情义春秋猜成语答案金陵:千金买邻
 干货   2019-11-12
 
推荐阅读
 
 
 
>>返回首頁<<
 
靜靜地坐在廢墟上,四周的荒凉一望無際,忽然覺得,淒涼也很美
© 2005- 王朝網路 版權所有