0
0
DBMS Theoryknowledge~6 mins

Transaction states in DBMS Theory - Full Explanation

Choose your learning style9 modes available
Introduction
Imagine you are making a bank transfer. The system must carefully track each step to ensure the money moves correctly or not at all. This tracking happens through different transaction states that show the progress and safety of the operation.
Explanation
Active
This is the first state when a transaction starts. The system is processing commands like reading or writing data. The transaction is not finished and can still be changed or stopped.
Active means the transaction is currently running and not yet complete.
Partially Committed
After the last operation in the transaction is executed, but before the changes are saved permanently, the transaction enters this state. It shows that the transaction is almost done but not yet final.
Partially Committed means the transaction finished its tasks but is not yet permanent.
Committed
In this state, all changes made by the transaction are saved permanently in the database. The transaction is successfully completed and cannot be undone.
Committed means the transaction’s changes are permanently saved.
Failed
If an error or problem occurs during the transaction, it moves to this state. The transaction cannot continue and must be stopped.
Failed means the transaction encountered a problem and cannot finish.
Aborted
When a transaction fails or is canceled, it enters the aborted state. All changes made during the transaction are undone to keep the database consistent.
Aborted means the transaction was stopped and all its changes were reversed.
Real World Analogy

Think of ordering a meal at a restaurant. You place your order (active), the kitchen starts cooking (partially committed), the meal is served and you pay (committed). If the kitchen runs out of ingredients, the order fails, and you cancel it (failed and aborted).

Active → Placing your order and the kitchen starting to prepare it
Partially Committed → The meal is almost ready but not yet served
Committed → Meal served and payment completed
Failed → Kitchen runs out of ingredients and cannot finish the meal
Aborted → Order canceled and no meal served or charged
Diagram
Diagram
┌─────────┐     ┌───────────────────┐     ┌───────────┐
│ Active  │ → → │ Partially Committed│ → → │ Committed │
└─────────┘     └───────────────────┘     └───────────┘
      ↓                 ↑                      
      │                 │                      
      ↓                 │                      
┌─────────┐             │                      
│ Failed  │ ← ← ← ← ← ← ┘                      
└─────────┘                                    
      ↓                                         
┌─────────┐                                    
│ Aborted │                                    
└─────────┘                                    
This diagram shows the flow of transaction states from Active to Committed or to Failed and Aborted.
Key Facts
ActiveThe transaction is currently executing operations.
Partially CommittedThe transaction has finished operations but changes are not yet permanent.
CommittedThe transaction’s changes are permanently saved in the database.
FailedThe transaction encountered an error and cannot continue.
AbortedThe transaction was stopped and all changes were undone.
Code Example
DBMS Theory
import sqlite3

conn = sqlite3.connect(':memory:')
cur = conn.cursor()
cur.execute('CREATE TABLE accounts (id INTEGER PRIMARY KEY, balance INTEGER)')
cur.execute('INSERT INTO accounts (balance) VALUES (100)')

try:
    conn.execute('BEGIN')  # Active state
    cur.execute('UPDATE accounts SET balance = balance - 50 WHERE id = 1')
    # Simulate error
    raise Exception('Simulated failure')
    conn.commit()  # Committed state
except Exception as e:
    conn.rollback()  # Aborted state
    print('Transaction aborted:', e)

cur.execute('SELECT balance FROM accounts WHERE id = 1')
print('Balance after transaction:', cur.fetchone()[0])
OutputSuccess
Common Confusions
Thinking that a transaction is committed as soon as it finishes its last operation.
Thinking that a transaction is committed as soon as it finishes its last operation. A transaction is only committed after changes are permanently saved; finishing operations puts it in the partially committed state first.
Believing that failed transactions keep their changes in the database.
Believing that failed transactions keep their changes in the database. Failed transactions must be aborted, which means all their changes are undone to keep data consistent.
Summary
Transactions move through states: Active, Partially Committed, Committed, Failed, and Aborted to manage data changes safely.
Only when a transaction reaches the Committed state are its changes saved permanently.
If a transaction fails, it must be Aborted to undo any partial changes and keep the database consistent.