The following site shows using the new "AutoClosable" features with JDBC: link. This site is showing how the Statement will be automatically closed, but the result set is not in the try() section where it would be auto-closed. So, my question is, do I NOT need to close ResultSets directly in Java 7? I have always used the pattern: close resultset, close statement, close connection.
To close a Statement , ResultSet , or Connection object that is not declared in a try -with-resources statement, use its close method. If auto-commit is disabled, you must explicitly commit or roll back active transactions before you close the connection.
closing the Statement automatically closes any open ResultSet created from it; executing SQL on a Statement also closes any previously-open ResultSet on that statement.
You should explicitly close your Statement and PreparedStatement objects to be sure. ResultSet objects might also be an issue, but as they are guaranteed to be closed when the corresponding Statement/PreparedStatement object is closed, you can usually disregard it.
A ResultSet object is automatically closed when the Statement object that generated it is closed, re-executed, or used to retrieve the next result from a sequence of multiple results. The number, types and properties of a ResultSet object's columns are provided by the ResultSetMetaData object returned by the ResultSet.
From the Javadoc of ResultSet:
A ResultSet object is automatically closed when the Statement object that generated it is closed, re-executed, or used to retrieve the next result from a sequence of multiple results.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With