mysql 提交讀 可重複讀

2021-07-23 02:11:58 字數 4173 閱讀 8426

環境

mysql> select version();

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

| version() |

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

| 5.6.31-log |

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

1 row in set (0.00 sec)

檢視當前資料庫隔離級別

mysql> select @@global.tx_isolation,@@tx_isolation;

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

| @@global.tx_isolation | @@tx_isolation |

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

| read-committed | read-committed |

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

1 row in set (0.00 sec)

檢視測試表結構

mysql> show create table t\g;

*****

*****

*****

*****

*****

** 1. row **

*****

*****

*****

*****

*****

table: t

create table: create table `t` (

`id` int(11) not null auto_increment,

`comment` varchar(30) default null,

`time` datetime default null,

primary key (`id`)

) engine=innodb auto_increment=2 default charset=utf8

1 row in set (0.00 sec)

解決髒讀問題,但是會出現不可重複讀

#session 1                                          session 2

mysql> begin; mysql> begin;

query ok, 0 rows affected (0.00 sec) query ok, 0 rows affected (0.00 sec)

mysql> select * from t;

empty set (0.00 sec)

mysql> insert into t(comment,time) values ('r1',now());

query ok, 1 row affected (0.00 sec)

mysql> commit;

query ok, 0 rows affected (0.00 sec)

mysql> select * from t;

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

| id | comment | time |

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

| 1 | r1 | 2016-09-23 15:56:17 |

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

1 row in set (0.00 sec)

mysql> commit;

query ok, 0 rows affected (0.00 sec)

設定全域性的隔離級別為repeatable-read

mysql>

setglobal tx_isolation=

'repeatable-read';

query ok, 0

rows affected (0.00 sec)

清空測試資料

mysql> truncate table t;

query ok, 0 rows affected (0.03 sec)

重複執行上面的實驗,可見在可重複讀的隔離級別下,不可重複讀得到了解決

#session 1                                          session 2

mysql> begin; mysql> begin;

query ok, 0 rows affected (0.00 sec) query ok, 0 rows affected (0.00 sec)

mysql> select * from t;

empty

set (0.00 sec)

mysql> insert into t(comment,time) values ('r2',now());

query ok, 1 row affected (0.00 sec)

mysql> commit;

query ok, 0 rows affected (0.00 sec)

mysql> select * from t;

empty

set (0.00 sec)

mysql> commit;

query ok, 0 rows affected (0.00 sec)

清空測試資料

mysql> truncate table t;

query ok, 0 rows affected (0.03 sec)

進一步測試,幻讀現象出現

#session 1                                          session 2

mysql> begin; mysql> begin;

query ok, 0 rows affected (0.00 sec) query ok, 0 rows affected (0.00 sec)

mysql> select * from t;

empty set (0.00 sec)

mysql> insert into t(comment,time) values ('r3',now());

query ok, 1 row affected (0.00 sec)

mysql> commit;

query ok, 0 rows affected (0.00 sec)

mysql> select * from t;

empty set (0.00 sec)

mysql> update t set comment='c3';

query ok, 1 row affected (0.00 sec)

rows matched: 1 changed: 1 warnings: 0

mysql> select * from t;

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

| id | comment | time |

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

| 1 | c3 | 2016-09-23 16:53:28 |

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

1 row in set (0.00 sec)

mysql> commit;

query ok, 0 rows affected (0.00 sec)

mysql 提交讀 可重複讀

環境 mysql select version version 5.6.31 log 1 row in set 0.00 sec 檢視當前資料庫隔離級別 mysql select global.tx isolation,tx isolation global.tx isolation tx isol...

mysql 可重複讀。

一 可重複讀 我們先看看現象,再分析原理。我的mysql版本是5.5。下面是一張表,只有一條資料,並且我開啟了事物 此時,另乙個事物將record加1,因此我在開啟乙個命令列客戶端,執行下面的命令 成功加1之後,實際上,資料庫中record肯定是2。然後回到之前的客戶端,再查一次 沒毛病,recor...

mysql可重複讀

mysql innodb的預設隔離級別是可重複讀,之前理解有些偏差,查閱一些資料後總結出幾點 首先有兩個概念 一致性檢視 當乙個事務開啟時,innodb會生成乙個檢視,這個檢視是邏輯檢視,通過undo log和row tranzaction id控制實現。在該事務的任何時間點,一致性檢視中的資料都是...