Transaction rollback on SQLException using new try-with-resources block
According to the language spec, the connection will be closed before the catch clause is executed (http://docs.oracle.com/javase/specs/jls/se7/html/jls-14.html#jls-14.20.3.2). A possible solution is to nest try-with-resources statements: try (java.sql.Connection con = createConnection()) { con.setAutoCommit(false); try (Statement stm = con.createStatement()) { stm.execute(someQuery); // causes SQLException } catch(SQLException ex) { con.rollback(); con.setAutoCommit(true); throw ex; } con.commit(); con.setAutoCommit(true); } Hopefully … Read more