Documente Academic
Documente Profesional
Documente Cultură
Concurrency Control
s-lock x-lock
s-lock
x-lock
read(A)
connecting lock points)
read(B)
lock
write(B)
unlock(B)
s-lock(B)
unlock(A)
unlock
read(B)
unlock(B)
DBMS: Concurrency Control 10 Dr. Kriengkrai Porkaew
2PL vs. Recoverability
T1 T2
x-lock(A) • 2-Phase Locking Protocol
read(A) – does not guarantee recoverable
write(A) schedules
x-lock(B) – because transactions (i.e., T1)
unlock(A) may release locks on modified
s-lock(A) data items (i.e., A and B)
before it commits or aborts
read(A)
– which allows other transactions
read(B)
(i.e., T2) to use the unstable values
write(B) (not committed yet) and
unlock(B) commits beforehand.
s-lock(B) – If it (T1) eventually aborts,
unlock(A) transactions (T2) that read its
read(B)
unstable values must abort too
even though it has already
unlock(B)
committed.
commit
abort
DBMS: Concurrency Control 11 Dr. Kriengkrai Porkaew
Strict 2PL and Rigorous 2PL
Strict 2PL • Strict 2PL Rigorous 2PL
T1 T2 – Same as 2PL but T1 T2
s-lock(A) – Hold all exclusive locks s-lock(A)
read(A) until the transaction has read(A)
already successfully
s-lock(A) committed or aborted. s-lock(A)
x-lock(B) – It guarantees cascadeless x-lock(B)
unlock(A) recoverability. read(A)
read(B) • Rigorous 2PL read(B)
write(B) – Same Strict 2PL but write(B)
– Hold all locks until the
read(A) commit
transaction has already
unlock(A) successfully committed unlock(B)
commit or aborted. s-lock(B)
unlock(B) – It is used in dynamic
read(B)
environments where data
s-lock(B) access patterns are not unlock(A)
read(B) known beforehand. commit
unlock(B) unlock(A)
commit unlock(B)
DBMS: Concurrency Control 12 Dr. Kriengkrai Porkaew
Lock Conversion
• Lock Conversion
– Each transaction only acquires the right lock mode at the right time.
• Transactions acquire an s-lock before a read operation.
• Transactions acquire an x-lock before a write operation.
• Each transaction acquires an s-lock on the data item that it needs to read.
Later on, it can upgrade the s-lock to an x-lock when it needs to update
that data item (as long as there is no lock conflict with other transactions).
– Similarly, transactions may downgrade an x-lock to an s-lock.
– Lock conversion allows higher concurrency.
• Lock Conversion and Strict/Rigorous 2PL
– Lock upgrade (x2s-lock) is considered as acquiring an x-lock,
which is a part of the lock growing phase.
– Lock downgrade (s2x-lock) is considered as releasing an x-lock
(but still holding an s-lock), which is a part of the lock shrinking phase.
– Note: for recoverable schedules, lock downgrade is meaningless
because locks can only be downgraded (i.e., releasing an x-lock)
after transactions have already committed or aborted;
where read/write operations are not allowed.
DBMS: Concurrency Control 13 Dr. Kriengkrai Porkaew
Automatic Lock Acquisition
• Automatic Lock Acquisition
– In many dominate database products,
locks are acquired automatically and implicitly
to ensure correctness of the databases.
– Transactions’ read/write operations are
preceded by the right lock acquisition
controlled by the DBMSs.
– Locks are upgraded as needed.
– Locks are released after transactions have
already committed or aborted successfully
to ensure cascadeless recoverability.
T3
DBMS: Concurrency Control 17 Dr. Kriengkrai Porkaew
Livelock and Prevention
• Livelock
T1 T2 T3 T4 – T2 cannot get an x-lock on A
s-lock(A) because T1 holds an s-lock on A.
wait for – T3 and T4 can get s-locks on A
x-lock(A) because they are compatible with
the s-lock on A that T1 holds.
read(A)
– There is a chance that T2 will
s-lock(A)
never get the x-lock it requests.
unlock(A)
• Prevention
s-lock(A)
– Do not allow any transaction to
x-lock(B) get a lock on the data item it
read(A) requests if that lock request is
unlock(A) incompatible with one of the
read(A) previous lock requests.
read(B) – Implementation: for each data
item, there is a lock queue to
…
guarantee fairness on data
access.
DBMS: Concurrency Control 18 Dr. Kriengkrai Porkaew
Lock Upgrade and Deadlock
• Deadlock due to
T1 T2 a Lock Upgrade
s-lock(A) – In a situation where two
read(A) transactions share an access
to the same data item through
s-lock(A)
s-locks, if both of them want to
read(A)
upgrade their s-locks to x-locks,
wait for
s2x-lock(A) none of them will be able to
wait for proceed because each transaction
s2x-lock(A) will be blocked by the other
No one can proceed. transaction due to a lock conflict.
T1 T2
(a) and (b) but no directed cycle. (a) and (b) with a directed cycle.
So, no deadlock. So, a deadlock.
T1 a T2 T1 T2
a b
b b
T4 T3 T4 b T3
a
IS
IX
SIX
X
DBMS: Concurrency Control 36 Dr. Kriengkrai Porkaew
Intention Locks for Multiple Granules
• Locking Order
– Locks are acquired from coarse granules to fine granules.
– Locks are released from fine granules to coarse granules.
• Locking
– to lock a granule in an s-lock mode:
• do an is-lock on each of the granule until the desired granule is reached.
• Lock the desired granule in the s-lock mode.
• E.g., to read a row, acquire an is-lock on the table and the page
containing the row, and acquire an s-lock on the row.
– to lock a granule in an x-lock mode:
• do an ix-lock on each of the granule until the desired granule is reached.
• Lock the desired granule in the x-lock mode.
• E.g., to write a row, acquire an x-lock on the table and the page
containing the row, and acquire an x-lock on the row.
• E.g., in case that the table must be read to compute something before
updating a row in the table, so do an six-lock on the table (to read the
table and to modify a part of the table), then an ix-lock on the page, and
then an x-lock on the row.