ConnectionHandler handler=new ConnectionHandler(this);
return handler.bind(con);
}
public class ConnectionHandler implements InvocationHandler {
private Connection conn;
private ConnectionPool pool;
public ConnectionHandler(ConnectionPool pool){
this.pool=pool;
}
/**
* 将动态代理绑定到指定Connection
* @param conn
* @return
*/
public Connection bind(Connection conn){
this.conn=conn;
Connection proxyConn=(Connection)Proxy.newProxyInstance(
conn.getClass().getClassLoader(), conn.getClass().getInterfaces(),this);
return proxyConn;
}
/* (非 Javadoc)
* @see java.lang.reflect.InvocationHandler#invoke(java.lang.Object, java.lang.reflect.Method, java.lang.Object[])
*/
public Object invoke(Object proxy, Method method, Object[] args) throws Throwable {
// TODO自动生成方法存根
Object obj=null;
if("close".equals(method.getName())){
this.pool.releaseConnection(this.conn);
}
文章来源于领测软件测试网 https://www.ltesting.net/