Shared lock (S lock) and exclusive lock (x Lock)

Source: Internet
Author: User

Shared lock "s lock"
Also called a read lock, if the transaction T with the data object a plus s lock, then the transaction T can read a but cannot modify a, the other transaction can only a plus s lock, and cannot add x lock, until T release S lock on A. This ensures that other transactions can read a, but cannot make any modifications to a before T releases the S lock on a. A shared lock causes a locked object to be shared, which repels an exclusive lock, so that other sessions cannot monopolize the resource's modification ("modify" will first add an exclusive lock to the modified object). However, other shared locks are not excluded, so an object can be combined with a shared lock by multiple sessions.

Exclusive lock "x lock"
Also known as write lock. If the transaction t has an X lock on the data object A, the transaction T can read a or modify a, and the other transaction cannot add any locks to a, until T releases the lock on A. This ensures that the other transaction cannot read and modify a until the lock on the A is released by T. An exclusive lock is well understood and is an exclusive resource. Other sessions are not allowed to share or exclusive locks on the same object. However, other sessions can be read, which also proves that ordinary reading is not locked. If the object being checked is modified, the query operation will be redirected to the corresponding undo block (original data) to ensure the transaction is isolated and read uniformly;

Locks are associated with transactions, and the life cycle of a lock is the start of the transaction to the end of the transaction (either commit or rollback is the end)

Here are some examples:
A row of data in a table is modified by a DML statement, and a transaction begins with the steps behind it:
1. Add a shared lock to this table. This is done to prevent other sessions from modifying the table structure of this table through DDL statements. DDL statements to modify this table, you must add an exclusive lock to the table. But now to add a shared lock on the table, it also excludes the DDL to add exclusive lock;
2. Add an exclusive lock to the modified line, and the other session cannot modify the line. But I add a shared lock to the whole table instead of an exclusive lock, so other sessions can still modify the other rows (also go through 1, 22 steps)

There are two points to add:
1. The meaning of locks is to protect objects, so they have a common purpose, which is not to allow other sessions to modify my lock object itself. is the table structure for a table, which is the data for each field for the row.

2. The locked object can be a table or a row inside the table (the entire row, the exclusive lock on the row, all the data in the field are not allowed to move).

Reference:

Http://www.itpub.net/thread-1829284-1-1.html

Shared lock (S lock) and exclusive lock (x Lock)

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.