To understand Transaction states, we need to know the following points:
- In the absence of failures, all transactions complete successfully.
- However, as we noted earlier, a transaction may not always complete its execution successfully. Such a transaction is termed aborted.
- If we are to ensure the atomicity property, an aborted transaction must have no effect on the state of the database.
- Thus, any changes that the aborted transaction made to the database must be undone.
- Once the changes caused by an aborted transaction have been undone, we say that the transaction has been rolled back.
- It is part of the responsibility of the recovery scheme to manage transaction aborts.
- A transaction that completes its execution successfully is said to be committed.
- A committed transaction that has performed updates, transforms the database into a new consistent state, which must persist even if there is a system failure.
- Once a transaction has committed, we cannot undo its effects by aborting it.
- The only way to undo the effects of a committed transaction is to execute a compensating
- For instance, if a transaction added $20 to an account, the compensating transaction would subtract $20 from the account.
- However, it is not always possible to create such a compensating transaction.
- Therefore, the responsibility of writing and executing a compensating transaction is left to the user, and is not handled by the database system.
- We need to be more precise about what we mean by successful completion of a transaction.
We therefore establish a simple abstract Transaction model. A Transaction must
be in one of the following states:
- Active: the initial state; the transaction stays in this state while it is executing.
- Partially committed: after the final statement has been executed.
- Failed: after the discovery that normal execution can no longer proceed.
- Aborted: after the transaction has been rolled back and the database has been.
restored to its state prior to the start of the transaction.
- Committed: after successful completion.
The state diagram corresponding to a transaction is shown above. Following points to be noted from the above mentioned transaction states:
- We say that a transaction has committed only if it has entered the committed state. Similarly, we say that a transaction has aborted only if it has entered the aborted state.
- A transaction is said to have terminated if it has either committed or aborted.
- A transaction starts in the active state. When it finishes its final statement, it enters
the partially committed state.
- At this point, the transaction has completed its execution, but it is still possible that it may have to be aborted, since the actual output may still be temporarily residing in main memory, and thus a hardware failure may preclude its successful completion.
- The database system then writes out enough information to disk that, even in the event of a failure, the updates performed by the transaction can be re-created when the system restarts after the failure. When the last of this information is written out, the transaction enters the committed state.
A transaction enters the failed state after the system determines that the transaction can no longer proceed with its normal execution (for example, because of hardware or logical errors).
Such a transaction must be rolled back. Then, it enters the aborted state. At this point, the system has two options:
- It can restart the transaction, but only if the transaction was aborted as a result of some hardware or software error that was not created through the internal logic of the transaction. A restarted transaction is considered to be a new transaction.
- It can kill the transaction. It usually does so because of some internal logical error that can be corrected only by rewriting the application program, or because the input was bad, or because the desired data were not found in the database.
Extra points – In case of External Writes:
We must be cautious when dealing with observable external writes, such as writes to a terminal or printer.
Once such a write has occurred, it cannot be erased, since it may have been seen external to the database system. Most systems allow such writes to take place only after the transaction has entered the committed state.
One way to implement such a scheme is for the database system to store any value associated with such external writes temporarily in nonvolatile storage, and to perform the actual writes only after the transaction enters the committed state. If the system should fail after the transaction has entered the committed state, but before it could complete the external writes, the database system will carry out the external writes (using the data in nonvolatile storage) when the system is restarted.
Handling external writes can be more complicated in some situations. For example suppose the external action is that of dispensing cash at an automated teller machine, and the system fails just before the cash is actually dispensed (we assume that cash can be dispensed atomically). It makes no sense to dispense cash when the system is restarted, since the user may have left the machine. In such a case a compensating transaction, such as depositing the cash back in the users account, needs to be executed when the system is restarted.
For certain applications, it may be desirable to allow active transactions to display data to users, particularly for long-duration transactions that run for minutes or hours. Unfortunately, we cannot allow such output of observable data unless we are willing to compromise transaction atomicity. Most current transaction systems ensure atomicity and, therefore, forbid this form of interaction with users.