Home  >  Q&A  >  body text

transaction - 关于 MySQL 的事务隔离级别(REPEATABLE READ)

根据书上所说的,REPEATABLE READ 可以保证在一次事务中,相同查询得到结果是一致的,但是我做了一些测试,貌似跟书上结论不同。

-- 创建测试表
create table test(
    id int,
    num int
);
插入数据
insert into test(id, num)
values(1,1),(2,2),(3,3);

下面是我做的一些测试,左边和右边分别是 session_1 和 session_2 的操作,竖向为时间轴。

在 session_1 事务未完结的时候,我在 session_2 中修改了一列,按照书上的说法,在 Repeatable Read 此时得到的结果应该仍然为上次查询的结果,也就是:

id     num  

------ --------

 1         1
 2         2
 3         3

然而,实际查询结果却为:

id     num  

------ --------

 1        10
 2         2
 3         3

事实上,经过测试在 session_2 COMMIT 之前,在 session_1 中的查询结果就已经为:

id     num  

------ --------

 1         1
 2         2
 3         3

也就是说,Reapeatable Read 隔离级别下和 Read Uncommitted 隔离级别的效果是一样的!
我想知道是我在操作中出了什么问题吗?

黄舟黄舟2743 days ago674

reply all(3)I'll reply

  • 阿神

    阿神2017-04-17 13:51:18

    I tested it myself and found that there is no problem. How to open the two sessions of LZ? Use mysql client tool? Or command line?
    If it is a mysql client tool, it is very likely that the two so-called sessions are actually the same. You can check it by checking the processlist.

    reply
    0
  • 迷茫

    迷茫2017-04-17 13:51:18

    Session2 has obviously only been checked once, the same transaction, the same query, what do you mean by trying it again...

    reply
    0
  • 黄舟

    黄舟2017-04-17 13:51:18

    I just wrote an article to demonstrate this result in detail, address: https://segmentfault.com/a/1190000004469395

    reply
    0
  • Cancelreply