1

I am wondering about what the correct behavior should when you call cancel() on a PreparedStatement that has parameter values set (assuming that the JDBC driver you are using supports it). Should the set values be retained if execute has not been attempted?

I am asking about the correct behavior defined by JDBC, not the behavior of a particular database driver.

For example,

String query = "SELECT * FROM t where t.c1 = ?";
PreparedStatement ps = con.prepareStatement(query);

ps.setInt(1, 1);
ps.cancel();
ps.execute();

Should the parameter value 1 be retained?

What about this? Are both batched parameter values retained?

Reader charReader1 = new InputStream(new FileInputStream("SomeBigFile"));
ps.setCharacterInputStream(1, charReader1, -1);
ps.addBatch();

ps.cancel();

Reader charReader2 = new InputStream(new FileInputStream("AnotherBigFile"));
ps.setCharacterInputStream(1, charReader2, -1);
ps.addBatch();

ps.executeBatch();
minus
  • 320
  • 1
  • 5
  • 14

1 Answers1

1

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.

Emphasis on "one thread to cancel a statement that is being executed by another thread". Your examples only involve one thread, and the statement isn't being executed, so that's not how you use cancel().

More discussion here.

Community
  • 1
  • 1
Kayaman
  • 72,141
  • 5
  • 83
  • 121
  • It says that it *can* be used by another thread to abort execution, not *must*. I am not trying the use cancel() in the way that I have outlined above - I am giving a trivial example to outline a point. And since there are no rules about when cancel() can be called, it is a valid example. – minus Feb 06 '14 at 20:51
  • 1
    Ah, you've misunderstood the language of the documentation. Just because it says "it can", doesn't mean that there's an alternative. It's used to cancel a statement that is `being executed`, provided the driver supports it. Your examples don't show `cancel()` being called during execution time, so they don't really make sense. It's feasible that an implementation would throw an IllegalStateException if the statement isn't being executed at the time of the call, but you'd have to find an actual implementation to observe first. – Kayaman Feb 06 '14 at 20:57
  • As for the JDBC spec. Like so many other things, it doesn't define how actual implementations should behave in a case like this. – Kayaman Feb 06 '14 at 20:58