Skip to content

Commit

Permalink
transaction-isolation-levels.md: update txn isolation (pingcap#3140)
Browse files Browse the repository at this point in the history
  • Loading branch information
cfzjywxk authored May 23, 2020
1 parent 79b9b63 commit 6aab3d3
Showing 1 changed file with 16 additions and 9 deletions.
25 changes: 16 additions & 9 deletions transaction-isolation-levels.md
Original file line number Diff line number Diff line change
Expand Up @@ -22,25 +22,27 @@ TiDB 实现了快照隔离 (Snapshot Isolation, SI) 级别的一致性。为与

> **注意:**
>
> 在 TiDB v3.0 中,事务的自动重试功能默认为禁用状态。关于该项功能对隔离级别的影响以及如何开启该项功能,请参考[事务重试](/optimistic-transaction.md#重试机制)
> 在 TiDB v3.0 中,事务的自动重试功能默认为禁用状态。不建议开启自动重试功能,因为可能导致**事务隔离级别遭到破坏**。更多关于事务自动重试的文档说明,请参考[事务重试](/optimistic-transaction.md#重试机制)
>
> 从 TiDB [v3.0.8](/releases/release-3.0.8.md#tidb) 版本开始,新创建的 TiDB 集群会默认使用[悲观事务模式](/pessimistic-transaction.md),悲观事务中的当前读(for update 读)为**不可重复读**,关于悲观事务使用注意事项,请参考[悲观事务模式](/pessimistic-transaction.md)
## 可重复读隔离级别 (Repeatable Read)

当事务隔离级别为可重复读时,只能读到该事务启动时已经提交的其他事务修改的数据,未提交的数据或在事务启动后其他事务提交的数据是不可见的。对于本事务而言,事务语句可以看到之前的语句做出的修改。

对于运行于不同节点的事务而言,不同事务启动和提交的顺序取决于从 PD 获取时间戳的顺序。

处于可重复读隔离级别的事务不能并发的更新同一行,当时事务提交时发现该行在该事务启动后,已经被另一个已提交的事务更新过,那么该事务会回滚并启动自动重试。示例如下:
处于可重复读隔离级别的事务不能并发的更新同一行,当事务提交时发现该行在该事务启动后,已经被另一个已提交的事务更新过,那么该事务会回滚。示例如下:

```sql
create table t1(id int);
insert into t1 values(0);

start transaction; | start transaction;
select * from t1; | select * from t1;
update t1 set id=id+1; | update t1 set id=id+1;
update t1 set id=id+1; | update t1 set id=id+1; -- 如果使用悲观事务,则后一个执行的 update 语句会等锁,直到持有锁的事务提交或者回滚释放行锁。
commit; |
| commit; -- 事务提交失败,回滚
| commit; -- 事务提交失败,回滚。如果使用悲观事务,可以提交成功。
```

### 与 ANSI 可重复读隔离级别的区别
Expand All @@ -53,14 +55,19 @@ MySQL 可重复读隔离级别在更新时并不检验当前版本是否可见

## 读已提交隔离级别 (Read Committed)

TiDB 仅在[悲观事务模式](/pessimistic-transaction.md)下支持读已提交隔离级别。在乐观事务模式下设置事务隔离级别为读已提交将不会生效,事务将仍旧使用可重复读隔离级别
TiDB [v4.0.0-beta](/releases/release-4.0.0-beta.md#tidb) 版本开始,TiDB 支持使用 Read Committed 隔离级别。由于历史原因,当前主流数据库的 Read Committed 隔离级别本质上都是 Oracle 定义的[一致性读隔离级别](https://docs.oracle.com/cd/B19306_01/server.102/b14220/consist.htm)。TiDB 为了适应这一历史原因,悲观事务中的 Read Committed 隔离级别的实质行为也是一致性读

由于历史原因,当前主流数据库的读已提交隔离级别本质上都是 Oracle 定义的一致性读隔离级别。TiDB 为了适应这一历史原因,悲观事务中的读已提交隔离级别的实质行为也是一致性读。
> **注意:**
>
> Read Committed 隔离级别仅在[悲观事务模式](/pessimistic-transaction.md)下生效。在[乐观事务模式](/optimistic-transaction.md)下设置事务隔离级别为 Read Committed 将不会生效,事务将仍旧使用可重复读隔离级别。
### 与 MySQL 读已提交隔离级别的区别
### 与 MySQL Read Committed 隔离级别的区别

MySQL 的读已提交隔离级别大部分符合一致性读特性,但其中存在某些特例,如半一致性读 ([semi-consistent read](https://dev.mysql.com/doc/refman/8.0/en/innodb-transaction-isolation-levels.html)),TiDB 没有兼容这个特殊行为。
MySQL 的 Read Committed 隔离级别大部分符合一致性读特性,但其中存在某些特例,如半一致性读 ([semi-consistent read](https://dev.mysql.com/doc/refman/8.0/en/innodb-transaction-isolation-levels.html)),TiDB 没有兼容这个特殊行为。

## 更多阅读

- [TiKV 的 MVCC (Multi-Version Concurrency Control) 机制](https://pingcap.com/blog-cn/mvcc-in-tikv/)
- [TiDB 的乐观事务模型](https://pingcap.com/blog-cn/best-practice-optimistic-transaction/)
- [TiDB 新特性漫谈-悲观事务](https://pingcap.com/blog-cn/pessimistic-transaction-the-new-features-of-tidb/)
- [TiDB 新特性-白话悲观锁](https://pingcap.com/blog-cn/tidb-4.0-pessimistic-lock/)
- [TiKV 的 MVCC (Multi-Version Concurrency Control) 机制](https://pingcap.com/blog-cn/mvcc-in-tikv/)

0 comments on commit 6aab3d3

Please sign in to comment.