Introduction
When multiple users try to change data at the same time, conflicts can happen that cause errors or wrong results. Two-phase locking helps manage these changes so that everyone sees consistent data and no conflicts occur.
Imagine a library where people want to read and write notes in shared books. Before writing, a person must put a 'Do Not Disturb' sign on the book and keep it until done. Only after finishing and removing the sign can others use the book.
┌───────────────────────┐
│ Transaction Starts │
└──────────┬────────────┘
│
▼
┌───────────────────────┐
│ Locking Phase │
│ (Acquire all locks) │
└──────────┬────────────┘
│
▼
┌───────────────────────┐
│ Unlocking Phase │
│ (Release all locks) │
└──────────┬────────────┘
│
▼
┌───────────────────────┐
│ Transaction Ends │
└───────────────────────┘