jdbc: does setAutoCommit(true) commits past executions? I would like to execute some statements, and commit the entire batch at the end. I've tried the following approach:



connection.setAutoCommit(false);            // Don't commit for each statement
int[] returnCodes = pstmt.executeBatch();   // Execute all statements
connection.setAutoCommit(true);             // Back to normal state - future statements
                                            // will be committed instantly
connection.commit();                        // Commit our batch


Which fails miserably:

java.sql.SQLException: Can't call commit when autocommit=true
--reference to the line with connection.commit()--


What's the right way to go? does connection.setAutoCommit(true) commits all executed batch statements?
From the Connection.setAutoCommit docs:


  NOTE: If this method is called during a transaction and the auto-commit mode is changed, the transaction is committed. If setAutoCommit is called and the auto-commit mode is not changed, the call is a no-op.


But I don't think it's very readable/obvious in your code. You should probably simply commit before switching back to autocommit mode. Makes the intent clear.