Facebook Pixel

Transaction Management

A transaction is a single logical unit of work consisting of a series of database operations. In the real world, a single action like 'Transferring money' actually consists of multiple underlying database queries.

These operations begin with high-level code execution and are translated into low-level memory block manipulations, primarily abstracted as Read and Write operations.

Transaction Management Overview
  • Read(A): Fetches the database block containing item A from the physical disk, loads it into a volatile main memory buffer, and presents it to the application.
  • Write(A): Writes the updated value of item A from the volatile main memory buffer back to the physical database on the disk.

The Transaction Lifecycle (States)

To recover from system crashes and application errors, the database management system (DBMS) rigorously tracks each transaction through a state machine during its execution.

Transaction Lifecycle State Diagram
  • Active: The transaction is in its initial state while its instructions are actively executing. During this state, all modifications reside ONLY in volatile memory buffers, not on the physical disk.
  • Partially Committed: The transaction enters this state immediately after its last instruction has executed. The operations are computationally complete, but the changes STILL reside in volatile memory buffers and haven't safely reached the disk.
  • Committed: The transaction reaches this state after all its modifications are successfully written (flushed) to the physical disk. The changes are now permanent.
  • Failed: If a hardware failure, software error, or logic error prevents the transaction from completing its execution, it enters the failed state.
  • Aborted: Once failed, the system performs a 'Rollback' operation to undo any partial modifications, restoring the database to its pre-transaction state. After rollback, the transaction is marked as aborted.
  • Terminated: The final state. A transaction enters the terminated state after it has successfully Committed or has been safely Aborted. At this point, all locks and resources are released.
Transaction StateCurrent Memory LocationTransition Path & Actions
ActiveVolatile memory buffersTransitions to Partially Committed on success, or Failed on error.
Partially CommittedVolatile memory buffersTransitions to Committed after flushing buffers to disk.
CommittedPersistent non-volatile diskFinal state; successfully completed.
FailedVolatile memory buffersTransitions to Aborted after initiating a rollback.
AbortedCleaned (Changes Undone)Final state; rolled-back transaction.
TerminatedCleaned (Resources Released)Reached after entering either Committed or Aborted.

The ACID Properties

To maintain absolute database consistency through system failures, power outages, and concurrent access by thousands of users, the DBMS strictly enforces four pillars known as the ACID properties.

1. Atomicity ('All or Nothing')

Definition: Guarantees that a transaction executes completely or not at all. If any single operation within the transaction fails, the entire transaction is aborted and rolled back to the beginning.

Before T1
AccountBalance
A$500
B$300
T1 Crashes (Rolled Back)
AccountBalance
A$500 (Refunded!)
B$300

Example: T1 transfers $100 from A to B. It deducts $100 from A, but the server crashes before adding to B. The money is lost in the void! Atomicity ensures the system detects the failure upon reboot and rolls back the deduction, refunding A.

2. Consistency (Integrity Conservation)

Definition: Ensures that a transaction transitions the database from one valid, consistent state to another valid, consistent state, perfectly satisfying all schema constraints (like Primary Keys, Foreign Keys, and custom checks).

Before (Sum = $800)
AccountBalance
A$500
B$300
After (Sum = $800)
AccountBalance
A$400
B$400

Example: Before the $100 transfer, A has $500 and B has $300. The total sum is $800. After the transfer completes (A has $400, B has $400), the total sum must STILL be exactly $800. Consistency guarantees that internal business rules remain intact.

3. Isolation (Independence)

Definition: Ensures that concurrently executing transactions do not interfere with one another. The system behaves as if transactions are executed strictly serially (one by one), preventing concurrent operations from reading incomplete or 'dirty' data.

T1 (In Progress)
AccountBalance
A$400 (Deducted)
B$300 (Not added yet)
T2 (Trying to Read)
ActionStatus
Calculate Total SumLOCKED (Waiting...)

Example: While T1 is transferring $100, T2 tries to calculate the total funds. If T2 reads the data *during* the transfer, it will see $700 total instead of $800! Isolation locks the data, forcing T2 to wait until T1 is completely finished.

4. Durability (Permanence)

Definition: Guarantees that once a transaction reaches the 'Committed' state, its updates persist in non-volatile storage and can NEVER be lost, even if someone immediately unplugs the database server's power cord.

Example: Durability in Banking
You deposit cash at an ATM. The ATM prints a 'Transaction Successful' receipt. Two seconds later, the entire bank data center loses power. Because the ATM confirmed the transaction was Committed, Durability ensures that your deposit was flushed to hard disk drives. When the power comes back, your money will still be there.

Placement Takeaways: Who handles what?

In interviews, you will frequently be asked to identify which internal subsystem of the DBMS is responsible for maintaining each specific ACID property. Memorize this mapping:

ACID PropertyResponsible DBMS Component
AtomicityTransaction Manager
ConsistencyApplication Programmer / Integrity Constraints
IsolationConcurrency Control Manager
DurabilityRecovery Manager

Sort the Concepts

Drag the real-world scenario to the ACID property it represents:

Atomicity
Consistency
Isolation
Durability
Unsorted Items:
Rolling back after a crash mid-transfer
Total bank funds remaining exactly $800
Hiding incomplete edits from other users
Data survives a sudden power outage

Fill in the Blank

A transaction enters the state immediately after its last instruction has executed, but BEFORE the changes are permanently flushed to the physical disk.
Please Login.
Please Login.
Please Login.
Please Login.
Please Login.
Please Login.
Please Login.
Please Login.
Please Login.
Please Login.
Please Login.
Please Login.
Please Login.
Please Login.
Please Login.
Please Login.
Please Login.
Please Login.
Please Login.
Please Login.