What is transaction?
A transaction is an action, or series of actions that are being performed by a single user or application program, which reads or updates the contents of the database. A transaction can be defined as a logical unit of work on the database.
Example: Suppose an employee of a bank transfers Rs 500 from M’s account to N’s account. This small transaction contains several low-level tasks:
M's Account Open_Account(M) Old_Balance = M.balance New_Balance = Old_Balance - 500 X.balance = New_Balance Close_Account(M)
N's Account Open_Account(N) Old_Balance = Y.balance New_Balance = Old_Balance + 500 Y.balance = New_Balance Close_Account(N)
Operations of Transaction
Following are the main operations of transaction:
Read(X): Read operation is used to read the value of X from the database and stores it in a buffer in main memory.
Write(X): Write operation is used to write the value back to the database from the buffer.
Let’s take an example to debit transaction from an account that consists of the following operations:
R(X); X = X - 500; W(X);
Let’s assume the value of X before starting of the transaction is 4000.
- The first operation reads X’s value from database and stores it in a buffer.
- The second operation will decrease the value of X by 500. So buffer will contain 3500.
- The third operation will write the buffer’s value to the database. So X’s final value will be 3500.
- But it may be possible that because of the failure of hardware, software or power, etc. that transaction may fail before finished all the operations in the set.
For example: If in the above transaction, the debit transaction fails after executing operation 2 then X’s value will remain 4000 in the database which is not acceptable by the bank. To solve this problem, we have two important operations:
Commit: It is used to save the work done permanently.
Rollback: It is used to undo the work done.
This article is contributed by Abhilash Singh. If you like Techarge and would like to contribute, you can also write an article using https://techarge.in/start-blogging-with-us/ or mail your article to [email protected] . See your article appearing on the Techarge home page and help other to learn. 🙂