闲话不多说,上代码:
public Connection getConnection() throws SQLException {
if(connPool.size() > 0){
//从集合中获取一个连接
final Connection conn = connPool.removeFirst();
//返回Connection的代理对象
log.debug("拿走了一个连接,池中还剩 " + connPool.size() + " 个连接");
return (Connection) Proxy.newProxyInstance(ConnPool.class.getClassLoader(), conn.getClass().getInterfaces(), new InvocationHandler() {
public Object invoke(Object proxy, Method method, Object[] args)
throws Throwable {
if(!"close".equals(method.getName())){
return method.invoke(conn, args);
}else{
connPool.add(conn);
log.debug("关闭连接,实际还给了连接池");
log.debug("池中连接数为 " + connPool.size());
return null;
}
}
});
}else{
throw new RuntimeException("数据库繁忙,稍后再试");
}
}
在使用动态代理增强Connection连接对象的close方法时,我碰到了如题所示的异常。通过搜索我发现这个异常出现的原因在于我使用的mysql数据库驱动的问题,由于数据库驱动不同,Connection.class.getInterfaces()返回的结果也不同,它返回的是一个Class[]数组,然而此数组的第一个元素必须是Connection才能把创建的代理类转为Connection对象,否则就会报错。
所以,这里我们可以采取一个替代方式替换Connection.class.getInterfaces(),即new Class[] { Connection.class },这样无论数据库驱动是什么版本的驱动,都能保证这个类型转换不出错。
即:
/**
* 获取数据库连接
*/
public Connection getConnection() throws SQLException {
if(connPool.size() > 0){
//从集合中获取一个连接
final Connection conn = connPool.removeFirst();
//返回Connection的代理对象
log.debug("拿走了一个连接,池中还剩 " + connPool.size() + " 个连接");
return (Connection) Proxy.newProxyInstance(ConnPool.class.getClassLoader(), new Class[] { Connection.class }, new InvocationHandler() {
public Object invoke(Object proxy, Method method, Object[] args)
throws Throwable {
if(!"close".equals(method.getName())){
return method.invoke(conn, args);
}else{
connPool.add(conn);
log.debug("关闭连接,实际还给了连接池");
log.debug("池中连接数为 " + connPool.size());
return null;
}
}
});
}else{
throw new RuntimeException("数据库繁忙,稍后再试");
}
}