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

知识点

  • mysql使用innodb引擎,innodb支持行锁,行锁实际上是加在索引上的,所以一个表如果没有配置索引,那行锁会退化成表锁
  • 事物A更新某一行数据时加排他锁,事物B读取该行数据时实际读取到了修改之前的数据,是MVCC(多版本并发控制)的作用
  • 一致性非阻塞读
    Consistent Nonblocking Reads
    https://dev.mysql.com/doc/refman/5.7/en/innodb-consistent-read.html
  • Transaction Isolation Levels
    https://dev.mysql.com/doc/refman/5.7/en/innodb-transaction-isolation-levels.html

事物

数据特性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

你可能感兴趣的:(mysql innodb事物隔离级别 (脏读-read committed / 不可重复读-Repeatable Read / 幻读-间隙锁))