mysql innodb事物隔离级别 (脏读-read committed / 不可重复读-Repeatable Read / 幻读-间隙锁)

知识点

事物

数据特性ACID

  • atomicity
    原子性:undo log实现
  • consistency
    一致性:数据的状态始终是有效的(有意义的)

>>> mysql官网解释 https://dev.mysql.com/doc/refman/5.7/en/glossary.html#glos_acid

The database remains in a consistent state at all times — after each commit or rollback, and while transactions are in progress. If related data is being updated across multiple tables, queries see either all old values or all new values, not a mix of old and new values.

  • isolation
    隔离性:锁实现
  • durability
    持久性:redo log实现

问题

  • 脏读
    事物A中读取到事物B中未提交修改的数据
  • 不可重复读
    事物A中读取到事物B提交前和提交后修改的数据,结果不一致
  • 幻读
    事物A过程中读不到事物B提交修改的数据,事物A commit后可以读取到

事物隔离级别

事物隔离级别 - 本质上是描述多个事物同时进行时 数据可见性 的问题,通过锁 + MVCC实现

  • Read Uncommitted
    未提交读
  • Read Committed
    提交读 - 解决脏读问题
  • Repeatable Read
    可重复读 - 解决不可重复读的问题
  • Serializable
    串行
幻读怎么解决

>>> https://dev.mysql.com/doc/refman/5.7/en/innodb-transaction-isolation-levels.html

This is the default isolation level for InnoDB. Consistent reads within the same transaction read the snapshot established by the first read. This means that if you issue several plain (nonlocking) SELECT statements within the same transaction, these SELECT statements are consistent also with respect to each other. See Section 14.7.2.3, “Consistent Nonlocking Reads”.
For locking reads (SELECT with FOR UPDATE or LOCK IN SHARE MODE), UPDATE, and DELETE statements, locking depends on whether the statement uses a unique index with a unique search condition, or a range-type search condition.

  • For a unique index with a unique search condition, InnoDB locks only the index record found, not the gap before it.
  • For other search conditions, InnoDB locks the index range scanned, using gap locks or next-key locks to block insertions by other sessions into the gaps covered by the range. For information about gap locks and next-key locks, see Section 14.7.1, “InnoDB Locking”.

图解

(待补充)

参考资料
https://blog.csdn.net/xiaozhuanddapang/article/details/53185775
https://www.cnblogs.com/zhoujinyi/p/3437475.html
https://www.imooc.com/article/17290
https://blog.csdn.net/huyangyamin/article/details/46848875

最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。