3

我正在使用c3p0在多线程环境中处理数据库连接池。这个问题可能与其他池库有关,但这就是我所拥有的。

最近我需要直接或间接使用 c3p0 在此类线程上实现中断离子处理,并注意到如果在尝试从池中获取我 ainterrupt()时被正确调用,它会抛出一个.c3p0Datasource.getConnection()ConnectionInterruptedException

显然,发生这种情况是因为wait()

at java.lang.Object.wait(Native Method)
at com.mchange.v2.resourcepool.BasicResourcePool.awaitAvailable(BasicResourcePool.java:1414)

凉爽的。问题是你如何正确处理这个问题 - 两种情况都是:a)你想在你的线程终止之前继续事务,b)你想中止。

我尝试了一个似乎运行良好的解决方案(作为答案发布) - 实际上,我认为这个主题已经结束。否则请随意筹码,谢谢!

4

1 回答 1

3

我做了一个简单的测试,在 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();
于 2014-09-20T19:12:19.427 回答