ACID Properties and Transactions in Database
The database management system manages data that should remain integrated when changes are made in it. In case the integrity of the information is affected, the whole data will get corrupted. Hence, to maintain the integrity of the data, there are four properties in DBMS, known as the ACID properties. The ACID properties are defined for transactions that go through different tasks. That is why we see the need for the ACID properties.
But what do the ACID properties stand for? What is the use of each property?
Let us learn about this concept further in this article. We will understand each property of the ACID properties along with some examples.
What is a transaction?
A transaction is a single unit of a program that accesses and possibly updates one or more data items in the database. A group of tasks where the task is a minimum processing unit that cannot be divided further.
States of Transactions
Active: The transaction stays in this state while it is executing.
Partially committed: After the final statement of the transaction has been executed.
Failed: This is the state of transaction where it has been discovered that it can no longer be executed.
Aborted: After the transaction has been canceled and rolled back, the database has been restored to its state before the transaction.
Committed: After the successful completion of a transaction.
What are ACID properties?
To maintain consistency in a database, before and after the transaction, specific properties are followed. These are called ACID properties.
The term atomicity means that we need to keep our data atomic. It means that if we need to perform a transaction on the data, it should be executed entirely or should not be executed at all. It also implies that the operation should not be performed partially or in different segments. In the case of executing operations on the transaction, the operation should be completely executed and not partially.
Consider the following scenario:
Ninja has an account A with Rs.300 in his account. Ninja wishes to send Rs.100 to Pranay’s account, that is, account B. In account B, Pranay already has a balance of Rs.1000.
When Rs.100 is transferred to account B, the balance will be Rs.1100. To do this transaction, there will be two operations that will take place.
The first operation will be to debit Rs.100 from account A. And the second operation will be the credit of Rs.100 into account B.
What if the first operation executes successfully but the second operation fails?
A total of Rs.100 will be debited from account A without any credit in account B.
From the above diagram, we can observe that after the credit operation of Rs.100, the balance in account B is still Rs.1000. Hence, it is not an atomic transaction.
The image below shows that both the debit and credit operations are successfully executed. Thus, the transaction is atomic.
So, we can conclude that when the transaction loses atomicity in the bank system, it becomes a huge issue. Hence atomicity is the main focus in the bank systems.
The term consistency means that the value should always be maintained. The integrity of the data should always be maintained. If a transaction is executed in the database, its changes on the data should be preserved further in the database. In the case of transactions, Data integrity is essential to maintain the consistency of data. The data should always be correct.
Consider three accounts A, B, and C, where A makes a transaction T one by one to both the accounts B and C. Debit and credit, two operations take place.
The initial balance in accounts A, B, and C is Rs.3000, Rs.1000, and Rs.500.
Consider a transaction of Rs.500 from A to B. Debit of Rs.500 from A takes place and credit of Rs.500 to B. Similarly, a Debit of Rs. 200 takes place from account A and credit of Rs.200 to account C.
After these operations, The value read in the balance of account A is Rs.2300. (which is the correct value after the net debit of Rs.700). This shows that the database is consistent. If account B and account C, read the value of Rs. 3000, then the database would be inconsistent.
The term isolation refers to separation. Isolation is the characteristic of a database where no data in this database should affect the other one. The changes in the databases can occur concurrently.
The operation performed on one database should only begin when the changes in the first database are saved. It implies that if two different operations are being executed in two different databases, then they are independent of each other.
In the case of transactions, when two or more transactions are executed simultaneously, consistency should be maintained in the database. Other transactions will not see any changes in the other database until changes are not committed in the memory.
For example, Consider two operations are concurrently running on two different accounts, then according to the isolation principle, the value of both accounts should not get affected. The values should be persistent. As seen in the below diagram, account A is executing T1 and T2 transactions to account B and account C. Both the transactions T1 and T2, are independently executed without affecting each other. It is known as isolation.
The term durability refers to the permanency of something. In database management, durability ensures that the data becomes permanent in the database after the execution of the operation. The durability of the data should be so reliable that even if the system fails or system crashes, the data and the database remains unchanged. It is the responsibility of the recovery manager to ensure the durability of the database.
To commit the values to the database, we use the COMMIT command after making the changes to the database.
Therefore, the ACID property of DBMS plays a vital role in maintaining the consistency and availability of data in the database.
Frequently Asked Questions (FAQs)
- What is a transaction?
A transaction is a single unit of the program that accesses and possibly updates one or more data items in the database.
- What are the ACID properties?
Atomicity, Consistency, Isolation, and Durability are the ACID properties followed in databases.
- Why do we need ACID properties?
To maintain consistency in the database, before and after the execution of a transaction, we follow ACID properties.
For more information about database management, You can also expand your knowledge by referring to these articles on DBMS.