Sub topic 1
What a Transaction Is and How It Moves Through Its States
A transaction is a logical unit of database work. It may contain one statement or many statements. For example, transferring 100 units from account A to account B requires two updates:
- Subtract 100 from A.
- Add 100 to B.
Both updates must be treated as one unit. A transaction should either complete all required work or leave the database as if the work never started.
Prerequisites
This chapter assumes that you know basic relational database concepts, including tables, rows, and SQL statements. You should also understand that a database system may execute several transactions at the same time.
Transaction States
A transaction moves through states that describe its progress. The database management system (DBMS) records enough information to commit the work safely or undo it after a failure.
| State | Meaning |
|---|---|
| Active | The transaction is executing reads and writes. |
| Partially committed | The final statement has executed, but the commit is not yet durable. |
| Committed | The DBMS has made the transaction's effects permanent. |
| Failed | The transaction cannot continue because of an error or system problem. |
| Aborted | The DBMS has undone the transaction's effects. |
| Terminated | The transaction has left the system after commit or abort. |
A transaction starts in the active state. It remains active while it performs its operations. When its last statement finishes, it enters the partially committed state.
Partially committed does not mean permanently saved. The DBMS still needs to record the transaction's commit information, often in a log stored on durable storage. If that step succeeds, the transaction becomes committed. If a crash occurs first, recovery may have to undo the transaction.
After a successful commit, the transaction is terminated. Its changes must remain visible even if the system crashes later. This is the durability part of the ACID properties.
If an operation fails, the transaction enters the failed state. Causes include a constraint violation, deadlock victim selection, explicit rollback, or a system crash. The DBMS then aborts the transaction by undoing its changes. An aborted transaction may be restarted as a new execution, or it may be terminated permanently.
Why the States Matter in GATE Questions
The distinction between partially committed and committed is a common source of mistakes. Completing the final SQL statement is not enough. The commit must also become durable.
Consider a transaction that updates two rows. If it fails after updating the first row, the DBMS must undo that first update. Otherwise, the database could contain only part of the intended logical operation.
A commit makes the transaction's effects permanent, but it does not necessarily mean that every later transaction has already observed them. Visibility also depends on the concurrency-control and isolation mechanisms being used.
Exercise
A transaction finishes its final UPDATE, but the system crashes before the commit record reaches durable storage. Which state did it reach, and what should recovery do?
Show answer
It reached the partially committed state. Recovery must treat it as unsuccessful and undo its effects unless the durable log proves that the commit completed.