what happens on connection.setAutoCommit = false

eatSleepCode picture eatSleepCode · Sep 23, 2015 · Viewed 38.6k times · Source

what happens if I do connection.setAutoCommit(false); does it creates a new transaction at database side?

Answer

Mick Mnemonic picture Mick Mnemonic · Sep 23, 2015

According to the documentation, connection.setAutoCommit(false) will allow you to group multiple subsequent Statements under the same transaction. This transaction will be committed when connection.commit() is invoked, as opposed to after each execute() call on individual Statements (which happens if autocommit is enabled).

Changing the auto-commit mode through connection.setAutoCommit() will implicitly commit the active transaction and create a new one. From the Javadocs:

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.