Transaction In DBMS

A transaction in DBMS is a collection of logically related tasks. It consists of a set of operations.

A transaction is an assembly of consecutive actions or a single operation that is done by a single operator to execute tasks to access the information of a database.

For example: Consider the employee of a bank who transfers an amount Rs 1000 from the bank account of X to that of Y. This task may seem small and easy, but it involves a series of low-level operations.

Explanation of transaction

Transactions in the bank account of X

Open_the_account (X)

Previous_Bal = X.bal

Current_Bal= Previous-Bal-1000

X.bal=Current_Bal

Close_the_account (X)

Transactions in the bank account of Y

Open_the_account (Y)

Previous_Bal = Y.bal

Current_Bal= Previous-Bal+1000

Y.bal=Current_Bal

Close_the_account (Y)

Tasks of the transaction in DBMS

Enlisted below are the primary tasks of the transaction

Read (X): To read the value of X from the tables of the database and store the same in the memory buffer

Write (X): To write the value from the memory buffer back to the tables of the database

Now let us consider the following scenario

R(X)

X=X-200

W(X)

Let the value of X before transaction be 1000

  • The initial task reads the value of X from the table of the databases and stores it in the buffer space
  • The second task reduces X’s amount by 200. The buffer now contains 800
  • The third task writes the value in the buffer to the table of the database. The final amount of X is 800

But owing to the failure of software, hardware, or power, the transaction might fail before completion.

For instance, If there is a failure in the debit transaction after the execution of the second task, then the value of X will remain in 1000, which will be unfavorable for the bank.

For the resolution of this issue, there are two significant transaction operations.

  • Rollback operation to undo the executed work
  • Commit for the permanent saving of the work.