Transactions in DBMS, its states and ACID Properties

A transaction is a set of operations used to perform a logical unit of work. It also refers to the change in the database. The transaction is executed as a single unit and should be atomic, consistent, isolated, and durable. This ACID property of transaction maintains the integrity of the data during the execution of the database.

This post is also the answer for

What do you mean by Transaction?

There are various states of a transaction

  1. Active state: when the transaction begins then it enters into the active state. In this state read and write operations on the transaction are performed.
  2. Partially committed: Transaction enters in this state when it is almost completed by all the operations and is just left with ‘commit’ the transaction.
  3. Committed state: transaction enters into this state when all of its operations are completed followed with ‘commit’. When the transaction enters into this state the changes made in a transaction in the active state are recorded in the actual database.
  4. Terminated state: transaction enters into this state when it completed its execution and is about to leave the system. In this stage, the transaction release all of its held resources.
  5. Failed state: Transaction enters into this stage when the transaction gets aborted while running in the active state or in the partially committed state. Abortion happens due to any possible error in transactions or operations during the execution of the transaction.

The transaction holds some basic property which is very important and must be followed by the transaction. This property is called the ACID property of transaction which contains 4 basic properties combined.

Transactions ACID Properties

Atomicity

It states that a transaction must be treated as a single atomic unit i.e. Either transaction executed successfully or completely failed. It should not stuck in between or execute partially. The states of the transaction should be defined before execution or after the execution or failure or abortion of the transaction.

For example: Suppose A has transferred $100 to B. In this case if money transferred successfully, amount should credit to Account B. But if failure occurs then amount should not credited to Account B, But Amount is already debited from A. So this situation is creating the data issue which show atomicity is not followed in this case.

So If Amount debited From Account A, It should credited to Account B.

Consistency

Transactions preserve the consistency of the database. It means we should not loose the integrity of data in any transaction. Suppose if some changes made on the database it should preserved the value before and after transaction.

For Example:

To understand consistency better, Lets take an example of ticket booking System. Suppose if one person is trying to book the 2 movie ticket, But due to payment failure ticket not booked. But here two seat is reserved for that person because before payment we have to choose the seat. Now after the payment failure system should release the two reserved seat and increase the count of available seat.

If this updating not happened correctly, we can say data is not consistent.

Isolation

Isolation means data on one transaction should not impact the data of other transaction. As we know that at the same time there are multiple transactions are executed. If the two transactions are working on the same data then first one transaction will finish the operation then second will start. And if the two transaction are working on different database then they should not impact others database. This property is basically Isolation means one transaction should not impact others and each transaction should be isolated.

Durability

Transactions make sure that the modifications made on data should be persisting in database, even in the event of device failure.  Suppose if any modification done on the database, records should save permanently. And in case of system failures, database should survive. Otherwise there is huge loss.

For example you can assume if banking database failed due to some issue. All records of the users will be lost which leads to trust issue and amount losses of the customer. Our durable system should protect from such scenario.