Lock-based protocols in DBMS Theory - Time & Space Complexity
Lock-based protocols control access to data in databases to avoid conflicts.
We want to understand how the time to manage locks grows as more transactions run.
Analyze the time complexity of the following lock management process.
BEGIN TRANSACTION;
FOR each data item needed {
ACQUIRE lock on data item;
READ or WRITE data item;
RELEASE lock on data item;
}
COMMIT TRANSACTION;
This code shows a transaction acquiring and releasing locks on multiple data items sequentially.
Look for repeated actions that affect time.
- Primary operation: Acquiring and releasing locks for each data item.
- How many times: Once per data item accessed in the transaction.
As the number of data items accessed grows, the number of lock operations grows too.
| Input Size (n) | Approx. Operations |
|---|---|
| 10 | About 10 lock acquire and 10 release operations |
| 100 | About 100 lock acquire and 100 release operations |
| 1000 | About 1000 lock acquire and 1000 release operations |
Pattern observation: The total lock operations increase directly with the number of data items accessed.
Time Complexity: O(n)
This means the time to manage locks grows linearly with the number of data items accessed in a transaction.
[X] Wrong: "Lock management time stays the same no matter how many data items are accessed."
[OK] Correct: Each data item needs its own lock operations, so more items mean more time spent managing locks.
Understanding how lock operations scale helps you explain database concurrency control clearly and confidently.
What if multiple transactions try to lock the same data items at the same time? How would that affect the time complexity?
Practice
Solution
Step 1: Understand the role of lock-based protocols
Lock-based protocols manage how multiple users access data simultaneously to avoid conflicts.Step 2: Identify the main goal
The main goal is to control concurrent access and maintain data consistency.Final Answer:
To control concurrent access to data and prevent conflicts -> Option AQuick Check:
Lock-based protocols = control access [OK]
- Confusing locking with data backup
- Thinking locks speed up queries
- Mixing locking with encryption
X in a lock-based protocol?Solution
Step 1: Understand shared lock syntax
Shared locks allow multiple readers and are usually requested with a 'shared' parameter.Step 2: Identify correct syntax format
Among options,lock(X, shared)correctly requests a shared lock on X.Final Answer:
lock(X, shared) -> Option BQuick Check:
Shared lock syntax = lock(X, shared) [OK]
- Using exclusive lock syntax for shared lock
- Confusing function names
- Omitting lock type parameter
A. T1 requests a shared lock on A, then T2 requests an exclusive lock on A. What will happen next?Solution
Step 1: Analyze lock compatibility
Shared locks allow multiple readers, but exclusive locks require exclusive access.Step 2: Understand lock waiting rules
T2's exclusive lock request conflicts with T1's shared lock, so T2 must wait until T1 releases the lock.Final Answer:
T2 will wait until T1 releases the shared lock -> Option DQuick Check:
Exclusive lock waits for shared lock release [OK]
- Assuming exclusive lock is granted immediately
- Thinking both locks can coexist
- Believing transaction abort happens automatically
B but the system throws a deadlock error. What is the most likely cause?Solution
Step 1: Understand deadlock in locking
Deadlock occurs when two or more transactions wait indefinitely for locks held by each other.Step 2: Identify cause of deadlock error
T's exclusive lock request is blocked because another transaction holds a conflicting lock, causing deadlock.Final Answer:
T is waiting for another transaction holding a conflicting lock on B -> Option AQuick Check:
Deadlock = waiting on conflicting locks [OK]
- Confusing deadlock with syntax errors
- Assuming deadlock means missing data
- Thinking deadlock occurs if locks are released
Solution
Step 1: Understand strict two-phase locking
Strict 2PL requires holding all locks until the transaction finishes (commit or abort) to ensure serializability.Step 2: Identify lock release timing
Locks are not released during the transaction but only after it ends, preventing conflicts.Final Answer:
All locks are released only after the transaction commits or aborts -> Option CQuick Check:
Strict 2PL releases locks after commit/abort [OK]
- Releasing locks too early during transaction
- Confusing strict 2PL with basic locking
- Assuming random lock release
Ready to prove it?
Pass the test and this pattern moves unknown
