溫馨提示×

溫馨提示×

您好,登錄后才能下訂單哦!

密碼登錄×
登錄注冊×
其他方式登錄
點擊 登錄注冊 即表示同意《億速云用戶服務(wù)條款》

MySQL共享鎖和排他鎖的介紹

發(fā)布時間:2021-09-16 09:04:42 來源:億速云 閱讀:134 作者:chen 欄目:大數(shù)據(jù)

這篇文章主要講解了“MySQL共享鎖和排他鎖的介紹”,文中的講解內(nèi)容簡單清晰,易于學(xué)習(xí)與理解,下面請大家跟著小編的思路慢慢深入,一起來研究和學(xué)習(xí)“MySQL共享鎖和排他鎖的介紹”吧!

共享鎖和排他鎖

InnoDB implements standard row-level locking where there are two types of locks, shared (S) locks and exclusive (X) locks.

InnoDB實現(xiàn)了兩種標(biāo)準(zhǔn)的 行級鎖 ,分別是 共享鎖(S鎖) 和 排他鎖(X鎖):

  • A shared (S) lock permits the transaction that holds the lock to read a row. 共享鎖(S鎖)允許(當(dāng)前)持有這個鎖的事務(wù)去讀這個行。

  • An exclusive (X) lock permits the transaction that holds the lock to update or delete a row. 排他鎖(X鎖)允許(當(dāng)前)持有這個鎖的事務(wù)去更新或刪除這個行。

If transaction T1 holds a shared (S) lock on row r, then requests from some distinct transaction T2 for a lock on row r are handled as follows:

  • A request by T2 for an S lock can be granted immediately. As a result, both T1 and T2 hold an S lock on r.

  • A request by T2 for an X lock cannot be granted immediately.

如果事務(wù)T1帶有一個在r行記錄上的共享鎖,那么另一個事務(wù)T2在請求對r行的鎖時,會被處理如下:

  • T2請求共享鎖將會被立刻放行。這樣,T1和T2事務(wù)都有r行的共享鎖。

  • T2請求排他鎖,將不會被立即放行。

If a transaction T1 holds an exclusive (X) lock on row r, a request from some distinct transaction T2 for a lock of either type on rcannot be granted immediately. Instead, transaction T2 has to wait for transaction T1 to release its lock on row r.

如果事務(wù)T1在r行上帶有排他鎖,那么另一個事務(wù)T2不管請求對r行的哪種鎖,都不會被立即放行。相反,事務(wù)T2必須等待事務(wù)T1釋放在r行上的鎖。

排他鎖和共享鎖的兼容性如下

 XS
XConflictConflict
 SConflictCompatible
Intention Locks 意向鎖

InnoDB supports multiple granularity locking which permits coexistence of row locks and table locks. For example, a statement such asLOCK TABLES ... WRITE takes an exclusive lock (an X lock) on the specified table. To make locking at multiple granularity levels practical, InnoDB uses intention locks. Intention locks are table-level locks that indicate which type of lock (shared or exclusive) a transaction requires later for a row in a table. There are two types of intention locks:

  • An intention shared lock (IS) indicates that a transaction intends to set a shared lock on individual rows in a table.

  • An intention exclusive lock (IX) indicates that a transaction intends to set an exclusive lock on individual rows in a table.

InnoDB 支持 多粒度鎖,它允許 行鎖 和 表鎖的同時存在。例如,‘LOCK TABLES ... WRITE’ 聲明語句 給明確的表加上一個排他鎖(X鎖)。為了鎖在多粒度級別的實踐,InnoDB 使用 意向鎖。意向鎖是表級鎖,它表明一個事務(wù)在(申請表級鎖之后)接下來對表中的某一行請求何種類型的鎖(共享還是排他)。意向鎖有兩種:

  • 意向共享鎖(IS鎖) 表明一個事務(wù)打算(在獲得表鎖之后)對表中的個別行設(shè)置一個共享鎖(S鎖)。

  • 意向排他鎖(IX鎖) 表明一個事務(wù)打算(在獲得表鎖之后)對表中的個別行設(shè)置一個排他鎖(X鎖)。

For example, SELECT ... FOR SHARE sets an IS lock, and SELECT ... FOR UPDATE sets an IX lock.

例如,SELECT ... FOR SHARE 會設(shè)置一個 意向共享鎖(IS鎖),SELECT ... FOR UPDATE 會設(shè)置一個意向排他鎖。

The intention locking protocol is as follows:

  • Before a transaction can acquire a shared lock on a row in a table, it must first acquire an IS lock or stronger on the table.

  • Before a transaction can acquire an exclusive lock on a row in a table, it must first acquire an IX lock on the table.

意向鎖的協(xié)議如下:

  • 在一個事務(wù)獲取表中某行的共享鎖之前,它必須首先獲取這個表的一個意向共享鎖或者一個更強(qiáng)的鎖。

  • 在一個事務(wù)獲取表中某行的排他鎖之前,它必須首先獲取這個表的一個意向排他鎖。

Table-level lock type compatibility is summarized in the following matrix.

表級鎖的兼容性總結(jié)如下:

 XIXSIS
XConflictConflictConflictConflict
IXConflictCompatibleConflictCompatible
SConflictConflictCompatibleCompatible
ISConflictCompatibleCompatibleCompatible

A lock is granted to a requesting transaction if it is compatible with existing locks, but not if it conflicts with existing locks. A transaction waits until the conflicting existing lock is released. If a lock request conflicts with an existing lock and cannot be granted because it would cause deadlock, an error occurs.

如果某個事務(wù)請求鎖,而這個鎖與已存在的鎖是兼容的,那么這個鎖將會被放行。但是如果這個鎖與已有的鎖沖突,則不會被放行。事務(wù)將會等待,直到那個已存在的引起沖突的鎖被釋放。如果一個鎖請求和一個已存在的鎖發(fā)生沖突,且不會被放行,因為如果放行,會造成死鎖,會發(fā)生錯誤。

Intention locks do not block anything except full table requests (for example, LOCK TABLES ... WRITE). The main purpose of intention locks is to show that someone is locking a row, or going to lock a row in the table.

意向鎖不會阻塞任何東西,除了全表掃描(比如 LOCK TABLES ... WRITE)。意向鎖的主要目的是為了展示有人正在鎖住表中的某一行,或者將要鎖住那一行。

Transaction data for an intention lock appears similar to the following in SHOW ENGINE INNODB STATUS and InnoDB monitor output:

TABLE LOCK table `test`.`t` trx id 10080 lock mode IX
備注:在執(zhí)行層面,當(dāng)MySQL-Server向InnoDB提交一個SQL語句時,意向鎖是InnoDB自己去實現(xiàn)的,該語句最終會獲得對應(yīng)行的鎖都是S鎖或者X鎖。

感謝各位的閱讀,以上就是“MySQL共享鎖和排他鎖的介紹”的內(nèi)容了,經(jīng)過本文的學(xué)習(xí)后,相信大家對MySQL共享鎖和排他鎖的介紹這一問題有了更深刻的體會,具體使用情況還需要大家實踐驗證。這里是億速云,小編將為大家推送更多相關(guān)知識點的文章,歡迎關(guān)注!

向AI問一下細(xì)節(jié)

免責(zé)聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點不代表本網(wǎng)站立場,如果涉及侵權(quán)請聯(lián)系站長郵箱:is@yisu.com進(jìn)行舉報,并提供相關(guān)證據(jù),一經(jīng)查實,將立刻刪除涉嫌侵權(quán)內(nèi)容。

AI