Chapter 04
Transactions & Concurrency Control
6 topics · 2 sections · beginner-friendly study notes · Updated
A transaction is a group of operations that must happen completely or not at all, like a bank money transfer.
Concurrency control makes sure that many transactions running together still give a correct result.
Schedules, serializability, 2PL and deadlock schemes are among the most repeated GATE topics in DBMS.
Safe money transfers, ticket booking, stock updates and any multi-user system.
ACID Properties of Transactions
A transaction is a logical unit of work made of reads and writes. ACID stands for Atomicity, Consistency, Isolation and Durability, the four properties every transaction must follow.
In short: A bank transfer must either fully happen or not happen at all. It must keep the books correct, not mix with other transfers, and survive a power cut.
Think about it
ACID is the guarantee that makes banking, booking and payments safe. If the power goes after money leaves your account but before it arrives in your friend's account, atomicity returns your money.
Atomicity: all operations happen or none do.
Handled by the recovery manager using the log (undo).
Consistency: a transaction takes the database from one correct state to another correct state (for example, total money stays the same).
The programmer and the integrity constraints take care of this.
Isolation: transactions running together behave as if they ran one after another.
Handled by the concurrency-control manager.
Durability: once committed, changes stay even after a crash.
Handled by the recovery manager using the log (redo).
Example
transferring 500 from account A to account B. Debit A and credit B must both happen.
If the system crashes after the debit, atomicity must undo it.
Key points
- Atomicity: All or nothing.
- Consistency: Valid state to valid state.
- Isolation: Parallel transactions do not disturb each other.
- Durability: Committed data is permanent.
- Log: A file that records every change, used for undo and redo.
- Atomicity and Durability
→recovery manager. Isolation→concurrency-control manager. - Consistency is the responsibility of the application and integrity constraints.
Example
CodeBank transfer as one transaction: BEGIN; UPDATE account SET balance = balance - 500 WHERE acc_no = 101; UPDATE account SET balance = balance + 500 WHERE acc_no = 102; COMMIT; -- if anything fails, use ROLLBACK instead
Transaction States
A transaction moves through these states: Active, Partially Committed, Committed, Failed and Aborted (and finally Terminated).
In short: It starts running (active), finishes its last step (partially committed), and then either saves permanently (committed) or is cancelled (failed, then aborted).
Think about it
Knowing the states explains when changes become permanent and what recovery has to do after a failure. When you press "Pay", your order is active. After the last step it is partially committed. After the bank writes it permanently to disk, it is committed.
Active: the transaction is executing its operations.
Partially committed: the last statement has run, but the changes may still be only in memory.
Committed: changes are safely recorded on disk, which means they are permanent.
Failed: a problem was found, either during execution or at the commit check.
Aborted: the transaction is rolled back and the database returns to the state before it started.
After an abort, the system may restart the transaction (if the cause was hardware or system error) or kill it (if the cause was a logic error).
A committed transaction can never be rolled back.
To undo its effect, you need a new compensating transaction.
Key points
- Active: Transaction is executing.
- Partially committed: Final statement executed; waiting to be saved safely.
- Committed: Changes are permanent.
- Failed: Normal execution cannot continue.
- Aborted: Rolled back; database restored to the old state.
- Paths: Active
→Partially committed→Committed, or Active/Partially committed→Failed→Aborted. - Committed and Aborted are the two final states.
Read more
Recoverable & Cascadeless Schedules
A schedule is the order in which operations of many transactions are run. A schedule is recoverable if a transaction commits only after every transaction whose data it read has committed. A schedule is cascadeless if a transaction reads only data written by already committed transactions.
In short: Do not commit work that is built on someone else's unsaved work. And do not even read someone else's unsaved work, so a rollback never forces other rollbacks.
Think about it
Recoverability makes sure that a failure does not leave the database wrong. Cascadeless saves time by avoiding chains of rollbacks. If you give a friend a loan from money that has not actually arrived yet, and the money transfer is cancelled, your loan decision was wrong. This is the cascade problem.
Dirty read: reading data written by a transaction that has not yet committed.
Non-recoverable: T2 reads a value written by T1 and commits before T1.
If T1 then aborts, T2 has used wrong data, but T2 is already committed and cannot be undone.
Recoverable: T2 commits after T1.
But if T1 aborts, T2 must also abort, and so on.
This chain of aborts is a cascading rollback.
Cascadeless (ACA): no dirty reads at all, so no cascading rollback.
Strict schedule: no read or write of an item until the last writer commits or aborts.
Relationship: Strict ⊂ Cascadeless ⊂ Recoverable ⊂ All schedules.
Example
W1(A) R2(A) C2 C1 is not recoverable.
W1(A) R2(A) C1 C2 is recoverable but not cascadeless.
W1(A) C1 R2(A) C2 is cascadeless.
Key points
- Dirty read: Reading uncommitted data.
- Cascading rollback: One abort forces other transactions to abort.
- Strict schedule: No read or write on an item until the last writer commits or aborts.
- Strict ⊂ Cascadeless ⊂ Recoverable.
- Recoverable allows dirty reads but needs the right commit order.
- Cascadeless allows no dirty reads.
Read more
Conflict Serializability
A schedule is conflict serializable if it can be turned into a serial schedule by swapping only non-conflicting operations. Two operations conflict when they belong to different transactions, work on the same data item and at least one is a write.
In short: If a mixed-up schedule gives the same result as running the transactions one by one in some order, it is safe.
Think about it
Serializability is the correctness rule of concurrency control. It lets many users work together without wrong results. Two clerks updating the same account at the same time must give the same final balance as if they had worked one after the other.
Conflict pairs: Read-Write, Write-Read and Write-Write on the same item.
Read-Read never conflicts.
Test with a precedence graph: draw a node for each transaction.
Draw an edge Ti → Tj if an operation of Ti conflicts with a later operation of Tj.
If the graph has no cycle, the schedule is conflict serializable, and any topological order of the graph gives an equivalent serial schedule.
If there is a cycle, it is not.
Example
R1(A) R2(A) W1(A) W2(A).
R2(A) before W1(A) gives T2 → T1.
R1(A) before W2(A) gives T1 → T2.
This is a cycle, so the schedule is not conflict serializable.
View serializability is a weaker idea: every conflict serializable schedule is view serializable, but not the other way round.
Number of serial schedules for n transactions = n!.
Key points
- Conflict: Different transactions, same item, at least one write.
- Precedence graph: Directed graph of transactions with edges for conflicts.
- Serial schedule: One transaction fully runs before the next one starts.
- View serializable: Same reads-from relation and final writes as some serial schedule.
- Cycle in the precedence graph ⟺ not conflict serializable.
- Conflict serializable ⊂ view serializable.
- Serializable does not mean recoverable. Check both separately.
Two-Phase Locking (2PL)
Two-Phase Locking is a protocol where each transaction has a growing phase, in which it only takes locks, and a shrinking phase, in which it only releases locks. After the first unlock, no new lock can be taken.
In short: First collect all the keys you need. Once you start giving keys back, you cannot take any new ones.
Think about it
Locking is the most common way databases give isolation, so correct results are guaranteed. Think of a bank locker room: you take every locker key you need first, then you open and use them, and give the keys back only at the end.
Lock types: Shared lock (S) for reading, many transactions can hold it together.
Exclusive lock (X) for writing, only one transaction can hold it.
Compatibility: S-S is allowed, and S-X, X-S and X-X are not allowed.
The point where a transaction gets its last lock is its lock point.
Basic 2PL guarantees conflict serializability, and the serial order follows lock points.
But it does not prevent deadlock and does not prevent cascading rollback.
Strict 2PL: holds all exclusive locks until commit or abort, so the schedule is strict and cascadeless.
Rigorous 2PL: holds all locks (shared and exclusive) until commit, so transactions are serialized in commit order.
Conservative 2PL: takes all locks before it starts.
It is deadlock-free, but not always practical because a transaction must know all items in advance.
Key points
- Shared lock (S): Read lock; many holders allowed.
- Exclusive lock (X): Write lock; only one holder.
- Lock point: The moment a transaction holds its last lock.
- Strict 2PL: Hold exclusive locks until commit or abort.
- Rigorous 2PL: Hold all locks until commit or abort.
- Basic 2PL: serializable, but deadlock and cascading rollback are possible.
- Strict 2PL: serializable and cascadeless, but deadlock is still possible.
- Conservative 2PL: deadlock-free.
- Compatibility: only S-S is compatible.
Deadlock Schemes in DBMS
A deadlock happens when two or more transactions wait for each other forever because each holds a lock the other needs. DBMS uses prevention, detection and recovery schemes to handle it.
In short: Two people each hold one key and each needs the other's key. Nobody moves.
Think about it
Locking can freeze the system if deadlocks are not handled. Every real DBMS has a deadlock strategy. Two cars meet on a narrow bridge from opposite sides. Neither can move forward until one of them backs up. That backing up is the rollback of the victim.
Example
T1 holds a lock on A and wants B, while T2 holds B and wants A. Detection uses a wait-for graph: nodes are transactions and an edge Ti → Tj means Ti is waiting for Tj.
A cycle in the wait-for graph means deadlock.
To recover, choose a victim (usually the one with the least work done), roll it back, and make sure the same transaction is not always chosen (starvation).
Prevention schemes use timestamps (an older transaction has a smaller timestamp).
Wait-Die (non-preemptive): if an older transaction asks for a lock held by a younger one, it waits.
If a younger transaction asks for a lock held by an older one, it dies (aborts and restarts with the same timestamp).
Wound-Wait (preemptive): if an older transaction asks for a lock held by a younger one, it wounds the younger (forces it to abort).
If a younger transaction asks for a lock held by an older one, it waits.
Another simple method is timeout: abort the transaction that waited too long.
In both wait-die and wound-wait, there is no deadlock and no starvation, because restarted transactions keep their original timestamp.
Key points
- Wait-for graph: Graph showing which transaction waits for which. A cycle means deadlock.
- Victim: The transaction chosen to be aborted to break a deadlock.
- Starvation: A transaction is rolled back again and again and never finishes.
- Wait-Die: Older waits, younger dies.
- Wound-Wait: Older wounds (aborts) younger, younger waits.
- Wait-Die: older waits, younger dies. Wound-Wait: older wounds, younger waits.
- Detection: wait-for graph cycle. Prevention: wait-die, wound-wait, conservative 2PL.
- Restarted transactions keep their original timestamp to avoid starvation.
Read more
Solved examples
Schedule S: R1(A) R2(A) W1(A) W2(A). Is it conflict serializable?
Check with a precedence graph
Formula Applied:—Approach note: Cycle in the precedence graph means not serializable.
Classify: (a) W1(A) R2(A) C2 C1 (b) W1(A) R2(A) C1 C2 (c) W1(A) C1 R2(A) C2
Recoverable or cascadeless for each
Formula Applied:—Approach note: Look for who reads from whom, then compare commit order.
T1 has 2 operations and T2 has 3 operations. How many different interleaved schedules are possible, and how many serial schedules for 3 transactions?
Counting schedules
Formula Applied:—Approach note: Interleavings = (n1 + n2)! / (n1! × n2!).
T1 has timestamp 5 and T2 has timestamp 10. Apply wait-die and wound-wait when (i) T1 requests a lock held by T2, (ii) T2 requests a lock held by T1.
Outcome in each case
Formula Applied:—Approach note: Wait-Die: older waits. Wound-Wait: older wounds.
Real-world applications
Using Transactions & Concurrency Control in real projects
Safe money transfers, ticket booking, stock updates and any multi-user system.
Common mistakes
❌ Myth: A conflict serializable schedule is always recoverable.
Why this is wrong: Serializability and recoverability are separate properties.
✅ Verified Truth: A schedule can be serializable but not recoverable if a transaction commits before the one it read from.
⚠️ Exam Tip: Check serializability and recoverability separately.
❌ Myth: 2PL prevents deadlock.
Why this is wrong: Two transactions can each hold a lock and wait for the other in the growing phase.
✅ Verified Truth: 2PL guarantees serializability only. Conservative 2PL is deadlock-free.
⚠️ Exam Tip: Remember "basic and strict 2PL can deadlock".
❌ Myth: Consistency in ACID is the same as consistency in CAP.
Why this is wrong: ACID consistency is about valid database rules. CAP consistency is about all replicas showing the same latest value.
✅ Verified Truth: They are different ideas with the same word.
⚠️ Exam Tip: Say which one you mean in the answer.
Revision notes
A transaction is a group of operations that must happen completely or not at all, like a bank money transfer. Concurrency control makes sure that many transactions running together still give a correct result.
High-Yield Quick Revision Points
- ACID: Atomicity, Consistency, Isolation, Durability
- States: Active → Partially committed → Committed, or Failed → Aborted
- Strict ⊂ Cascadeless ⊂ Recoverable
- Conflict: different transactions, same item, at least one write
- Cycle in precedence graph → not conflict serializable
- 2PL: growing phase then shrinking phase; ensures serializability
- Strict 2PL: cascadeless. Conservative 2PL: deadlock-free
- Wait-Die: older waits, younger dies. Wound-Wait: older wounds, younger waits
Practice questions
Which ACID property is mainly ensured by the recovery manager using undo?
Which is the final state of a successful transaction?
The schedule W1(A) R2(A) C1 C2 is:
A schedule is conflict serializable if its precedence graph:
Which protocol makes the schedule cascadeless and holds exclusive locks until commit?
In wait-die, when a younger transaction requests a lock held by an older one, it: