MySQL事务隔离级别

事务的基本要素(ACID)

1. 原子性(Atomic)

组成一个事务的多个数据库操作是一个不可分割的原子单元;只有所有操作执行成功,整个事务才提交,其中一个操作失败,都必须回滚到初始状态。

2. 一致性(Consistency)

事务操作成功后数据库所处的状态和它的业务规则是一致的;(即数据总额不会被破坏。如A账户转账100到B账户,无
论操作成功与否,A和B的存款总额是不变的)

3. 隔离性(Isolation)

在并发数据操作时,不同的事务拥有各自的数据空间,它们的操作不会对彼此产生干扰。(并非是完全无干扰,根据数据库
的隔离级别,会产生不同程度的干扰)

4. 持久性(Durability)

一旦事务提交成功,事务中的数据操作都必须持久化到数据库中;就算数据库崩溃,也必须保证有某种机制恢复。

事务的并发问题

https://dev.mysql.com/doc/refman/5.7/en/glossary.html

1. 脏读(dirty read)

事务A读取到事务B未提交的更新新据。当事务B正在访问数据,并且对数据进行了修改,而这种修改还没有提交到数据库中,这时事务A也访问这个数据,然后使用了这个数据。
因为这个数据是还没有提交的数据, 那么事务A读到的这个数据是脏数据,依据脏数据所做的操作也可能是不正确的。

2. 不可重复读(non-repeatable read)

The situation when a query retrieves data, and a later query within the same transaction retrieves what should be the same data, but the queries return different results (changed
by another transaction committing in the meantime).
事务A多次读取同一数据,事务B在事务A多次读取的过程中对数据作了更新并提交,导致事务A多次读取同一数据的结果不一致。

3. 幻读(phantom read)

事务A执行一次查询,然后事务B新插入一行记录,这行记录恰好可以满足A所使用的查询的条件。然后 A又使用相同的查询再次对表进行检索,但此时却看到了事务B刚才插入的
新行。这个新行就称为“幻像”,因为对A来说这一行就像突然出现的一样。

The so-called phantom problem occurs within a transaction when the same query produces different sets of rows at different times. For example, if a SELECT is executed twice,
but returns a row the second time that was not returned the first time, the row is a “phantom” row.

https://dev.mysql.com/doc/refman/5.7/en/innodb-next-key-locking.html

小结:不可重复读的和幻读很容易混淆,不可重复读侧重于修改,幻读侧重于新增或删除。解决不可重复读的问题只需锁住满
足条件的行,解决幻读需要锁表。

事务隔离级别

ISOLATION_DEFAULT是PlatfromTransactionManager默认的隔离级别,使用数据库默认的事务隔离级别。每种数据库的默认隔离级别是不同的,例如SQL Server、Oracle默认
Read Commited,MySQL默认Repeatable Read。另外四个与JDBC的隔离级别相对应,不同的隔离级别采用不同的锁类型来实现,在四种隔离级别中,Serializable的隔离级别最高,
Read Uncommited的隔离级别最低。

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

1. READ UNCOMMITTED(读取未提交)

读未提交数据,这是事务最低的隔离级别,在并发的事务中,它充许一个事务可以读到另一个事务未提交的更新数据。

2. READ COMMITTED(读已提交)

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

For locking reads (SELECT with FOR UPDATE or LOCK IN SHARE MODE), UPDATE statements, and DELETE statements, InnoDB locks only index records, not the gaps before them, and
thus permits the free insertion of new records next to locked records. Gap locking is only used for foreign-key constraint checking and duplicate-key checking.

Because gap locking is disabled, phantom row problems may occur, as other sessions can insert new rows into the gaps.

3. REPEATABLE READ(可重复读)

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. 

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. 

4. SERIALIZABLE(串行化)

事务被处理为顺序执行。这是花费最高,但也是最可靠的事务隔离级别。

MYSQL查看当前事务隔离级别

select  @@tx_isolation,@@global.tx_isolation;

 

posted @ 2018-12-07 11:06  景岳  阅读(9104)  评论(0编辑  收藏  举报