本文介绍了如何调用java.sql.Connection :: abort?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想写一个SwingWorker,当用户取消任务时,它是:1)取消PreparedStatement,2)中止Connection.

I would like to write a SwingWorker that, when the user cancels the task, it: 1) cancels the PreparedStatement and 2) aborts the Connection.

下面是一些代码来说明:

Here is some code to illustrate:

class Task extends SwingWorker<Void, Void> {

    Connection conn;
    PreparedStatement p_stmt;
    @Override
    protected Void doInBackground() throws Exception {
        ResultSet results = p_stmt.executeQuery();
        while(results.next()) {
            if(isCancelled())
                return;
        }
        return null;
    }

    void cancell() {
        cancel(true);
        try {
            p_stmt.cancel();
            conn.abort(/* How do I properly implement Executor? */);
        } catch (SQLException e) {
            handleError(e);
        }
    }

    @Override
    protected void done() {
        try {
            get();
        } catch (ExecutionException | InterruptedException e) {
            handleError(e);
        }
    }
}

static void handleError(Exception e) {
    // ...
}

此行:

conn.abort(/* How do I properly implement Executor? */);

是我感兴趣的那一行.我应该传递给Connection::abort的什么?

Is the line I'm interested in. What should I pass to Connection::abort?

推荐答案

您需要在此处提供Executor的任何实现.例如,这可以是由ExecutorService ="nofollow noreferrer"> Executors ,例如Executors.newSingleThreadPool().您还可以使用 ForkJoinPool ,例如ForkJoinPool.commonPool()返回的公共池,但它也可以像使用lambda一样基本,该lambda为传递的每个可运行的对象创建一个新线程:

You need to provide any implementation of Executor here. This could for example be an ExecutorService created by one of the methods of Executors, for example Executors.newSingleThreadPool(). You could also use a ForkJoinPool, for example the common pool returned by ForkJoinPool.commonPool(), but it could also be as basic as using a lambda that creates a new thread for each runnable passed:

connection.abort(runnable -> new Thread(runnable).start())

您甚至可以将所有线程保留在当前线程中,直到清理完成为止:

You could even just keep everything blocked in the current thread until the cleanup is done:

connection.abort(Runnable::run)

每个都有各自的优缺点,不幸的是,由于JDBC规范尚不清楚如何使用Executor,因此很难提供良好的建议.例如,驱动程序应该将所有清理任务从abort返回之前添加到执行器 之前(Javadoc中的措辞暗示了这一点,但可以解释),或者可以自由添加任务在清理过程中异步进行?清理任务在执行时间等方面的表现应如何.

Each have their pros and cons, and unfortunately it is hard to provide a good advice because the JDBC specification is unclear on how the Executor is to be used. For example, should the driver add all cleanup tasks to the executor before returning from abort (the wording in the Javadoc implies this, but it is open to interpretation), or is it free to add tasks asynchronously during the cleanup? How well-behaved should the cleanup task(s) be in terms of execution time, etc.

如果创建专门用于使用Executors.newSingleThreadPool()调用中止的Executor,则最终需要将其关闭,否则会泄漏线程,但是如果中止清理任务以异步方式添加新任务,则没有明确定义您可以关闭执行程序服务的那一刻(或者您可能过早关闭了它).这可能表明您应在应用程序的生命周期内使用可能具有多个线程的固定线程池,而不是专门为调用中止而创建它.

If you create an Executor specifically for calling abort using Executors.newSingleThreadPool(), you will need to shut it down eventually otherwise you are leaking threads, but if the abort clean up tasks asynchronously add new tasks, there is no clearly defined moment that you can shutdown the executor service (or you may have shut it down too soon). This may be an indication that you should use a fixed thread pool with maybe more than one thread for the lifetime of your application, instead of creating it specifically for calling the abort.

另一方面,如果使用公共的fork/join池,则可能会影响应用程序的其他部分,如果添加了许多任务或这些任务长时间运行,则这些部分也会使用它.

On the other hand if you use the common fork/join pool, you may impact other parts of your application that also use it if a lot of tasks are added, or if the tasks are long running.

如果驱动程序创建了很多任务,我提供的第一个lambda会导致创建很多线程,而如果驱动程序期望它执行的话,我提供的第二个lambda可能会导致漫长的等待甚至是错误的行为.异步运行.

The first lambda I provided can result in the creation of a lot of threads if the driver creates a lot of tasks, and the second lambda I provided could result in a long wait or maybe even incorrect behaviour if the driver expects it to be run asynchronously.

换句话说,这有点令人头疼.

In other words, it is a bit of a headache.

我将对许多JDBC驱动程序进行一些检查,以根据实际的实现是否有更好的建议;可能要花点时间.

这篇关于如何调用java.sql.Connection :: abort?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-04 13:47