Locks are an integral part of maintaining concurrency control in a DBMS. Transactions in any system that implements lock-based concurrency control cannot read or write statements until they acquire the required locks.
There are two types of locks in lock-based protocols. They are:
The different locking protocols are -
A transaction acquires a lock on a data value before performing a write operation. After the write operation is completed, the lock can be released. An example of a simple locking protocol is:
T1 | T2 |
---|---|
R (A) | |
# R(A) | |
Lock (C) |
|
R(C) | |
##W(C) | |
Unlock(C) | Submit |
|
|
Commit |
R(A) tr> | |
##S(A) | |
X(B) | |
##R(B) | |
W(B) | |
##X(C) | |
##R(C) | |
W(C) | |
Unlock (C) | |
Unlock (A) | |
Unlock (B) | |
Unlock (A) | |
Submit | |
In the above example, T1 and T2 share the lock using the shared variable A because only read operations are performed on A. T1 acquires the exclusive lock on B for the write operation and releases it soon after. T2 does the same thing as C. Strict two-phase locking protocol |
The strict two-phase locking protocol is similar to the two-phase locking protocol. The only difference is that in a strict 2PL protocol, all exclusive locks acquired by the protocol need to be retained until the protocol commits or aborts. An example of a strict two-phase locking protocol is: |
T1
tr> | ||||||||||||||||||||||||||||||||||
---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
##S(A) | ||||||||||||||||||||||||||||||||||
##R(A) | X(B) | |||||||||||||||||||||||||||||||||
##R(B) | ||||||||||||||||||||||||||||||||||
W(B) |
||||||||||||||||||||||||||||||||||
##Unlock (A) | ||||||||||||||||||||||||||||||||||
Unlock(A) | ||||||||||||||||||||||||||||||||||
##Submit | ||||||||||||||||||||||||||||||||||
Unlock (B) | ||||||||||||||||||||||||||||||||||
Submit |
||||||||||||||||||||||||||||||||||
Unlock(C) |
||||||||||||||||||||||||||||||||||
In the above example, T1 and T2 use a shared lock to share variable A, because only read operations are performed on A. T1 acquires an exclusive lock on B for write operations, and T2 acquires an exclusive lock on C. The exclusive lock is released only after the transaction commits. However, shared locks have no such restrictions. Strict two-phase locking protocolThe strict two-phase locking protocol is just an extension of the two-phase locking protocol and the strict two-phase locking protocol. Lock protocol. Here, all locks held by a transaction, whether shared or exclusive, are released only when the transaction commits or aborts. An example of a strict two-phase locking protocol is:
|
The above is the detailed content of Using locks for concurrency control in DBMS. For more information, please follow other related articles on the PHP Chinese website!