What is Transaction in DBMS

A transaction is a collection of logically related operations which reads and possibly updates the various data items in the database. Usually, a transaction is initiated by a user program written in high-level DML language (SQL), or programming language, with an embedded database, accesses in JDBC or ODBC.

In other words, we can say that any logical calculation which is done in a consistent mode in the database is called a transaction. Every database before and after the transaction must be in a consistent state.

A transaction has the following properties to ensure the integrity of data:

  1. Atomicity
  2. Consistency
  3. Isolation
  4. Durability

Simple Transaction Example

Suppose a bank employee transfers money from X’s account to Y’s account. As the amount of 100Rs gets transferred from the X account to Y’s account, the following series of operations are performed in the background of the screen.

Simply we can say, the transaction involves many following operations such as:

  1. Open the account of X
  2. Read the old balance
  3. Deduct the certain amount 100Rs from X account
  4. saving new balance to X account
  5. Open the account of Y
  6. Read the old balance
  7. Add the certain amount 100Rs to Y’s account
  8. Save new balance to Y account

X’s Account

Old_Balance = X.balance
New_Balance = Old_Balance - 100
X.balance = New_Balance

Y’s Account

Old_Balance = Y.balance
New_Balance = Old_Balance + 100
Y.balance = New_Balance

Operations of transaction

Following are the two main operations in a transaction:

  1. Read operation
  2. Write operation

Read Operation: This operation transfers the data item from the database and then stores it in a buffer in main memory.

Write Operation: This operation writes the updated data value back to the database from the buffer.

For example: Let Ti be a transaction that transfer Rs50 from X account to Y account. This transaction can be defined as:

T i: read(X); …….(1)
X := X?50; …….(2)
write(X); …….(3)
read(Y); …….(4)
Y := Y + 50; …….(5)

Assume that the value of both X and Y before starting of the transaction Ti is 100.

  • The first operation read the value of X(100) from the database and stores it in a buffer. The second operation will decrease X value by 50. So buffer will contain 50. The third operation will write the value of the buffer to the database. So the final value of X will be 50.
  • The fourth operation read the value of Y(100) from the database and stores it in a buffer.The fifth operation will add X value by 50. So buffer will contain 150. The sixth operation will write the value of buffer to the database. So the final value of Y will be 150.

But, it may be possible that due to hardware or software failure that transaction may fail before completing all the operations in the set.
For example: If in the above transaction, the transaction fails after executing operation 5, then Y's value will remain 100 in the database, which is not acceptable by the bank.

To overcome this problem, we have two important operations Commit and Rollback.
Commit Operation: This operation is used to save the work done permanently in the database.
Rollback Operation: This operation is used to undo the work done.