read committed mysql_mysql中read commited与repeatable read两种隔离级别的测试

两个事物,A与B

1. 先看read committed级别的情况:

--A事物

mysql> set session transaction isolation level read committed;

Query OK, 0 rows affected (0.00 sec)

mysql> set autocommit = 0;

Query OK, 0 rows affected (0.01 sec)

mysql> begin;

Query OK, 0 rows affected (0.00 sec)

mysql> select * from test;

+------+------+

| id   | comm |

+------+------+

|    1 | aaa  |

+------+------+

1 row in set (0.00 sec)

--B事物

mysql> set autocommit = 0;

Query OK, 0 rows affected (0.00 sec)

mysql> begin;

Query OK, 0 rows affected (0.00 sec)

mysql> update test set comm = 'bbb' where id = 1;

Query OK, 1 row affected (0.00 sec)

Rows matched: 1  Changed: 1  Warnings: 0

--A事物此时查询表test,此时B事物未提交,因此读到的仍然是旧的数据

mysql> select * from test;

+------+------+

| id   | comm |

+------+------+

|    1 | aaa  |

+------+------+

1 row in set (0.00 sec)

--B事物提交

mysql> commit;

Query OK, 0 rows affected (0.00 sec)

--A事物再次查询,数据已发生改变。

mysql> select * from test;

+------+------+

| id   | comm |

+------+------+

|    1 | bbb  |

+------+------+

1 row in set (0.00 sec)

可见,在 read committed级别,顾名思义,A事物在事物进行过程中,读取不到B事物未提交的数据,但是可以读取到B事物已经提交的数据修改

2.再看repeatable read级别

--A事物:

mysql> set autocommit = 0;

Query OK, 0 rows affected (0.00 sec)

mysql> set session transaction isolation level repeatable read;

Query OK, 0 rows affected (0.00 sec)

mysql> begin;

Query OK, 0 rows affected (0.00 sec)

mysql> select * from test;

+------+------+

| id   | comm |

+------+------+

|    1 | aaa  |

+------+------+

1 row in set (0.00 sec)

--B事物:

mysql> set autocommit = 0;

Query OK, 0 rows affected (0.00 sec)

mysql> begin;

Query OK, 0 rows affected (0.00 sec)

mysql> update test set comm = 'bbb' where id = 1;

Query OK, 1 row affected (0.00 sec)

Rows matched: 1  Changed: 1  Warnings: 0

--A事物此时查询表test,此时B事物未提交,因此读到的仍然是旧的数据

mysql> select * from test;

+------+------+

| id   | comm |

+------+------+

|    1 | aaa  |

+------+------+

1 row in set (0.00 sec)

--B事物提交

mysql> commit;

Query OK, 0 rows affected (0.00 sec)

--A事物再次查询,发现数据未发生改变

mysql> select * from test;

+------+------+

| id   | comm |

+------+------+

|    1 | aaa  |

+------+------+

1 row in set (0.00 sec)

可见,在 repeatable read 级别,顾名思义,A事物在事物进行过程中,是不能读取到B事物未提交和已提交的数据修改。

对于repeatable read 级别,事物只能读取到事物开始时的数据,因此在事物进行过程中读取到的数据时一致的,而对于read commited级别,在事物进行过程中,读取到的数据可能是不一致的。

来自 “ ITPUB博客 ” ,链接:http://blog.itpub.net/10972173/viewspace-2153205/,如需转载,请注明出处,否则将追究法律责任。

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值