What does setAutoCommit(false) do | Eklavya Online

What does setAutoCommit(false) do

A JDBC connection is created in auto-commit mode by default. This means that each individual SQL statement is treated as a transaction and will be automatically committed as soon as it is executed. If you require two or more statements to be grouped into a transaction then you need to disable auto-commit mode using below command.

con.setAutoCommit(false);

Once auto-commit mode is disabled, no SQL statements will be committed until you explicitly call the commit method. A Simple transaction with use of autocommit flag is demonstrated below.

con.setAutoCommit(false);

PreparedStatement updateStmt =

con.prepareStatement( “UPDATE EMPLOYEE SET SALARY = ? WHERE EMP_NAME LIKE ?”);

updateStmt.setInt(1, 5000); updateSales.setString(2, “Jack”);

updateStmt.executeUpdate();

updateStmt.setInt(1, 6000); updateSales.setString(2, “Tom”);

updateStmt.executeUpdate();

con.commit();

con.setAutoCommit(true);