InnoDB
implements standard row-level locking where there are two types of locks, shared (S
) locks and exclusive (X
) locks.html
InnoDB實現了兩種標準的 行級鎖 ,分別是 共享鎖(S鎖) 和 排他鎖(X鎖):mysql
A shared (S
) lock permits the transaction that holds the lock to read a row. 共享鎖(S鎖)容許(當前)持有這個鎖的事務去讀這個行。sql
An exclusive (X
) lock permits the transaction that holds the lock to update or delete a row. 排他鎖(X鎖)容許(當前)持有這個鎖的事務去更新或刪除這個行。app
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:ui
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
.spa
A request by T2
for an X
lock cannot be granted immediately.code
若是事務T1帶有一個在r行記錄上的共享鎖,那麼另外一個事務T2在請求對r行的鎖時,會被處理以下:htm
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行上的鎖。ip
排他鎖和共享鎖的兼容性以下
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 使用 意向鎖。意向鎖是表級鎖,它代表一個事務在(申請表級鎖以後)接下來對錶中的某一行請求何種類型的鎖(共享仍是排他)。意向鎖有兩種:
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