3

Unfortunately, the jdbc specification remains silent about the guarantees of Statement.cancel().

The only thing which is stated by the javadoc is that Statement.cancel() is thread-safe.
But:

  • Is it guaranteed that cancel() will always work?
    (in a manner that the thread which is currently executing the Statement will immediately unblock and continue)
  • Is it guaranteed that cancel() will not block and (more or less) return immediately?
Community
  • 1
  • 1
MRalwasser
  • 15,605
  • 15
  • 101
  • 147
  • At least for the oracle jdbc driver there are no such guarantees - especially in case of network or server-side issues: http://download.oracle.com/docs/cd/E11882_01/java.112/e16548/apxtblsh.htm#BACDAICJ – MRalwasser Jan 05 '11 at 08:18

2 Answers2

3

Is it guaranteed that cancel() will always work?

No. At least not in my experience. I think I even had to deal with drivers that simply threw an UnsupportedOperationException

Is it guaranteed that cancel() will not block and (more or less) return immediately

No. Depending on the statement being executed, sometimes the cancel() might wait until the first row of a query is returned. Again this is highly dependent on the driver.

  • Is it possible to set a timeout for the cancel process to kill the process rigorously? I thought about a `Statement.cancel(int timeout)` method, but I didn't find one ;) – bobbel Jan 31 '14 at 16:25
2

From the same javadoc:

if both the DBMS and driver support aborting an SQL statement

That limits the guarantee. The other limitation is that Statement.cancel() is implemented by the concrete driver, in other words, the implementation is not part of the Java RE.

So cancelling really should be thread safe, but you'll have to ask the vendor of your actual driver/DBMS.

Andreas Dolk
  • 113,398
  • 19
  • 180
  • 268