← Previous: T09.2: The Consistency Property in Depth | 🏠 Index | Next: T09.4: Concurrent Execution and Schedules →
T09.3: Transaction States and Life-Cycle Diagram
Topic: T09: Transactions, ACID & Anomalies | Priority: 🟡 Medium Priority | Exam Frequency: 2/7 finals (6 marks: 2020 Q.5(b) [4m], 2023 Q.4(c) [2m])
Sources: Silberschatz 7e Ch. 17 (§17.1, §17.4, pp. 799, 806–807) · Lecture Deck 07 (Slides 8–9) · Raidah Class Notes (p. 26)
Why This Topic Matters
As a transaction runs, it moves through a set of well-defined states, much like a finite state machine. This lifecycle comes up regularly in finals, appearing both as a 4-mark descriptive question (2020 Q.5(b)) and as a quick 2-mark diagram question (2023 Q.4(c)). Examiners look for four specific details: naming all five states accurately, explaining why "Partially Committed" is separate from "Committed", stating the two actions the system can take after an abort (Restart vs. Kill), and noting that Committed and Aborted are the only terminal states.
📝 Key Definitions
Transaction: A transaction is a logical unit of program execution that reads and updates one or more database items. It starts with begin transaction and ends with end transaction (commit or abort). It acts as a single indivisible operation that moves the database from one consistent state to another.
Active State: The Active state is the initial state of a transaction when it begins execution. The transaction stays in this state while it runs its read, write, and calculation statements.
Partially Committed State: A transaction enters the Partially Committed state right after its final program statement finishes running. At this stage, its changes may still be sitting in main-memory buffers waiting to be flushed to disk.
Failed State: A transaction enters the Failed state as soon as the system detects that normal execution can no longer continue. This can happen due to a hardware crash, a logic error, a constraint violation, or a concurrency abort.
Aborted State: A transaction enters the Aborted state after the recovery system rolls back all of its partial changes and restores the database to its pre-transaction state. After aborting, the DBMS either restarts the transaction or kills it permanently.
Committed State: A transaction enters the Committed state after all of its updates and commit log records are safely written to non-volatile disk storage. Its changes are now permanent and cannot be rolled back.
Terminated Transaction: A transaction has terminated when it reaches either the Committed state or the Aborted state. Once terminated, its execution life cycle is complete.
Transaction Lifecycle State Machine
Intuitive Analogy: Why Execution Finishing Is Not the Same as Saving
Imagine typing an essay in a word processor. Finishing your last sentence in RAM is like reaching the Partially Committed state. Your work is done in memory, but if the power cord is pulled before you hit Save and the hard drive finishes writing the file, your changes are lost. Once the file is safely written to the disk, you reach the Committed state. Because database systems buffer updates in fast RAM before flushing them to disk, they need that extra state in the middle to track transactions whose code has finished running but whose disk writes are still in flight.
A database transaction moves through five distinct states. The DBMS tracks these states to manage locks and recovery logs accurately.
State Transition Diagram
Figure 9.3: Transaction State Transition Diagram (Source: Lecture Deck 07, Slide 8).
The Five Transaction States in Detail
1. Active State
- When it enters: Immediately when the transaction starts (
begin transaction).
- What happens here: The transaction stays in the Active state while it runs its
read, write, and calculation steps.
- Where it goes next:
- It moves to Partially Committed as soon as its last program statement finishes executing.
- It moves to Failed if something goes wrong mid-execution, such as a division by zero, a constraint violation, a hardware fault, or a deadlock abort.
2. Partially Committed State
- When it enters: Right after the final statement of the transaction finishes running.
- Why this state exists: Even though all calculations are done, the updated values may still be sitting in volatile main-memory (RAM) buffers. They have not necessarily been written out to permanent disk storage yet.
- What can go wrong: If a power outage or operating system crash hits before the DBMS flushes those memory buffers and log records to disk, the transaction cannot finish committing.
- Where it goes next:
- It moves to Committed once all log records and updates are safely written to non-volatile disk storage.
- It moves to Failed if a crash or disk error interrupts the flush before it finishes.
3. Committed State
- When it enters: Once all changes and the
commit log record are permanently recorded on stable storage.
- Key properties:
- Marks the successful completion of the transaction and guarantees Durability.
- The transaction is now terminated.
- No turning back: Once a transaction reaches the Committed state, the DBMS can never roll it back. If you later realize the transaction was a mistake, the only way to undo its effect is to run a separate compensating transaction (like issuing a refund transaction to reverse an earlier payment).
4. Failed State
- When it enters: The moment the DBMS realizes normal execution cannot continue.
- Common triggers:
- Hardware or system crashes, such as power loss or disk I/O errors.
- Software bugs inside the transaction, such as division by zero, invalid user input, or broken foreign key constraints.
- Concurrency control aborts, such as being chosen as a deadlock victim under Two-Phase Locking (2PL) or failing a timestamp check under Timestamp Ordering (TSO).
- Where it goes next: It always moves to Aborted so the recovery manager can clean up its partial changes.
5. Aborted State
- When it enters: After the recovery manager finishes rolling back every partial write made by the transaction, putting all modified data items back to their original values from before the transaction started.
- Key properties:
- Guarantees Atomicity (all-or-nothing execution) and marks the transaction as terminated.
- Two System Choices After an Abort:
Once the rollback is complete, the DBMS handles the aborted transaction in one of two ways depending on what caused the failure:
- Restart the Transaction: The DBMS automatically starts the transaction over as a brand-new transaction. It only does this when the failure came from an external, temporary glitch (like a deadlock abort or a brief hardware hiccup) that was not the transaction's own fault.
- Kill the Transaction: The DBMS cancels the transaction permanently and reports the error to the user or application. It must do this whenever the failure was caused by an internal logic bug (like division by zero, bad SQL syntax, or invalid input data), because rerunning the exact same code would just fail all over again.
State Transition Summary Table
| State Transition | What Triggers It | What It Means for the Database |
|---|
| Active → Partially Committed | Final statement of the transaction runs | Code execution is done, but changes are still in RAM buffers |
| Partially Committed → Committed | Log records and updates flush to disk | Durability is locked in and the transaction finishes |
| Active → Failed | Runtime error, constraint failure, or deadlock abort | Normal execution stops and rollback begins |
| Partially Committed → Failed | System crashes before disk flush completes | Commit fails and rollback begins |
| Failed → Aborted | Recovery manager finishes undoing partial writes | Database returns to its exact pre-transaction state |
| Aborted → Restart (New Active) | Failure was an external deadlock or temporary glitch | System resubmits the transaction automatically |
| Aborted → Kill (Stopped) | Failure was a logic bug or bad user input | Transaction is dropped permanently |
Observable External Writes
Transactions that trigger real-world physical actions outside the database need special care. For example, think of an ATM dispensing paper cash or a server sending a confirmation email. Once paper money comes out of the slot or an email leaves the server, the recovery manager cannot "roll it back" by restoring disk blocks.
Because of this, database systems delay observable external writes until after the transaction enters the Committed state. If a hardware failure happens right as cash is dispensed, the system has to log the event and run a compensating action later to reconcile the balance.
Exam Variants
The table below summarizes how transaction lifecycle questions have appeared in past exams:
| Year & Question | Marks | Format / Focus | Key Elements Expected in Solution |
|---|
| 2020 Q.5(b) | 4 | Definition + lifecycle walkthrough | Transaction definition (1m), explanation of all 5 states + Restart vs. Kill (2m), and state diagram (1m). |
| 2023 Q.4(c) | 2 | State diagram drawing | Clean, labeled 5-state diagram with transition arrows and a short legend. |
🏆 Golden Questions (Past Exam Archive)
🎯 GQ1 · 2020 Q.5(b) · Transaction Definition and Lifecycle Description
Appeared: 2020 Q.5(b) (4 marks)
Question:
What is a transaction? Describe the life cycle of a transaction.
Full Answer:
1. Definition of a Transaction
A transaction is a logical unit of program execution that accesses and possibly updates one or more data items in a database. It begins with begin transaction and ends with end transaction (commit or abort). To the user and the database system, a transaction acts as a single all-or-nothing unit of work that moves the database from one consistent state to another.
2. The Life Cycle of a Transaction
As a transaction executes, it moves through five states:
Figure 9.4: Transaction Lifecycle State Diagram (Source: Lecture Deck 07, Slide 8).
- Active:
- This is the starting state when a transaction begins.
- The transaction stays in the Active state while it executes its
read, write, and calculation instructions.
- Partially Committed:
- The transaction enters this state right after its final statement finishes executing.
- At this point, all calculations are complete, but the modified values may still be sitting in main-memory (RAM) buffers waiting to be flushed to permanent disk storage.
- Failed:
- The transaction enters this state whenever the system detects that normal execution can no longer continue, whether during the Active state or while flushing buffers in the Partially Committed state.
- Common causes include hardware crashes, division by zero, constraint violations, or deadlock aborts.
- Aborted:
- The transaction enters this state after the recovery manager rolls back all uncommitted changes and restores every data item to the value it had before the transaction started.
- Once a transaction is aborted, the DBMS takes one of two steps:
- Restart: It restarts the transaction as a new transaction if the failure came from an external hardware glitch or deadlock rather than the transaction's own logic.
- Kill: It permanently cancels the transaction if the failure was caused by an internal programming error or invalid input data.
- Committed:
- The transaction enters this state once all updates and the commit log record have been safely written to non-volatile disk storage.
- Its changes are now permanent and cannot be rolled back.
Committed and Aborted are the only two terminal states of a transaction.
🎯 GQ2 · 2023 Q.4(c) · Transaction State Diagram Drawing
Appeared: 2023 Q.4(c) (2 marks)
Question:
Draw state diagram of a transaction.
Full Answer:
State Transition Diagram of a Transaction
Figure 9.5: State Diagram of a Transaction.
Transition Legend
- Active → Partially Committed: The transaction finishes executing its final statement.
- Partially Committed → Committed: All in-memory updates and log records are safely written to permanent storage.
- Active → Failed: A runtime error, constraint violation, or concurrency abort stops execution.
- Partially Committed → Failed: A system crash or disk error interrupts the buffer flush before commit completes.
- Failed → Aborted: The recovery manager undoes all partial writes and restores the database to its initial state.
⚡ Exam Tips & Common Mistakes
- Never Skip the "Partially Committed" State: A classic exam mistake is drawing an arrow straight from Active to Committed. Always include Partially Committed in the middle to show the window where code has finished running in RAM but disk writes are not finished yet.
- Draw the Arrow from Partially Committed to Failed: Because a power failure can strike while the DBMS is flushing RAM buffers to disk, you must draw the arrow pointing from Partially Committed down to Failed.
- Never Draw Outgoing Arrows from Committed: Once a transaction commits, its changes are permanent. Do not draw an arrow from Committed to Failed or Aborted.
- Always Mention Restart vs. Kill: In descriptive questions like 2020 Q.5(b), explicitly write out the two post-abort choices: Restart (for deadlocks or temporary hardware faults) and Kill (for logic bugs or bad input).
🔗 Related Topics
← Previous: T09.2: The Consistency Property in Depth | 🏠 Index | Next: T09.4: Concurrent Execution and Schedules →