In which cases is SQL Statement object automatically closed?

java, jdbc, sql

Solution

For JDBC 4.0 and earlier (Java 6 and earlier):

- A Statement is closed when the connection is closed

JDBC 4.1 (Java 7) adds the following:

- A statement is closed if `closeOnCompletion()` has been called and the `ResultSet` created from that statement has been read beyond the last row (and that read happened after enabling closeOnCompletion)

I don't consider 'try-with-resources' to be automatically closing the connection, but 'just' syntactic sugar for `try ... finally`.

Problem

My question regards this statement : Releases this Statement object's database and JDBC resources immediately instead of waiting for this to happen when it is automatically closed. http://docs.oracle.com/javase/6/docs/api/java/sql/Statement.html#close%28%29 So, in which cases is Statement object automatically closed?

Original source