There are two basic types of blocking: Exclusive lock (x Lock) and shared lock (S-lock). The so-called X-Lock, which is the transaction T to data a plus x lock, allows only transaction T to read and modify data a,... The so-called S-lock, is the transaction T to data a plus s lock, the other transaction can only add to the data a plus s lock, and cannot add x lock, until T release S lock on A. If the transaction T has an S lock on the data object A, T can read a, but not update (S lock is therefore called a read lock), before T releases the S lock on a, other transactions can be a plus s lock, but cannot add X lock, so that can read a, but not update a.
Two-stage lock protocol:
The two-segment lock protocol for a database means that all transactions must lock and unlock data items in two stages
1. Expansion phase
Before you read or write any data item, apply and obtain a blockade of the data item.
2. Contraction phase
In each transaction, all blocking requests must precede the unlock request.
For example, transaction T follows the two-phase lock protocol, and its blocking protocol is:
BEGIN TRANSACTION;
LOCK (a); READ A; A: = a + 100; WRITE A; LOCK (B); UNLOCK (A); READ (b), UNLOCK (b),;
COMMIT;
It can be proved that any parallel scheduling policy for these parallel transactions is serializable if all concurrently executed transactions adhere to the two-phase lock protocol
It is necessary to note that if all the concurrently executed transactions comply with the two-phase lock protocol, it is only sufficient condition that the parallel scheduling strategy of these transactions can be serializable, not a requirement.
The difference between a two-phase locking protocol and a blocking method to prevent deadlocks:
A blocking method requires that a transaction must be locked at once for all data items to be used, otherwise it cannot continue running.
Obviously, a blocking protocol is consistent with two-phase locking protocols, but the two-phase lock protocol does not require that all data items need to be locked at once, so transactions that comply with the two-phase lock protocol may be deadlocked
This article is from the "a" blog, please make sure to keep this source http://lzs66.blog.51cto.com/9607068/1856025
Database S-Lock and X-Lock, two-stage lock protocol