States of Transaction in DBMS
Transaction States
A transaction passes through many different states in its life cycle. These states are known as transaction states. A transaction can be in one of the following states in the database:
- Active state
- Partially committed state
- Committed state
- Failed state
- Terminated state
Active State
This is the first state in every transaction life cycle. A transaction is said to be in an active state when it starts execution and performs read and write operations.
Partially committed state
This state is also an execution phase. Any transaction enters into this state when the final operation of the transaction gets executed, but the data is still not saved to the database.
Committed state
Any transaction enters in committed state when all the operations of transaction are completed successfully. And all the changes made by the transaction are permanently saved into the database.
Failed state
Any transaction is entered in this state when the normal execution of it can no longer proceed. Or we can say that, if any transaction cannot proceed to the execution phase due to system failure, then the transaction is said to be in a failed state.
Aborted state
Any transaction is said to be in an aborted state when a transaction fails in its execution and goes into a failed state. And all the changes made by the transaction has to be rolled back to the previous consistent state.
Terminated State
Any transaction reaches terminated state when a specific transaction which is leaving the system can't be restarted. After the transaction is committed successfully or rollback due to system or database failure, finally it indicates the transaction is over.
Example:
Let’s suppose a transaction T through which you want to transfer 100Rs from account ‘X’ (source account) to account ‘Y’ (destination account). Assume that the account balance of X is 500 and Y is 1000. This transaction T can be represented as:
Transaction T |
BEGIN TRANSACTION |
Read(X); …..(1) |
X=X-50; …..(2) |
Write(X); …..(3) |
Read(Y); …..(4) |
Y=Y+50; …..(5) |
Write(Y); …..(6) |
Commit; …..(7) |
END TRANSACTION |
ACTIVE: When the above transaction T starts, i.e., enters BEGIN TRANSACTION, the transaction in an active state. From BEGIN TRANSACTION and COMMIT statement, the transaction is in the ACTIVE state only.
In an ACTIVE state, value of X= 500 and value of Y = 1000.
PARTIALLY COMMITTED: If T transaction reaches the statement COMMIT, it goes into partially committed state.
In PARTIALLY COMMITTED state, X= 400 and Y= 1100;
FAILED: This state happens if one of the following occurs:
If any system or database failure happens to the transaction in an ACTIVE state, then the transaction goes into an FAILED state.
- If the transaction failed before Write(X), then X = 500 and Y = 1000.
- If the transaction failed after Write(X), then X= 400 and Y = 1000.
- If the transaction failed before Commit and after WRITE(Y)statement, then X = 400 and Y = 1100.
- If any failure happen to the above transaction T in a partially committed state, then the transaction goes into the failed state, the value of X is 500, and Y is 1000.
ABORTED: In an aborted state, X= 500 and Y= 1000. [X and Y both are rolled back to Old consistent state]
COMMITTED: If above transaction T executes COMMIT statement successfully. In other words, if it successfully writes the new value of X and Y into the database or a log file, then the transaction is said to be in a committed state.
In COMMITTED state, X = 400 and Y = 1100. [New consistent state]