Tuesday, January 25, 2022

JDBC Transaction

 JDBC Transaction

JDBC Transaction

  • Exchange data or information between two media is known as Transaction.

  •  By default, the AutoCommit mode is set to boolean true value because of which all the data are automatically saved into the Database Server whenever we perform any   Database operation.

  • We can explicitly disable AutoCommit mode by using setAutoCommit() and passing a boolean false argument.


Syntex:-

        +void setAutoCommit()

  eg:-

        con.setAutoCommit(false);

Note:-

  •  We have to disable the AutoCommit mode before we begin the transaction but after establishing a connection with Database Server.

  • Once the AutoCommit mode is disabled we have to explicitly save the data into the Database Server by using commit() at the end of the transaction.


Syntex:-

        +void commit()

  eg:-

        `con.commit();


  •  If anyone of the database operation fails, then the rollBack() operation is called which is used to rollBack Entire executed Database Operation and transaction starts from the beginning.   


Syntex:-

          +void rollBack()


   eg:-

           con.rollBack();


Definition for JDBC Transaction:


  • JDBC Transaction is considered to be a Single Business Unit which may have multiple SQL Statements which must be executed.


Need for JDBC Transaction:-

  •  JDBC Transaction is needed to maintain Data Consistency in the Database Server.


Advantage of JDBC:-

  •  It is used to achieve ACID Properties or rules where A refers to Atomicity, C refers to Consistency, I refers Isolation, D for Durability


Atomicity: Atomicity means Do Everything or Do Nothing.


  •  Do Everything refers to a Complete or Successful transaction where if all the Database operations are successfully executed, then the data are saved into the Database   Server leading to Data Consistency.

  •  Do Nothing refers to an Unsuccessful or Uncomplete transaction where if any of the Database operations fails, then the rollBack operation is called which is used to rollback the Entire executed Database operation, and the transaction starts from the beginning without saving any data into the Database Server due to data inconsistency.


SavePoint:-

Syntex:-

         java.sql.Savepoint sp=con.setSavePoint();


setSavePoint():-

  •  setSavePoint() is a factory or helper method which is used to create and return implementation object of Savepoint interface.

  • Hence the return type of setSavepoint() is the Savepoint interface.

0 comments:

If you have any doubts,please let me know