[concurrency-interest] Cancellation convention

Joe Bowbeer joe.bowbeer at gmail.com
Wed Jun 7 04:11:17 EDT 2006

Mike Quilleash <mike.quilleash at azuresolutions.com> wrote:
> Specifically I'm interested in being able to cancel long running
> JDBC calls (using PreparedStatement.cancel()).  Would a sensible
> way be to fork off the PreparedStatement execution in another thread
> and have the main thread wait for the second thread to complete, but
> if it is interrupted then call the PreparedStatement.cancel() and throw
> an exception out to indicate the cancellation succeeded?  Just after
> some best practice advice.

Assuming you've scheduled the request as a FutureTask, I would call
task.cancel(false) to cancel it.  This will void the task -- without
sending an interrupt into JDBC land.

Then, assuming it's reliable(*), I would override the protected done()
method in your FutureTask so that it will cancel the prepared
statement if the task was cancelled.

  protected void done() {
    if (isCancelled()) {
      try {
      } catch (SQLException ex) {
        // oops!

(*) The spec says Statement.cancel() will:

"Cancels this Statement object if both the DBMS and driver support
aborting an SQL statement. This method can be used by one thread to
cancel a statement that is being executed by another thread."

Joe Bowbeer ~ joebowbeer.thruhere.net

More information about the Concurrency-interest mailing list