事务隔离级别

概念

事务的隔离性是指并发事务之间的隔离程度,隔离程度越高,数据可见性越底,能够支持的并发程度越低。

隔离级别

  • READ UNCOMMITTED;
  • READ COMMITTED;
  • REPEATABLE READ(默认);
  • SERIALIZABLE;

READ UNCOMMITTED

定义:事务A能够读取到事务B未提交的数据;
问题:脏读。如果事务B发生回滚,则事务A读取到的数据是错误或者不存在的。

READ COMMITTED

定义:事务A能够读取到事务B已提交的数据;
问题:不可重复读。事务A执行过程中,如果有其它事务提交了写操作,那么事务A内的多次查询记过可能不一致。
实现原理: 锁+MVCC,每次查询数据时,读取最新事务版本的数据。

Each consistent read, even within the same transaction, sets and reads its own fresh snapshot.

REPEATABLE READ

定义:事务A执行过程中,多次查询的结果一致;
问题:幻读。事务A执行过程中,如果有其它事务提交了写操作,那么事务A不感知,比如事务B插入新数据。

Suppose that you are running in the default REPEATABLE READ isolation level. When you issue a consistent read (that is, an ordinary SELECT statement), InnoDB gives your transaction a timepoint according to which your query sees the database. If another transaction deletes a row and commits after your timepoint was assigned, you do not see the row as having been deleted. Inserts and updates are treated similarly.

实现原理: 锁+MVCC,每次查询数据时,只能读取小于等于当前事务版本的数据。

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.

SERIALIZABLE

定义:并发事务串行化执行;
问题:性能低下,一般不使用。

参考:

  1. https://dev.mysql.com/doc/refman/5.7/en/innodb-transaction-isolation-levels.html
  2. https://dev.mysql.com/doc/refman/5.7/en/innodb-consistent-read.html
  3. https://dev.mysql.com/doc/refman/5.7/en/glossary.html#glos_consistent_read;

猜你喜欢

转载自blog.csdn.net/yangguosb/article/details/79948019