Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Does Java Connection.close rollback?

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();
}
like image 531
Antonio Avatar asked Oct 20 '08 13:10

Antonio


People also ask

What Does connection close do in Java?

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.

Does connection close commit?

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.

What does connection rollback do?

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.

What happens if you dont close JDBC connection?

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.


1 Answers

According to the javadoc, you should try to either commit or roll back before calling the close method. The results otherwise are implementation-defined.

like image 138
Joel Avatar answered Oct 25 '22 09:10

Joel