What problems can occur when transactions run concurrently without control?
MediumConceptConcurrency Control & Locking
Answer
Without concurrency control, interleaving the operations of transactions can produce results that no serial order would produce. The four classic problems are:
- Lost update: two transactions read the same value and both write it back, so one update overwrites the other.
- Dirty read: a transaction reads data written by another transaction that has not committed yet and may still roll back.
- Non-repeatable read: a transaction reads the same row twice and gets different values, because another transaction updated it and committed in between.
- Phantom read: a transaction repeats a range query and sees new rows that another committed transaction inserted.
Example: A lost update on a stock count of 10:
T1: read stock = 10
T2: read stock = 10
T1: write stock = 10 - 3 = 7
T2: write stock = 10 - 2 = 8
Five items were sold, so the stock should be 5, but it ends at 8, because T1's update is lost.