源代码如下:
接管Connection对象,屏蔽close()方法,当调用close()时,调用释放连接对象回连接池中的函数freeConnection()
public class ConnectionHandler implements InvocationHandler{
ConnectionPool pool;
Connection conn;
public ConnectionHandler(ConnectionPool pool){
this.pool=pool;
} public Connection bind(Connection conn){
this.conn=conn;
Connection proxyConn=(Connection)Proxy.newProxyInstance(conn.getClass().getClassLoader(),conn.getClass().getInterface(),this)
return proxyConn;
} public Object invoke(Object proxy,Mathod method,Object[] args) throws Throwable{
Object result=null;
if("close".equals(method.getName())){
this.pool.freeConnection(this.conn);
}
else{
result=mathod.invoke(this.conn,args);
}
}
public static void main(String[] args)
{
}
}
连接池类,管理数据库连接,用完不直接关闭连接,而是放回连接池中
public class ConnectionPool{
public synchronized freeConnection(){
/**
实现将对象放回连接池,不关闭连接
*/
}
public synchronized Connection getConnection(){
try{
Connection currentConnection=null;
/**
得到一个可用的数据库连接对象
*/
ConnectionHandler handler=new ConnectionHandler(this);//初始化代理类
return handler.bind(currentConnection);//绑定并接管返回的连接 }catch(SQLException e){return null;}
}
}
对于上面这两个片段中,我想实现的是屏蔽Connectoin的close方法,当在客户端代码中调用conn.close()方法时,调用的是freeConnection()方法。但是我有一点想不通,我每次getConnection时得到的连接对象都是实现了动态代理,即这些连接都被接管了,一旦这些connection调用close()方法时,都调用了freeConnection(),那我怎么才能真正的把它关闭呢?
能力有限,不知道我有没有把问题说清楚,请各位大侠不要见笑,帮帮忙啊!!!
接管Connection对象,屏蔽close()方法,当调用close()时,调用释放连接对象回连接池中的函数freeConnection()
public class ConnectionHandler implements InvocationHandler{
ConnectionPool pool;
Connection conn;
public ConnectionHandler(ConnectionPool pool){
this.pool=pool;
} public Connection bind(Connection conn){
this.conn=conn;
Connection proxyConn=(Connection)Proxy.newProxyInstance(conn.getClass().getClassLoader(),conn.getClass().getInterface(),this)
return proxyConn;
} public Object invoke(Object proxy,Mathod method,Object[] args) throws Throwable{
Object result=null;
if("close".equals(method.getName())){
this.pool.freeConnection(this.conn);
}
else{
result=mathod.invoke(this.conn,args);
}
}
public static void main(String[] args)
{
}
}
连接池类,管理数据库连接,用完不直接关闭连接,而是放回连接池中
public class ConnectionPool{
public synchronized freeConnection(){
/**
实现将对象放回连接池,不关闭连接
*/
}
public synchronized Connection getConnection(){
try{
Connection currentConnection=null;
/**
得到一个可用的数据库连接对象
*/
ConnectionHandler handler=new ConnectionHandler(this);//初始化代理类
return handler.bind(currentConnection);//绑定并接管返回的连接 }catch(SQLException e){return null;}
}
}
对于上面这两个片段中,我想实现的是屏蔽Connectoin的close方法,当在客户端代码中调用conn.close()方法时,调用的是freeConnection()方法。但是我有一点想不通,我每次getConnection时得到的连接对象都是实现了动态代理,即这些连接都被接管了,一旦这些connection调用close()方法时,都调用了freeConnection(),那我怎么才能真正的把它关闭呢?
能力有限,不知道我有没有把问题说清楚,请各位大侠不要见笑,帮帮忙啊!!!
解决方案 »
- 能不能删除这一局Test test=new Test();?这一局又什么意思?
- 正则表达式的应用
- 麻烦大家推荐一本jsp的书,谢谢
- 放入Set集合中的对象必须重写equals()方法。及重写hashCode()方法,会提高性能。这二点,能举一个例子吗,感谢
- 文件重命名,怎么弄呀!我都要疯了
- 如何修改弹出的message dialog的字体?
- 一个超简单的问题,高手不要进来取笑我........
- 请教一个简单的问题
- 还是applet的socket的问题,请多指教。
- 有谁知道Jbuilder的中文版教程可以下载站点,希望能让所有java爱好者分享,壮大我们java程序员的力量,振兴国内的java事业!
- 请帮我看一下这个程序好吗?
- 集合类的一个问题~!
自己写的会有各种问题,
扩展性也不会太好用开源的改造一下吧,性能、安全什么的都会比较好
http://jopener.com/category/database-connection-pools
再写个方法?那这方法是应该写在哪个类里呢?
我就是有一点不清楚,当我在getConnection中用
ConnectionHandler handler=new ConnectionHandler(this);//初始化代理类
return handler.bind(currentConnection);//绑定并接管返回的连接
使连接绑定后,是否每一个连接之后的close操作会调用InvocationHandler接口的invoke函数呢?
不好意思,我属于初学者,水平有限,不知道问的问题是不是弱弱了点