minus
minus

Reputation: 320

JDBC: What is the correct behavior for PreparedStatement.cancel()

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

Upvotes: 1

Views: 428

Answers (1)

Kayaman
Kayaman

Reputation: 73568

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.

Upvotes: 1

Related Questions