我做了一个简单的测试,在 1 秒内发出大量Connection
请求,每次执行 SELECT 以确保池瓶颈,然后调用interrupt()
.
我发现该connection
物体在被捕获后很好而且很漂亮InterruptedException
,即使堆栈跟踪显示我 c3p0 在awaitAvailable(..)
. 就在此时,我正在查看他们的消息来源,当然,他们处理InterruptedException
. 他们甚至发出适当的警告:
WARNING: com.mchange.v2.resourcepool.BasicResourcePool@5bcf4b61 -- an attempt to checkout a resource was interrupted, and the pool is still live: some other thread must have either interrupted the Thread attempting checkout!
告诉我们它仍然存在,尽管中间有很多词模糊。解决了。
无论如何,这是测试。
ComboPooledDataSource ds = new ComboPooledDataSource();
// testing with various pool sizes - same effect
ds.setMinPoolSize(1);
ds.setMaxPoolSize(5);
ds.setInitialPoolSize(2);
Thread connectingThread = new Thread() {
public void run() {
Connection cnxn = null;
while (true) {
try {
cnxn = ds.getConnection();
System.out.println("Got connection.);
executeQuery(cnxn);
} catch (SQLException e) {
System.out.println("Got exception.");
e.printStackTrace();
// SOLUTION:
Throwable cause = e.getCause();
if (cause instanceof InterruptedException) {
System.out.println("Caught InterruptedException! Cnxn is " + cnxn);
// note that cnxn is a com.mchange.v2.c3p0.impl.NewProxyConnection
// also note that it's perfectly healthy.
//
// You may either want to:
// a) use the cnxn to submit your the query
executeQuery(cnxn);
cnxn.close()
// b) handle a proper shutdown
cnxn.close();
}
break;
}
}
};
};
connectingThread.start();
try {
Thread.sleep(1000);
} catch (InterruptedException e) { e.printStackTrace(); }
connectingThread.interrupt();