今天客户发来的日志中发现异常。该异常偶尔在程序启动的时候出现。
java.sql.SQLException: org.logicalcobwebs.proxool.ProxoolException: Attempt to register duplicate pool called 'pool'at org.logicalcobwebs.proxool.ProxoolDriver.connect(ProxoolDriver.java:109)at java.sql.DriverManager.getConnection(DriverManager.java:582)at java.sql.DriverManager.getConnection(DriverManager.java:185)
检查了下代码,似乎没有什么问题,一切都是按照proxool的教程中的example写的。于是把proxool的源代码下载,发现似乎真的有问题。
在类ProxoolDriver中,方法public Connection connect(String url, Properties info) 中,
if (!ConnectionPoolManager.getInstance().isPoolExists(alias)) {ProxoolFacade.registerConnectionPool(url, info, false); cp = ConnectionPoolManager.getInstance().getConnectionPool(alias);
}
这部分代码逻辑很简单,首先判断连接池的别名是否存在(isPoolExists),如果不存在,创建;已经存在,则直接获得连接。
在第2行的registerConnectionPool中,再次判断,该别名是否存在,如果已经存在,则抛出异常:
if (!ConnectionPoolManager.getInstance().isPoolExists(alias)) {ConnectionPoolDefinition cpd = new ConnectionPoolDefinition(url, info, explicitRegister);registerConnectionPool(cpd);
} else {throw new ProxoolException("Attempt to register duplicate pool called '" + alias + "'");
}
那么,自然而然地考虑,这部分代码在并发的时候会出现问题。比如,下面两个thread, 分别调用getConnection
Thread1 Thread2
运行至(1), isPoolExists ? False 运行至(1), isPoolExists ? False
运行至(2), 进入registerConnectionPool 运行至 (2) registerConnectionPool是static synchronized, 被阻塞住
创建完毕,返回 此时进入registerConnectionPool
If 处判断为已经存在(因为Thread1已经创建了),进入04行的else块,在05行抛出异常
然后写一小段程序来测试下:
import java.sql.*;public class TestForBug {public static void main(String[] arug) {new Thread(new T()).start();new Thread(new T()).start();}
}class T implements Runnable {public void run() {Connection connection = null;try {Class.forName("org.logicalcobwebs.proxool.ProxoolDriver");try {String url = "proxool.example:com.mysql.jdbc.Driver:jdbc:mysql://192.168.1.100:3306/mr";String username = "root";String password = "password";connection = DriverManager.getConnection(url, username, password);} catch (SQLException e) {System.err.println("Problem getting connection" + e.toString());}if (connection != null) {System.out.println("Got connection :)");} else {System.err.println("Didn't get connection, which probably means that no Driver accepted the URL");}} catch (ClassNotFoundException e) {System.err.println("Couldn't find driver " + e.getMessage());} finally {try {if (connection != null) {connection.close();}} catch (SQLException e) {System.err.println("Problem closing connection " + e);}}}
}
这段程序十有八九会抛出那个异常。
这就怪了。Proxool用的也很多,为啥会犯这么低级的错误呢。后来在跟踪的时候,发现:
如果运行在JDK5下,就没问题;
如果运行在JDK6下,则有问题。
查看下运行的堆栈,为:
测试代码
java.sql.DriverManager.
org.logicalcobwebs.proxool.Driver
既然测试代码和proxool.Driver都没改变,那么就看看java.sql.DriverManager是怎么回事。
结果真相大白了。
JDK5中:
public static synchronized Connection getConnection(String url, String user, String password) throws SQLException { //省略
}
JDK6中:
public static Connection getConnection(String url, String user, String password) throws SQLException { //省略
}
也就是,上面说的并发的问题,在JDK5中是同步的。因为DriverManager里面的方法就是同步的。但是在6中就不是了。这就是为啥上面的isPoolExists的代码失效的原因。也就是说,在JDK6中,proxool可能会出现这个问题。
那么解决也很简单,修改ProxoolDriver,把connect方法加上synchronized即可:
public synchronized Connection connect(String url, Properties info) {//省略
}