Computer Science and IT‹›⌂ 0:00 studied

Module 10 · 16 min read · Free chapter

Transactions and concurrency control

In short

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.

What are you preparing for? We show the practice and previous year questions of your exam.

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 r1(A)r_1(A) for "transaction T1T_1 reads A", w2(A)w_2(A) for "T2T_2 writes A", and c1c_1 for "T1T_1 commits". A serial schedule runs one transaction completely, then the next, so it is always correct but slow.

Two transactions with mm and nn operations can be interleaved in many ways, because each keeps its own order but the two are mixed. The count is

(m+n)!m! n!\frac{(m + n)!}{m!\,n!}

so with 3 and 2 operations there are 5!3! 2!=10\dfrac{5!}{3!\,2!} = 10 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 Ti→TjT_i \to T_j when TiT_i'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 r1(A),w1(A),r2(A),r3(B),w2(A),w3(B),r1(B)r_1(A), w_1(A), r_2(A), r_3(B), w_2(A), w_3(B), r_1(B). On A, w1(A)w_1(A) before r2(A)r_2(A) gives T1→T2T_1 \to T_2, and on B, w3(B)w_3(B) before r1(B)r_1(B) gives T3→T1T_3 \to T_1. The graph has no cycle, so the schedule is conflict serialisable with the serial order T3,T1,T2T_3, T_1, T_2.

Worked example 2. The two clerks run r1(A),r2(A),w1(A),w2(A)r_1(A), r_2(A), w_1(A), w_2(A). Here r1(A)r_1(A) before w2(A)w_2(A) gives T1→T2T_1 \to T_2, but r2(A)r_2(A) before w1(A)w_1(A) gives T2→T1T_2 \to T_1. 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 r1(A),w2(A),w1(A),w3(A)r_1(A), w_2(A), w_1(A), w_3(A). The pairs r1(A),w2(A)r_1(A), w_2(A) and w2(A),w1(A)w_2(A), w_1(A) give T1→T2T_1 \to T_2 and T2→T1T_2 \to T_1, a cycle, so it is not conflict serialisable. Compare it with the serial order T1,T2,T3T_1, T_2, T_3: T1T_1 reads the initial A in both, no other read exists, and T3T_3 writes A last in both, so the schedule is view serialisable.

What made this possible? T2T_2 and T3T_3 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 TjT_j reads a value written by TiT_i, TiT_i commits before TjT_j commits. In w1(A),r2(A),c2,c1w_1(A), r_2(A), c_2, c_1, T2T_2 commits after reading from T1T_1; if T1T_1 now aborts, T2T_2 cannot be undone, so the schedule is not recoverable. Swapping the commits to w1(A),r2(A),c1,c2w_1(A), r_2(A), c_1, c_2 makes it recoverable.

Recoverable schedules can still cause cascading rollback. In w1(A),r2(A)w_1(A), r_2(A), if T1T_1 aborts before committing, T2T_2 must abort too, and anyone who read from T2T_2 must follow. A schedule is cascadeless if transactions read only values written by committed transactions, so r2(A)r_2(A) must come after c1c_1.

A schedule is strict if no item is read or overwritten until the transaction that last wrote it has committed or aborted. So w1(A),w2(A),c1w_1(A), w_2(A), c_1 is cascadeless, since nobody reads, but it is not strict, because T2T_2 overwrites A before T1T_1 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 T1T_1 holds A and waits for B while T2T_2 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 TS(T)\mathit{TS}(T) 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, RTS(X)\mathit{RTS}(X), the largest timestamp of any transaction that read X, and WTS(X)\mathit{WTS}(X), the largest timestamp of any transaction that wrote X. The rules are short:

  • On a read by T, if TS(T)<WTS(X)\mathit{TS}(T) < \mathit{WTS}(X), a younger transaction has already written X, so T is rolled back; otherwise the read happens and RTS(X)\mathit{RTS}(X) becomes max⁡(RTS(X),TS(T))\max(\mathit{RTS}(X), \mathit{TS}(T)).
  • On a write by T, if TS(T)<RTS(X)\mathit{TS}(T) < \mathit{RTS}(X) or TS(T)<WTS(X)\mathit{TS}(T) < \mathit{WTS}(X), T is rolled back; otherwise the write happens and WTS(X)=TS(T)\mathit{WTS}(X) = \mathit{TS}(T).

Worked example. Let TS(T1)=10\mathit{TS}(T_1) = 10 and TS(T2)=20\mathit{TS}(T_2) = 20, and run r1(A),w2(A),w1(A)r_1(A), w_2(A), w_1(A) with both counters of A starting at 0. The read r1(A)r_1(A) is allowed and sets RTS(A)=10\mathit{RTS}(A) = 10, and the write w2(A)w_2(A) is allowed because 20≥1020 \ge 10, so it sets WTS(A)=20\mathit{WTS}(A) = 20. Now w1(A)w_1(A) has 10<WTS(A)=2010 < \mathit{WTS}(A) = 20, so basic timestamp ordering rolls T1T_1 back.

