Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

what happens on connection.setAutoCommit = false

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

like image 851
eatSleepCode Avatar asked Sep 23 '15 12:09

eatSleepCode


People also ask

What does setAutoCommit false do?

What does setAutoCommit(false) do? Explanation: setAutoCommit(false) does not commit transaction automatically after each query. That saves a lot of time of the execution and hence improves performance.

What is the function of setAutoCommit () method?

In this scenario you can use the setAutoCommit() method. This method belongs to the Connection interface and, it accepts a boolean value. If you pass true to this method it turns on the auto-commit feature of the database and, if you pass false to this method it turns off the auto-commit feature of the database.

What happens when autocommit is set on?

When autocommit is set on, a commit occurs automatically after every statement, except PREPARE and DESCRIBE. If autocommit is on and a cursor is opened, the DBMS does not issue a commit until the CLOSE cursor statement is executed, because cursors are logically a single statement.

When you set a auto-commit false then?

Explicit Transaction Management When Auto-Commit Is False We need to disable auto-commit mode when we want to handle transactions ourselves and group multiple SQL statements into one transaction. We do this by passing false to the connection's setAutoCommit method: connection. setAutoCommit(false);


1 Answers

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.

like image 67
Mick Mnemonic Avatar answered Oct 02 '22 03:10

Mick Mnemonic