1. What is Concurrency Control?
π It manages multiple transactions running together so they donβt mess up data.
Goal: Correct results β
, no conflicts β, consistency maintained.
2. Locks (to control access)
- Shared Lock (S):
- For read only.
- Many can read at the same time.
- Example: 2 users check the bank balance together.
- Exclusive Lock (X):
- For write (update/delete).
- Only 1 transaction allowed β no read/write by others.
- Example: Updating balance.
Lock Compatibility Table
| Lock on Data | New Shared Lock? | New Exclusive Lock? |
|---|---|---|
| Shared (S) | β Yes | β No |
| Exclusive (X) | β No | β No |
3. Deadlock
What is it?
π Two transactions wait for each otherβs locks β stuck forever.
Example:
- T1 locks A, waits for B.
- T2 locks B, waits for A.
- Both are stuck.
Prevention Methods:
- Timeout: Cancel if waiting too long.
- Wait-Die: Old waits, young dies (cancelled).
- Wound-Wait: Old wounds (forces young to cancel).
4. Isolation Levels (Control accuracy vs speed)
| Level | Dirty Read | Non-Repeatable Read | Phantom Read |
|---|---|---|---|
| Read Uncommitted | β Possible | β Possible | β Possible |
| Read Committed | β Prevented | β Possible | β Possible |
| Repeatable Read | β Prevented | β Prevented | β Possible |
| Serializable | β Prevented | β Prevented | β Prevented |
Quick Meaning:
- Read Uncommitted: Fastest β‘, least safe (reads dirty data).
- Read Committed: Prevents dirty reads, but data may change later.
- Repeatable Read: Same data stays same if read again.
- Serializable: Safest π‘οΈ, like transactions run one by one (but slowest).
5. Key Takeaways (for quick revision)
- Locks: S β read, X β write.
- Deadlock: Transactions block each other. Solutions = Timeout / Wait-Die / Wound-Wait.
- Isolation Levels:
- Read Uncommitted β Least safe.
- Serializable β Most safe.
MCQ
1. Basic Concepts of Concurrency Control
- What is the primary goal of concurrency control in a database?
- A) Improve transaction speed.
- B) Prevent unauthorized access.
- C) Ensure data consistency when multiple transactions execute.
- D) Reduce database storage.
Answer: C) Ensure data consistency when multiple transactions execute.
- Which of the following problems does concurrency control aim to prevent?
- A) Data loss during system crashes.
- B) Conflicts between multiple transactions.
- C) Database schema corruption.
- D) Excessive query optimization.
Answer: B) Conflicts between multiple transactions.
2. Locks
- Which type of lock allows multiple transactions to read but not write the same data?
- A) Shared Lock
- B) Exclusive Lock
- C) Write Lock
- D) Deadlock
Answer: A) Shared Lock
- What happens when an exclusive lock is applied to a data item?
- A) Multiple transactions can read the item.
- B) Only one transaction can write to or read the item.
- C) The lock is automatically converted to a shared lock.
- D) Other transactions are allowed to access the item freely.
Answer: B) Only one transaction can write to or read the item.
- Which of the following combinations is valid under lock compatibility rules?
- A) Shared lock and exclusive lock on the same data item.
- B) Multiple shared locks on the same data item.
- C) Multiple exclusive locks on the same data item.
- D) A shared lock followed by an exclusive lock on the same data item.
Answer: B) Multiple shared locks on the same data item.
3. Deadlock
- What is a deadlock?
- A) A state where the database server stops responding.
- B) A situation where two or more transactions wait indefinitely for each otherβs locks.
- C) A mechanism to manage multiple locks in a database.
- D) A technique to improve transaction speed.
Answer: B) A situation where two or more transactions wait indefinitely for each otherβs locks.
- Which technique helps prevent deadlocks by setting a time limit for waiting?
- A) Wound-Wait
- B) Timeout
- C) Wait-Die
- D) Timestamp Ordering
Answer: B) Timeout
- In the Wound-Wait scheme for deadlock prevention:
- A) Older transactions force younger ones to release locks.
- B) Younger transactions force older ones to release locks.
- C) Transactions wait for a fixed period before acquiring a lock.
- D) Deadlocks are resolved by restarting all transactions.
Answer: A) Older transactions force younger ones to release locks.
- Which of the following is NOT a method for deadlock prevention?
- A) Wait-Die
- B) Timeout
- C) Shared Locks
- D) Wound-Wait
Answer: C) Shared Locks
4. Isolation Levels
- At which isolation level can dirty reads occur?
- A) Serializable
- B) Repeatable Read
- C) Read Committed
- D) Read Uncommitted
Answer: D) Read Uncommitted
- What is the highest level of isolation in databases?
- A) Serializable
- B) Repeatable Read
- C) Read Committed
- D) Read Uncommitted
Answer: A) Serializable
- Which isolation level allows phantom reads?
- A) Serializable
- B) Repeatable Read
- C) Read Committed
- D) Read Uncommitted
Answer: B) Repeatable Read
- Which problem does the Read Committed isolation level prevent?
- A) Dirty reads
- B) Non-repeatable reads
- C) Phantom reads
- D) Lost updates
Answer: A) Dirty reads
- Match the isolation level with the problems it prevents:
- A) Read Uncommitted β Dirty Reads, Non-Repeatable Reads
- B) Read Committed β Dirty Reads
- C) Serializable β Dirty Reads, Non-Repeatable Reads, Phantom Reads
- D) Repeatable Read β Dirty Reads, Phantom Reads
Answer: C) Serializable β Dirty Reads, Non-Repeatable Reads, Phantom Reads
5. Scenario-Based Questions
- A transaction reads uncommitted changes made by another transaction. Which isolation level is being used?
- A) Read Committed
- B) Repeatable Read
- C) Serializable
- D) Read Uncommitted
Answer: D) Read Uncommitted
- Two transactions are accessing the same row:
- T1 has a shared lock to read the row.
- T2 wants an exclusive lock to update the row.
What will happen? - A) T2 proceeds immediately.
- B) T2 waits until T1 releases the shared lock.
- C) T1 and T2 share the lock simultaneously.
- D) Both transactions are aborted.
Answer: B) T2 waits until T1 releases the shared lock.
- A transaction T1 locks data item A, and T2 locks data item B. If T1 waits for B and T2 waits for A, which method can resolve the deadlock?
- A) Wound-Wait
- B) Timeout
- C) Wait-Die
- D) Any of the above
Answer: D) Any of the above
6. Advanced Concepts
- Which isolation level ensures that transactions execute as if they were running one at a time?
- A) Serializable
- B) Repeatable Read
- C) Read Committed
- D) Read Uncommitted
Answer: A) Serializable
- What is a “phantom read”?
- A) Reading uncommitted changes from another transaction.
- B) Reading the same data twice but getting different results.
- C) New rows appear or existing rows disappear between reads.
- D) A transaction failing due to system crashes.
Answer: C) New rows appear or existing rows disappear between reads.
- Which isolation level balances speed and data integrity by preventing dirty reads but allowing phantom reads?
- A) Read Uncommitted
- B) Read Committed
- C) Repeatable Read
- D) Serializable
Answer: B) Read Committed
