Many users work on the same database at once. Concurrency control makes the result as correct as if they had run one after another.
Lock-based protocols
A lock stops others from using a data item in a conflicting way.
- Shared lock (S): for reading. Many transactions can hold it together.
- Exclusive lock (X): for writing. Only one transaction can hold it, and no shared locks may coexist.
| Held \ Requested | S | X |
|---|---|---|
| S | Yes | No |
| X | No | No |
Before reading, a transaction asks for S; before writing, it asks for X. The lock manager grants or delays each request.
Two-phase locking (2PL)
A transaction has two phases:
- Growing: it may take locks but not release any.
- Shrinking: it releases locks and may not take new ones.
2PL guarantees that the schedule is conflict serializable. Strict 2PL holds all exclusive locks until commit, which also prevents dirty reads and cascading rollbacks.
Serializability and recoverability
A schedule is serializable if its effect equals some serial schedule. Conflict serializability is tested with a precedence graph: draw an arrow Ti → Tj when an operation of Ti conflicts with a later one of Tj. If the graph has no cycle, the schedule is serializable.
A schedule is recoverable if no transaction commits before the transaction whose data it read has committed. A cascadeless schedule goes further and stops one abort from forcing others to abort.
Deadlocks
A deadlock happens when each transaction waits for a lock held by another: T1 holds A and wants B, while T2 holds B and wants A.
- Prevention: order the locks or use timestamps (wait-die, wound-wait).
- Detection: build a wait-for graph; a cycle means deadlock. One transaction (the victim) is rolled back.
- Timeout: abort transactions that wait too long.
Performance of locking
Locks cause waiting. Smaller lock units (row level instead of table level) allow more parallel work, and short transactions hold locks for less time. Too many blocked transactions reduce throughput.
Crash recovery in brief
The DBMS writes every change to a log before changing the data (write-ahead logging). After a crash it redoes committed work and undoes uncommitted work using the log.
Common mistakes
- Believing 2PL prevents deadlocks. It does not.
- Releasing an exclusive lock early, which allows dirty reads.
Practice questions
- Differentiate shared and exclusive locks.
- Explain the two-phase locking protocol.
- What is a serializable schedule? How is it tested?
- What is a deadlock? How can it be handled?
- Why is a log used in recovery?
Answers to the practice questions
- Shared allows many readers; exclusive allows one writer and blocks all others.
- A growing phase where locks are taken, then a shrinking phase where they are released; no lock after the first release.
- One equivalent to a serial schedule; tested with a precedence graph with no cycles.
- Transactions waiting on each other forever; prevent, detect with a wait-for graph, or time out.
- It records changes so committed work can be redone and uncommitted work undone after a crash.
Sources
- Official BCA syllabus Database Management Systems, course code CC202, Unit III
- Raghu Ramakrishnan and Johannes Gehrke, Database Management Systems, 3rd edition, McGraw-Hill
