Sub topic 9
Shared and Exclusive Locks and the Compatibility Matrix
This chapter assumes you know basic SQL, transactions, and schedules. You should also recognize a directed graph and understand that a transaction groups database operations into one logical unit.
A lock is a control held by a transaction before it accesses a database item. A lock manager checks whether the requested lock is compatible with locks already held by other transactions. If it is not compatible, the requester waits or aborts.
Shared and exclusive locks
A shared lock, written \(S\), permits reading. Several transactions may hold shared locks on the same item at the same time.
An exclusive lock, written \(X\), permits both reading and writing. Only one transaction may hold an exclusive lock on an item, and no other transaction may hold a shared lock on it.
The usual rules are:
- Before reading item \(A\), a transaction requests \(S(A)\).
- Before writing item \(A\), a transaction requests \(X(A)\).
- A transaction holding \(S(A)\) may need to upgrade it to \(X(A)\) before writing.
- A transaction holding \(X(A)\) can read or write \(A\).
- Locks normally remain until the transaction releases them according to its concurrency-control protocol.
The compatibility matrix
The matrix compares a new request with a lock already held by another transaction.
| Existing lock on A | Requested S(A) | Requested X(A) |
|---|---|---|
| S(A) | Yes | No |
| X(A) | No | No |
Two shared locks are compatible because both transactions only read. An exclusive lock conflicts with every other lock on the same item.
Compatibility does not mean that the request is immediately granted. The transaction might still wait because of a policy such as two-phase locking. Compatibility only answers whether the two locks can coexist.
Applying locks to a schedule
Consider two transactions that update the same account:
-- Transaction T1
BEGIN;
SELECT balance FROM account WHERE id = 1 FOR UPDATE;
UPDATE account
SET balance = balance - 100
WHERE id = 1;
COMMIT;
-- Transaction T2, running at the same time
BEGIN;
SELECT balance FROM account WHERE id = 1 FOR UPDATE;
UPDATE account
SET balance = balance - 50
WHERE id = 1;
COMMIT;FOR UPDATE requests an exclusive row lock in many SQL systems. If T1 obtains the lock first, T2 cannot obtain its lock immediately. T2 waits until T1 commits, then reads the committed value and performs its update. This prevents both transactions from reading the same old balance and overwriting each other’s result.
A read-only transaction can use a shared lock:
BEGIN;
SELECT balance FROM account
WHERE id = 1
FOR SHARE;
COMMIT;While this shared lock exists, another transaction cannot obtain an exclusive lock on that row. The exact SQL syntax and lock behavior vary by database system, so production code must follow that system’s documentation.
Lock conversion
A transaction may first acquire \(S(A)\), read \(A\), and later request an upgrade to \(X(A)\). The upgrade is granted only when no other transaction holds \(S(A)\). If two transactions both hold \(S(A)\) and both request upgrades, each may wait for the other. This is a common source of deadlock.
Lock compatibility is therefore a local rule, not a complete correctness proof. The database also needs a protocol governing when locks may be acquired and released. Two-phase locking provides that broader protocol.
Exercise
T1 holds S(A), and T2 requests X(A). Is T2's request compatible, and what normally happens?
Show answer
The request is not compatible. T2 must wait or be aborted until T1 releases its shared lock.