Database S-Lock and X-Lock, two-stage lock protocol

Source: Internet
Author: User

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

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.