Does Java Connection.close rollback into a finally block?.
I know .Net SqlConnection.close does it.
With this I could make try/finally blocks without catch...
Example:
try {
conn.setAutoCommit(false);
ResultSet rs = executeQuery(conn, ...);
....
executeNonQuery(conn, ...);
....
conn.commit();
} finally {
conn.close();
}
If the connection has come from a pool, closing it actually sends it back to the pool for reuse. You typically have to do this in a finally{} block, such that if an exception is thrown, you still get the chance to close this.
By default, in all app server and oracle db, autocommit is true. No matter you use connection pooling or direct connection[DriverManager] since commit is the operation on connection object it doesn't matter.
rollback. Undoes all changes made in the current transaction and releases any database locks currently held by this Connection object. This method should be used only when auto-commit mode has been disabled.
If you don't close it, it leaks, and ties up server resources. @EJP The connection itself might be thread-safe (required by JDBC), but the applications use of the connection is probably not threadsafe. Think of things like different transaction isolation, boundaries (commit/rollback/autocommit) etc.
According to the javadoc, you should try to either commit or roll back before calling the close method. The results otherwise are implementation-defined.
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