Will try-with-resources close all opened resources if exception happens?
private void insertUserInAccessTable(int user_id) throws SQLException {
final String sql = "bla bla";
try( Connection con = ...; PreparedStatement ps = ... ) {
...
if(i==0) throw new SQLException();
}
}
Yes, but not ones that were initialized outside of the try block or inside its body (after the resource declarations).
* When try-with-resources closes the
Statement
, JDBC says the statement should close theResultSet
it created. So it may get closed, but only because of the JDBC contract and not because of try-with-resources.