MySQL共享锁和排他锁的介绍

发布时间:2021-09-16 09:04:42 作者:chen
来源:亿速云 阅读:130

这篇文章主要讲解了“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锁):

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:

如果事务T1带有一个在r行记录上的共享锁,那么另一个事务T2在请求对r行的锁时,会被处理如下:

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.

如果事务T1在r行上带有排他锁,那么另一个事务T2不管请求对r行的哪种锁,都不会被立即放行。相反,事务T2必须等待事务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:

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:

意向锁的协议如下:

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

表级锁的兼容性总结如下:

 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.

如果某个事务请求锁,而这个锁与已存在的锁是兼容的,那么这个锁将会被放行。但是如果这个锁与已有的锁冲突,则不会被放行。事务将会等待,直到那个已存在的引起冲突的锁被释放。如果一个锁请求和一个已存在的锁发生冲突,且不会被放行,因为如果放行,会造成死锁,会发生错误。

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-Server向InnoDB提交一个SQL语句时,意向锁是InnoDB自己去实现的,该语句最终会获得对应行的锁都是S锁或者X锁。

感谢各位的阅读,以上就是“MySQL共享锁和排他锁的介绍”的内容了,经过本文的学习后,相信大家对MySQL共享锁和排他锁的介绍这一问题有了更深刻的体会,具体使用情况还需要大家实践验证。这里是亿速云,小编将为大家推送更多相关知识点的文章,欢迎关注!

推荐阅读:
  1. mysql的共享锁与排他锁的区别和使用方法
  2. mysql共享锁与排他锁用法实例分析

免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:is@yisu.com进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。

mysql

上一篇:JDBC连接数据库mysql serverTimezone useSSL报错问题解析

下一篇:MYSQL的字符串函数具体用法

相关阅读

您好,登录后才能下订单哦!

密码登录
登录注册
其他方式登录
点击 登录注册 即表示同意《亿速云用户服务条款》