|
|
从一个ConnectionPool的实现看Design Pattern的运用 (二)
作者:ajoo
好啦,现在让我们看看我们是怎样设计这个ConnectionPool的接口的。 public interface ConnectionPool { public Connection getConnection(); public void clear(); } 当然,这只是一个简化的接口。真正实现时也许我们还会有一些其他的方法。如同时支持同步getConnection()和异步getConnection()。同时,这个返回的Connection必须改写close()方法。原来的close()方法是关闭物理连接,但现在,我们要把这个Connection返还到ConnectionPool。因此,这个Connection对象必须知道它出身的ConnectionPool。这样,用户仍然调用Connection.close(), 也不用担心把Connection返还到错误的ConnectionPool。 再来看看我们的实现: public class ConnectionPoolImpl: implements ConnectionPool { class PooledConnection implements Connection { private final Connection conn; private boolean closed;
public PooledConnection(Connection conn) throws SQLException { this.conn = conn; closed = conn.isClosed(); }
public void close() { if(!closed) { //保证重复调用close()不会把一个connection重复返还。 closeConnection(conn); closed = true; } }
public boolean isClosed() { return closed; }
public Statement createStatement() throws SQLConnection { if(isClosed()) { throw new SQLException(“Connection closed”); }
return conn.createStatement(); }
public void commit() throws SQLConnection { if(isClosed()) { throw new SQLException(“Connection closed”); } conn.commit(); }
//其他所有的方法都这样做委托。 }
public synchronized Connection getConnection() { 如果pool里有Connection 从pool中去掉一个Connection conn; clients++; return new PooledConnection(conn); 否则,如果clients<maxClients 生成一个新的连接conn clients++; return new PooledConnection(conn); 否则,wait(),直到pool中有空闲Connection } //其他的实现都和我们第一章里的代码一样。 }
好了,这样,通过内部类PooledConnection这样一个wrapper, 我们就可以实现这个能处理ConnectionPool的Connection。 对了,忘了说了,这个PooledConnection其实就是Design Pattern里的Decorator模式。 现在让我们再欣赏一下我们的代码吧。ConnectonPoolImpl提供了一种基于简单策略的ConnectionPool的实现。PooledConnection封装了数据库连接,使之能和ConnectionPool协同工作。 完美吧? 不过,慢着!PooledConnection只是和ConnectionPoolImpl协同工作。张三要写ConnectionPool2还得自己重新实现一个Decorator。 怎么样能使不同的ConnectionPool的实现重用我们PooledConnection呢?而且,在所有大约二十个委托函数里,我们都有if(isClosed())……, 是不是很眼熟啊?一个Connection一旦被close()之后,只有在涅磐之后(通过ConnectionPool.getConnection()再次返回),才能使用。而所有对关闭了的Connection的调用,返回结果都是一样的.(不行,不行,不行!) 猜到点什么了吗?对啦,State Patten!
|
|