DBMS Transaction Management
A transaction is a set of logically related operations. For example, you are transferring money from your bank account to your friend's account, the set of operations would be like this:
Simple Transaction Example
1. Read your account balance
2. Deduct the amount from your balance
3. Write the remaining balance to your account
4. Read your friend's account balance
5. Add the amount to his account balance
6. Write the new updated balance to his account
This whole set of operations can be called a transaction. Although I have shown you read, write and update operations in the above example but the transaction can have operations like read, write, insert, update, delete.
In DBMS, we write the above 6 steps transaction like this:
Lets say your account is A and your friend's account is B, you are transferring 10000 from A to B, the steps of the transaction are:
In the above transaction R refers to the Read operation and W refers to the write operation.
Transaction failure in between the operations
Now that we understand what is transaction, we should understand what are the problems associated with it.
The main problem that can happen during a transaction is that the transaction can fail before finishing the all the operations in the set. This can happen due to power failure, system crash etc. This is a serious problem that can leave database in an inconsistent state. Assume that transaction fail after third operation (see the example above) then the amount would be deducted from your account but your friend will not receive it.
To solve this problem, we have the following two operations
Commit: If all the operations in a transaction are completed successfully then commit those changes to the database permanently.
Rollback: If any of the operation fails then rollback all the changes done by previous operations.
Even though these operations can help us avoiding several issues that may arise during transaction but they are not sufficient when two transactions are running concurrently. To handle those problems we need to understand database ACID properties.