What if a connection closes in the middle of a commit?
Short Answer

Add exception handling to ensure a rollback happens on error.

try {
    &lt;code to execute&gt;
} catch (SQLException ex) {
    connection.rollback();
} finally {
    connection.commit();
}


Explanation

if autocommit is set to false, and no connection.rollback() was issued before the program exits, the drivers call an automatic connection.commit() before it closes the connection.