Transactions in Java  JDBC | JDBC Tutorials

This JDBC Tutorials  “Transactions in Java ” explains clearly about what is transaction, how transactions execute in jdbc application and how multiple queries execute as a transaction.


 

Transactions:

  • A transaction is a set of one or more statements that are executed as a unit.
  • In a transaction either all of the statements are executed or none of the statements is executed.
  • In JDBC application, when ever Connection is created, it is in auto-commit mode.
  • This means each individual SQL statement is treated as a transaction and is automatically committed right after its execution.

void setAutoCommit(boolean status) :
‘status’ is ‘true’ by default.

 

  • for Example, in the sql prompt if we execute any DML command, we need to call commit statement explicitly.

sql> delete from emps;
sql>commit;

 

  • But in java application, if we execute DML command…

Statement obj = conn.createStatement();
int res = obj.executeUpdate(“delete from emps”);
Committed implicitly…

 

  • The way to allow two or more statements to be grouped as a transaction, first we need to set auto-commit mode to false.
                    con.setAutoCommit (false);
Share this