Thomas write rule notices that T1T_1's write would be overwritten by the younger T2T_2 in the serial order anyway. So when TS(T)<WTS(X)\mathit{TS}(T) < \mathit{WTS}(X) it simply ignores the obsolete write and lets T continue, although the test TS(T)<RTS(X)\mathit{TS}(T) < \mathit{RTS}(X) still rolls T back. In the example w1(A)w_1(A) is skipped, and the result matches the serial order T1,T2T_1, T_2.

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 RTS\mathit{RTS} and WTS\mathit{WTS} 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 TiT_i requests an item held by TjT_j:

Scheme Older TiT_i requests Younger TiT_i requests
Wait-die TiT_i waits TiT_i dies (rolled back)
Wound-wait TjT_j is wounded (rolled back) TiT_i 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 Ti→TjT_i \to T_j when TiT_i waits for TjT_j, 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. T1T_1 has committed, so it is redone and A is set to 50. T2T_2 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:

  1. Define the ACID properties and transaction states, with a banking or reservation example.
  2. Test a schedule for conflict serialisability with a precedence graph, and classify it as recoverable, cascadeless or strict.
  3. Explain a protocol, such as two-phase locking, timestamp ordering or a deadlock scheme, and what it guarantees.
  4. Describe log-based recovery and show the undo and redo steps for a given log.

Practice questions (our own, not previous-year questions).

  1. 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)
  2. Draw the precedence graph of r1(A),r2(B),w2(A),r3(A),w1(B),w3(B)r_1(A), r_2(B), w_2(A), r_3(A), w_1(B), w_3(B) and decide whether the schedule is conflict serialisable. (10 marks)
  3. 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, r1(A)r_1(A) before w2(A)w_2(A) gives T1→T2T_1 \to T_2 and w2(A)w_2(A) before r3(A)r_3(A) gives T2→T3T_2 \to T_3.
  • Add the edges on B: r2(B)r_2(B) before w1(B)w_1(B) gives T2→T1T_2 \to T_1, and the pairs before w3(B)w_3(B) give T2→T3T_2 \to T_3 and T1→T3T_1 \to T_3.
  • Point out the cycle between T1T_1 and T2T_2 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 mm and nn operations have (m+n)!m! n!\dfrac{(m + n)!}{m!\,n!} 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.

Check yourself

Exam-style questions on this chapter. Pick an answer to see the explanation. 0 of 14 answered

  1. Two clerks read the same seat count, and both write back their own decremented value. This problem is called:

  2. The property that the effects of a committed transaction survive a crash is:

  3. After executing its last statement but before its changes are safely recorded, a transaction is:

  4. Which pair of operations does NOT conflict?

  5. The schedule r1(A),w1(A),r2(A),r3(B),w2(A),w3(B),r1(B)r_1(A), w_1(A), r_2(A), r_3(B), w_2(A), w_3(B), r_1(B) is conflict equivalent to which serial order?

  6. T1T_1 has 3 operations and T2T_2 has 2. How many schedules interleave them, each keeping its own order?

  7. r1(A),w2(A),w1(A),w3(A)r_1(A), w_2(A), w_1(A), w_3(A) is:

  8. The schedule w1(A),r2(A),c2,c1w_1(A), r_2(A), c_2, c_1 is:

  9. Basic two-phase locking guarantees:

  10. Which protocol holds every exclusive lock until the transaction commits or aborts?

  11. TS(T1)=10\mathit{TS}(T_1) = 10, TS(T2)=20\mathit{TS}(T_2) = 20. Under basic timestamp ordering, the schedule r1(A),w2(A),w1(A)r_1(A), w_2(A), w_1(A) leads to:

  12. Under Thomas write rule, a write by T with TS(T)<WTS(X)\mathit{TS}(T) < \mathit{WTS}(X) (and not older than the last read) is:

  13. Under wait-die, a younger transaction requests an item held by an older one. The younger transaction:

  14. The log shows ⟨T1 start⟩, ⟨T1, A, 100, 50⟩, ⟨T2 start⟩, ⟨T1 commit⟩, ⟨T2, B, 200, 300⟩ and then a crash. Recovery will:

Previous year questions

Previous year questions for UPSC CSE will appear here once each one is checked word for word against the official question paper.

Learn with AI

A tutor that asks instead of telling, a partner that argues the other side, and this chapter told through your own district.

Tell the tutor what you are stuck on. It will guide you with questions and give the answer after a few tries.

🙋 Raise a doubt or feedback

Stuck somewhere, or spotted a mistake? Write to the ExamHike team. We usually reply within a day, and you will see the reply on My progress.

Log in to ask a doubt or send feedback →

Frequently asked questions

How do you test a schedule for conflict serialisability?

Draw a precedence graph with an edge from Ti to Tj for each conflict where Ti acts first; the schedule is conflict serialisable if there is no cycle.

What is the difference between wait-die and wound-wait?

In wait-die an older requester waits and a younger one dies; in wound-wait an older requester rolls back the holder and a younger one waits.

What does Thomas write rule do?

It ignores an obsolete write whose timestamp is older than the item's last write, instead of rolling the transaction back.

Does two-phase locking prevent deadlock?

No. Basic 2PL guarantees conflict serialisability but can deadlock; only conservative 2PL, which takes all locks in advance, avoids it.

More in this module: Databases

Official sources

Facts in this chapter were checked against these official sources.

  1. GATE 2026 Computer Science and Information Technology (CS) syllabus, IIT Guwahati
  2. NPTEL Database Systems (IIT Kanpur, 106104135): transactions, concurrency control and recovery