Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/spring-boot/5.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 连接池和线程中断()_Java_Connection Pooling_C3p0_Interrupt Handling_Interrupted Exception - Fatal编程技术网

Java 连接池和线程中断()

Java 连接池和线程中断(),java,connection-pooling,c3p0,interrupt-handling,interrupted-exception,Java,Connection Pooling,C3p0,Interrupt Handling,Interrupted Exception,我使用它来处理多线程环境中的数据库连接池。这个问题可能与其他池LIB有关,但这就是我的问题 最近,我需要直接或间接地使用c3p0在这样的线程上实现离子处理,并注意到如果interrupt()在c3p0Datasource.getConnection()试图从池中获取连接时被正确调用,它会抛出InterruptedException 显然,这是因为wait() 酷。问题是如何正确地处理这一问题——两种情况都是a)您希望在线程终止之前继续事务,以及b)您希望中止 我尝试了一个似乎效果不错的解决方案(

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

最近,我需要直接或间接地使用c3p0在这样的线程上实现离子处理,并注意到如果
interrupt()
c3p0Datasource.getConnection()
试图从池中获取
连接时被正确调用,它会抛出
InterruptedException

显然,这是因为
wait()

酷。问题是如何正确地处理这一问题——两种情况都是a)您希望在线程终止之前继续事务,以及b)您希望中止


我尝试了一个似乎效果不错的解决方案(发布为答案)——事实上,我认为这个主题已经结束了。请随意插手,否则,谢谢

我做了一个简单的测试,在1秒内启动了大量的
连接
请求,每次确保池瓶颈时执行一个SELECT,然后调用
中断()

我发现
连接
对象在
中断异常
被捕获后状态良好,即使堆栈跟踪显示我在
等待可用(…)
时c3p0崩溃。就在这一秒,我正在检查它们的源代码,当然,它们处理
中断异常。他们甚至发出了适当的警告:

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!
告诉我们它仍然是活的,尽管在很多单词fuzz之间。解决了

这是测试

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();
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();