您好,登錄后才能下訂單哦!
這篇文章主要講解了“MySQL共享鎖和排他鎖的介紹”,文中的講解內容簡單清晰,易于學習與理解,下面請大家跟著小編的思路慢慢深入,一起來研究和學習“MySQL共享鎖和排他鎖的介紹”吧!
InnoDB
implements standard row-level locking where there are two types of locks, shared (S
) locks and exclusive (X
) locks.
InnoDB實現了兩種標準的 行級鎖 ,分別是 共享鎖(S鎖) 和 排他鎖(X鎖):
A shared (S
) lock permits the transaction that holds the lock to read a row. 共享鎖(S鎖)允許(當前)持有這個鎖的事務去讀這個行。
An exclusive (X
) lock permits the transaction that holds the lock to update or delete a row. 排他鎖(X鎖)允許(當前)持有這個鎖的事務去更新或刪除這個行。
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.
如果事務T1帶有一個在r行記錄上的共享鎖,那么另一個事務T2在請求對r行的鎖時,會被處理如下:
T2請求共享鎖將會被立刻放行。這樣,T1和T2事務都有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 r
cannot be granted immediately. Instead, transaction T2
has to wait for transaction T1
to release its lock on row r
.
如果事務T1在r行上帶有排他鎖,那么另一個事務T2不管請求對r行的哪種鎖,都不會被立即放行。相反,事務T2必須等待事務T1釋放在r行上的鎖。
排他鎖和共享鎖的兼容性如下
X | S | |
---|---|---|
X | Conflict | Conflict |
S | Conflict | Compatible |
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 使用 意向鎖。意向鎖是表級鎖,它表明一個事務在(申請表級鎖之后)接下來對表中的某一行請求何種類型的鎖(共享還是排他)。意向鎖有兩種:
意向共享鎖(IS鎖) 表明一個事務打算(在獲得表鎖之后)對表中的個別行設置一個共享鎖(S鎖)。
意向排他鎖(IX鎖) 表明一個事務打算(在獲得表鎖之后)對表中的個別行設置一個排他鎖(X鎖)。
For example, SELECT ... FOR SHARE
sets an IS
lock, and SELECT ... FOR UPDATE
sets an IX
lock.
例如,SELECT ... FOR SHARE 會設置一個 意向共享鎖(IS鎖),SELECT ... FOR UPDATE 會設置一個意向排他鎖。
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.
意向鎖的協議如下:
在一個事務獲取表中某行的共享鎖之前,它必須首先獲取這個表的一個意向共享鎖或者一個更強的鎖。
在一個事務獲取表中某行的排他鎖之前,它必須首先獲取這個表的一個意向排他鎖。
Table-level lock type compatibility is summarized in the following matrix.
表級鎖的兼容性總結如下:
X | IX | S | IS | |
---|---|---|---|---|
X | Conflict | Conflict | Conflict | Conflict |
IX | Conflict | Compatible | Conflict | Compatible |
S | Conflict | Conflict | Compatible | Compatible |
IS | Conflict | Compatible | Compatible | Compatible |
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.
如果某個事務請求鎖,而這個鎖與已存在的鎖是兼容的,那么這個鎖將會被放行。但是如果這個鎖與已有的鎖沖突,則不會被放行。事務將會等待,直到那個已存在的引起沖突的鎖被釋放。如果一個鎖請求和一個已存在的鎖發生沖突,且不會被放行,因為如果放行,會造成死鎖,會發生錯誤。
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
感謝各位的閱讀,以上就是“MySQL共享鎖和排他鎖的介紹”的內容了,經過本文的學習后,相信大家對MySQL共享鎖和排他鎖的介紹這一問題有了更深刻的體會,具體使用情況還需要大家實踐驗證。這里是億速云,小編將為大家推送更多相關知識點的文章,歡迎關注!
免責聲明:本站發布的內容(圖片、視頻和文字)以原創、轉載和分享為主,文章觀點不代表本網站立場,如果涉及侵權請聯系站長郵箱:is@yisu.com進行舉報,并提供相關證據,一經查實,將立刻刪除涉嫌侵權內容。