what happens if I do connection.setAutoCommit(false); does it creates a new transaction at database side?
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.
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.
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.
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);
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With