Transactions are not a single operation on the database but is a collection of operations which are performed on the database. Upon execution, these transactions transform the database from one consistent state to the other. Here comes the role of ACID Properties.
When the database is being used by multiple simultaneous users, then the database may result in an inconsistent state as the transactions of these users may overlap each other on execution. This inconsistency in the database may lead to loss of data integrity.
Hence, there must be some way in which the inconsistency in the database can be checked.
ACID Properties – An Introduction
The properties which keep a check on the integrity of the database and help solve the problem of inconsistencies caused by simultaneous transactions of multiple users are known as ACID properties.
- A – Atomicity: This property keeps a check on the execution of the transaction. It works on the principle of ‘all or nothing’. It sees that either the transaction must be completed successfully or else the transaction must be roll backed.
- C – Consistency: This property keeps a check that a database must be in a consistent state before the start of the transaction and after the completion of the transaction.
- I – Isolation: This property is needed most when transactions are run concurrently. This property holds true when the result of transactions run concurrently is the same as if those transactions were run serially.
- D – Durability: This property states that the database must be able to recover all the committed transactions back in case of any system or media failure.
ACID Properties – Example
In order to understand these properties in a much better manner let us assume that you have to pay online fees of say Rs 10,000 to a certain ABC institute where you are studying.
Let us assume the initial balance in your account (Ac101) to be Rs 50,000 and that of the institute (AC501) be Rs 11,50,000. Now, take a look at the operations that would take place during this transaction.
|2||AC101 = AC101 – 10000|
|5||AC501 = AC 501 + 10000|
ACID Properties – Explained
A – Atomicity
A transaction is termed as atomic if it executes till completion or it has no effect at all on the database.Some of the key features of atomicity are as follows:
- All operations in the transaction should be completed or none of them should have an effect on the database.
- Atomicity must be maintained even when failures like disk failure, power failure, CPU failure or Software failure occur.
- Atomicity must be maintained in the presence of deadlocks.
It is the responsibility of DBMS itself to maintain the atomicity of the transactions. In order to do so, the DBMS maintains a record of all operations done in the transactions in the form of logs.
These logs are used to maintain the consistency of the database – by either writing the new values to the database on successful completion of the transaction or by restoring the old values of the database from the logs when a transaction fails to execute successfully.
Consider the example above: Say the system experiences some problem and the transaction halts after the execution of the 3rd operation. After the 3rd operation, the balances in both the accounts will be Rs 40,000 and Rs 11,50,000 respectively. That is, money gets deducted from your account but is not successfully transferred to the account of the institute.
This makes the data in the database inconsistent as the transaction did not complete its execution of all operations successfully. Such a state is termed as an inconsistent state. Hence, the database must ensure that such inconsistencies must not be reflected in the database.
Hence, as soon as the failure occurs, then the database rollback the operations (with the help of logs) already executed and does not reflect the intermediate changes made by the transaction on the database. Hence, this will help in maintaining databaseconsistency.
C – Consistency
This property works on the principle that integrity rules in the database must not be violated. Hence preserving the consistency of the database.
Now, if we look at the example, initial balance in AC101 was Rs 50,000 and that of AC501 was Rs 11,50,000. This makes a total of Rs 12,00,000 before the transaction started. Now, when the transaction completes execution, the balances would be Rs 40,000 and Rs 11,60,000 respectively. Adding to Rs 12,00,000 again. As both the sum total of accounts match before and after the execution of the transaction, we say that the consistency in the database remains.
It is the responsibility of the programmer to apply integrity rules in the database and help maintain the consistency.
I – Isolation
When transactions are run concurrently problems may arise due to overlapping of concurrently run transactions. This is where isolation property helps as no transactions are allowed to view the partial results of the actions of the transactions which are in intermediate states. This will also allow transactions to abort in isolation hence avoiding cascading aborts.
Isolation is enforced by concurrency control protocol which guarantees serializability.
D – Durability
This property states that once the database has committed a transaction, then the results must not be lost ever even if there is any failure in the system. Durability is handled by a recovery management component of DBMS. This is also done with the help of log files discussed earlier.