咱们在平时项目中用到了数据库链接池,好比c3p0,dbcp,jndi... java
在使用结束的时候咱们也要关闭链接。为何呢。具体解释以下: sql
使用 c3p0 的话,也是 java.sql.Connection,只要是 JDBC 都是这个接口的对象! 数据库
public class ConnectionDecorator implements Connection { private Connection con = null; public ConnectionDecorator(Connection con) { this.con = con; } public void close() throws SQLException { // 重写! } public void commit() throws SQLException { this.con.commit(); } public Statement createStatement() throws SQLException { return this.con.createStatement(); } public Statement createStatement(int resultSetType, int resultSetConcurrency) throws SQLException { return this.con.createStatement(resultSetType, resultSetConcurrency); } ...... }而后通过链接池控制的 Connection 对象都使用该装饰器进行包装 二:动态代理: 使用动态代理从新实现 close 方法,每一个得到 Connection 是一个代理后的对象。 一个完善的链接池,其架构设计很是复杂,Connection#close 问题就是链接池诸多设计难点当中的一个。