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