Module 10 · 16 min read · Free chapter
Transactions and concurrency control
A transaction must be atomic, consistent, isolated and durable. A schedule is conflict serialisable when its precedence graph has no cycle. Two-phase locking guarantees this but allows deadlock, and strict 2PL also stops cascading rollback. Timestamp ordering is deadlock-free, and wait-die or wound-wait prevent deadlock by always rolling back the younger transaction.
Start from zero: two clerks and one berth
At a railway reservation office, two clerks sit at two windows and share one computer system. Only one berth is left on a train. Both clerks check at the same moment, both see "1 berth left", both book it, and both print a ticket; two passengers now hold the same berth.
Each clerk did the right thing alone, but together they caused a wrong result. The second booking overwrote the first one's change, which is called a lost update. A database must stop such clashes while still letting hundreds of clerks work at once.
Now take a bank transfer of ₹500 from account A to account B. It has two steps, subtract ₹500 from A and add ₹500 to B. If the power fails between them, ₹500 vanishes, so both steps must happen together or not at all.
Such a group of steps that must act as one unit is a transaction. This chapter explains the rules a transaction must follow, how the database decides whether a mix of transactions is safe, and how it recovers after a crash.
Why it matters. Banks, railways and exam result systems run thousands of transactions every second. GATE tests this topic with schedule questions that need careful step-by-step reasoning, and they carry good marks.
ACID properties and transaction states
Every transaction must have four properties, remembered as ACID:
- Atomicity means all actions of a transaction happen or none do, so the ₹500 is never half moved.
- Consistency means a transaction that starts with a correct database leaves a correct database, such as keeping the total money in A and B unchanged.
- Isolation means concurrent transactions behave as if they ran one after another, so the two clerks never see each other's half-done work.
- Durability means that once a transaction commits, its effect survives any later crash.
Different parts of the DBMS provide them. The recovery system gives atomicity and durability, the concurrency control system gives isolation, and consistency is shared between the programmer and the integrity constraints.
A transaction moves through a few states. It is active while running, becomes partially committed after its last statement, and becomes committed once its changes are safely recorded. If something goes wrong it becomes failed, and after its changes are rolled back it is aborted, after which it may be restarted.
Why it matters. The ACID words are asked directly in RRB JE and IT officer papers. They also explain why later sections need both concurrency control and recovery.
Schedules and conflict serialisability
A schedule is the order in which the operations of several transactions actually run. We write for "transaction reads A", for " writes A", and for " commits". A serial schedule runs one transaction completely, then the next, so it is always correct but slow.
Two transactions with and operations can be interleaved in many ways, because each keeps its own order but the two are mixed. The count is
so with 3 and 2 operations there are schedules, of which only 2 are serial.
Which mixed schedules are safe? Two operations conflict if they belong to different transactions, touch the same item, and at least one is a write. So read-write, write-read and write-write pairs conflict, while two reads never do. Swapping two adjacent operations that do not conflict never changes the result.
A schedule is conflict serialisable if such swaps can turn it into a serial schedule. The test is the precedence graph: draw one node per transaction, and for every conflicting pair draw an edge when 's operation comes first. The schedule is conflict serialisable exactly when the graph has no cycle, and any topological order gives the equivalent serial order.
Worked example 1. Take . On A, before gives , and on B, before gives . The graph has no cycle, so the schedule is conflict serialisable with the serial order .
Worked example 2. The two clerks run . Here before gives , but before gives . The cycle shows that the schedule is not conflict serialisable, which is exactly the lost update.
Why it matters. Conflict serialisability is the practical meaning of isolation, and the precedence graph is the tool GATE expects you to use. Mark every conflicting pair, not just the obvious ones, before you look for a cycle.
View serialisability
Conflict serialisability is a little stricter than needed. Some schedules give the same result as a serial schedule even though their precedence graph has a cycle. View serialisability captures these by asking only what each transaction sees and what is left at the end.
Two schedules are view equivalent when three conditions hold for every item. The same transaction reads the initial value, every read takes its value from the same write, and the same transaction does the final write. A schedule is view serialisable if it is view equivalent to some serial schedule.
Worked example. Take . The pairs and give and , a cycle, so it is not conflict serialisable. Compare it with the serial order : reads the initial A in both, no other read exists, and writes A last in both, so the schedule is view serialisable.
What made this possible? and write A without reading it first, which is called a blind write. A view serialisable schedule that is not conflict serialisable always contains a blind write.
Every conflict serialisable schedule is view serialisable, but not the other way round. Testing view serialisability is NP-complete, so real systems use conflict-based methods. Exams use it mainly to check that you can tell the two classes apart.
Why it matters. GATE often asks "which of these schedules is view serialisable but not conflict serialisable". Look first for a cycle in the precedence graph, then for blind writes, then check the three view conditions.
Recoverable, cascadeless and strict schedules
Serialisability is about correctness when everyone commits. But a transaction may abort, and then anyone who read its data has read something that never officially existed. Three classes of schedules describe how safely aborts can be handled.
A schedule is recoverable if, whenever reads a value written by , commits before commits. In , commits after reading from ; if now aborts, cannot be undone, so the schedule is not recoverable. Swapping the commits to makes it recoverable.
Recoverable schedules can still cause cascading rollback. In , if aborts before committing, must abort too, and anyone who read from must follow. A schedule is cascadeless if transactions read only values written by committed transactions, so must come after .
A schedule is strict if no item is read or overwritten until the transaction that last wrote it has committed or aborted. So is cascadeless, since nobody reads, but it is not strict, because overwrites A before commits. Strictness lets recovery simply restore the old value of an aborted write.
The three classes nest inside one another: every strict schedule is cascadeless, and every cascadeless schedule is recoverable. These properties are independent of serialisability, so a schedule can be serialisable but not recoverable.
Why it matters. GATE gives a schedule with commits and asks which classes it belongs to. Check each "reads from" pair and the position of the commits, and test from the weakest class upward.
Locking and two-phase locking
The commonest way to enforce serialisability is locking. Before reading an item a transaction takes a shared lock (S), and before writing it takes an exclusive lock (X). Many transactions may hold S locks on the same item, but an X lock excludes every other lock.
| Requested | Held: S | Held: X |
|---|---|---|
| S | Granted | Wait |
| X | Wait | Wait |
Locks alone are not enough, because a transaction might unlock A, let another transaction change it, and lock it again. Two-phase locking (2PL) adds one rule: a transaction acquires locks only in a growing phase, and once it releases any lock it enters a shrinking phase and may take no new lock. The moment it holds its last new lock is its lock point.
Every schedule allowed by 2PL is conflict serialisable, and the equivalent serial order is the order of the lock points. But 2PL has two weaknesses. It allows deadlock, as when holds A and waits for B while holds B and waits for A. It also allows cascading rollback, because a transaction may release an X lock before committing and let others read its uncommitted value.
Strict 2PL fixes the second weakness by holding every X lock until commit or abort, so the schedules it allows are strict and cascadeless. Rigorous 2PL holds all locks, S and X, until commit, and orders transactions by their commit times. Conservative 2PL takes all its locks before it starts, so it never deadlocks, but it must know its items in advance.
Why it matters. Most real databases use strict 2PL. GATE asks whether a given sequence of lock and unlock steps obeys 2PL, and what problems a variant does or does not prevent.
Timestamp ordering and Thomas write rule
Locking makes transactions wait. Timestamp ordering takes another route: it fixes the serial order in advance by giving each transaction a timestamp when it starts, so an older transaction has a smaller timestamp. The protocol then rejects any operation that would break this order.
Each item X keeps two numbers, , the largest timestamp of any transaction that read X, and , the largest timestamp of any transaction that wrote X. The rules are short:
- On a read by T, if , a younger transaction has already written X, so T is rolled back; otherwise the read happens and becomes .
- On a write by T, if or , T is rolled back; otherwise the write happens and .
Worked example. Let and , and run with both counters of A starting at 0. The read is allowed and sets , and the write is allowed because , so it sets . Now has , so basic timestamp ordering rolls back.
Thomas write rule notices that 's write would be overwritten by the younger in the serial order anyway. So when it simply ignores the obsolete write and lets T continue, although the test still rolls T back. In the example is skipped, and the result matches the serial order .
The schedule above is not conflict serialisable, but it is view serialisable, so Thomas write rule accepts some view serialisable schedules that basic timestamp ordering rejects. Timestamp methods never make a transaction wait, so they are deadlock-free. But a long transaction may be rolled back again and again, which is starvation.
Why it matters. GATE gives a sequence of operations with timestamps and asks which ones are allowed. Keep a small table of and for each item and update it after every step.
Deadlock handling and log-based recovery
Locking can lead to deadlock, so the system must either prevent it or detect it. Two prevention schemes use timestamps, where a smaller timestamp means an older transaction. Suppose requests an item held by :
| Scheme | Older requests | Younger requests |
|---|---|---|
| Wait-die | waits | dies (rolled back) |
| Wound-wait | is wounded (rolled back) | waits |
In both schemes only one direction of waiting is allowed, so no cycle can form. A rolled-back transaction restarts with its original timestamp, so it grows older and is eventually never the victim, which prevents starvation. The alternative is detection: the system draws a wait-for graph with an edge when waits for , and a cycle means deadlock, so one victim is rolled back.
Recovery gives atomicity and durability through a log kept on stable storage. Its records include ⟨T start⟩, ⟨T, X, old value, new value⟩, ⟨T commit⟩ and ⟨T abort⟩. The write-ahead logging rule says a log record must reach stable storage before the data it describes, so the log can always undo or redo a change.
After a crash, a transaction with a ⟨T commit⟩ record is redone by writing the new values forward through the log. A transaction without one is undone by restoring the old values backward. With deferred update, changes reach the database only after commit, so undo is never needed; with immediate update, both undo and redo may be needed.
Worked example. The log reads ⟨T1 start⟩, ⟨T1, A, 100, 50⟩, ⟨T2 start⟩, ⟨T1 commit⟩, ⟨T2, B, 200, 300⟩, and then the system crashes. has committed, so it is redone and A is set to 50. has not committed, so it is undone and B goes back to 200.
A checkpoint writes all changed blocks to disk and records the active transactions in the log. Recovery then starts from the last checkpoint instead of the start of the log, which saves time.
Why it matters. GATE asks which transaction is rolled back under wait-die or wound-wait, and which are redone or undone from a log. Mark ages first, then apply the rule in the table.
Descriptive lens: how written papers ask this
Written papers for IT officer posts, state Assistant Programmer exams and university tests ask about transactions in four ways:
- Define the ACID properties and transaction states, with a banking or reservation example.
- Test a schedule for conflict serialisability with a precedence graph, and classify it as recoverable, cascadeless or strict.
- Explain a protocol, such as two-phase locking, timestamp ordering or a deadlock scheme, and what it guarantees.
- Describe log-based recovery and show the undo and redo steps for a given log.
Practice questions (our own, not previous-year questions).
- Explain the ACID properties with the example of a ₹500 transfer between two accounts, and name the DBMS component that ensures each one. (10 marks)
- Draw the precedence graph of and decide whether the schedule is conflict serialisable. (10 marks)
- Compare two-phase locking with timestamp ordering in terms of serialisability, deadlock and starvation. (15 marks)
Answer plan for question 2.
- List every pair of operations on the same item from different transactions in which at least one is a write.
- Write the edges: on A, before gives and before gives .
- Add the edges on B: before gives , and the pairs before give and .
- Point out the cycle between and and conclude that the schedule is not conflict serialisable.
- Add that it is not view serialisable either, because it has no blind write, so the cycle cannot be excused.
Quick revision
- A transaction is one unit of work, and uncontrolled concurrency causes lost updates and dirty reads.
- ACID means atomicity, consistency, isolation and durability.
- Two operations conflict when they come from different transactions, touch the same item, and include a write.
- A schedule is conflict serialisable exactly when its precedence graph has no cycle.
- Two transactions of and operations have interleavings.
- View serialisability also admits schedules with blind writes, and every conflict serialisable schedule is view serialisable.
- Strict schedules are cascadeless, and cascadeless schedules are recoverable.
- 2PL guarantees conflict serialisability but allows deadlock, while strict 2PL also prevents cascading rollback.
- Timestamp ordering rolls back operations that break timestamp order, and Thomas write rule ignores obsolete writes.
- In wait-die the older waits and the younger dies, while in wound-wait the older wounds and the younger waits.
- After a crash, committed transactions are redone and uncommitted ones are undone, following write-ahead logging.