Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

transaction-isolation-levels.md: update txn isolation #3140

Merged
merged 7 commits into from
May 23, 2020
Merged
Changes from 4 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
20 changes: 14 additions & 6 deletions transaction-isolation-levels.md
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,9 @@ TiDB 实现了快照隔离 (Snapshot Isolation, SI) 级别的一致性。为与

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

## 可重复读隔离级别 (Repeatable Read)

Expand All @@ -38,11 +40,12 @@ insert into t1 values(0);

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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

change to

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

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 语句会等锁,直到持有锁事务提交或者回滚释放行锁
yikeke marked this conversation as resolved.
Show resolved Hide resolved
commit; |
| commit; -- 事务提交失败,回滚
| commit; -- 事务提交失败,回滚。如果使用悲观事务,可以提交成功
yikeke marked this conversation as resolved.
Show resolved Hide resolved
```


Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change

To fix CI

### 与 ANSI 可重复读隔离级别的区别

尽管名称是可重复读隔离级别,但是 TiDB 中可重复读隔离级别和 ANSI 可重复隔离级别是不同的。按照 [A Critique of ANSI SQL Isolation Levels](https://www.microsoft.com/en-us/research/wp-content/uploads/2016/02/tr-95-51.pdf) 论文中的标准,TiDB 实现的是论文中的快照隔离级别。该隔离级别不会出现狭义上的幻读 (A3),但不会阻止广义上的幻读 (P3),同时,SI 还会出现写偏斜,而 ANSI 可重复读隔离级别不会出现写偏斜,会出现幻读。
Expand All @@ -53,14 +56,19 @@ MySQL 可重复读隔离级别在更新时并不检验当前版本是否可见

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

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

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

### 与 MySQL 读已提交隔离级别的区别
yikeke marked this conversation as resolved.
Show resolved Hide resolved

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

## 更多阅读

- [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/)