DB transaction is a combination of different operations. If not performed in a proper manner, different transactions working on the same data at the same time may leave the data in corrupted state, effecting the application. In this article, I am going to illustrate DB transaction ACID properties through an example of money transfer application between two different accounts A and B. To begin with, lets suppose that accounts A and B both have initial balance of $100.
ACID stands for Atomicity, Consistency, Isolation and Durability. Let's try to understand these one by one.
ACID stands for Atomicity, Consistency, Isolation and Durability. Let's try to understand these one by one.
- Atomicity: This is the property that mandates that if a transaction is started, either all the operations which are part of the transaction need to be completed by end of the transaction completion as a single unit of work or none of the operations needs to be completed. It is maintained by transaction management component. If a debit of $10 is made from account A, then the corresponding credit of $10 also needs to be made to account B.
- Consistency: This property ensures that the data effected by the transaction remains in consistent state before the transaction starts and after that ends. It implies that all the keys, checks, data types and triggers are successful and no constraint violation have happened. It is the responsibility of the application developer to ensure consistency. If a debit of $10 is made from account A to account B, at the end of the transaction the total amount of money in A and B is (100-10+100+10=)200 which is same as the total amount of money before the transaction (100+100=)200.
- Isolation: The whole idea behind this property is to ensure that two transaction operating on the same data are unaware of each other. No two transaction can update the same data at the same time. As a result the intermediate changes (uncommitted changes) done by one transaction aren't visible to other transaction. So, it appears to both of the transactions that they are operating in a sequential manner. If one transaction needs the data changes done by the other transaction, then it needs to wait until the other transaction completes. It is managed by the concurrency control manager. If one transaction makes of transfer of $10 from account A to account B, until the transaction is complete, another transaction running at the same time still sees each of the accounts A and B has balance of $100.
- Durability: This property ensures the data changes are permanent after successful completion of transaction. The data needs to be stored on a disk so that in case of a system failure, the changes aren't undone and still remain safe. It is the responsibility of the recovery manager to ensure the property is enforced. If a debit of $10 is made from account A to account B, this property ensures that at the end of successful transaction, changes in balance of these accounts are permanent (account A has balance of $90 and B has $110) and these changes don't get reversed in the case of failure.
Comments