SELECT @@global.tx_isolation,@@tx_isolation;
REPEATABLE-READ REPEATABLE-READ
#MySQL 默认事务等级是这两种
# 全局
set global transaction isolation level serializable;
# 会话
set session transaction isolation level serializable;
create table test_wl (id int PRIMARY KEY AUTO_INCREMENT, name varchar(20), num int);
# session1: 会话一
1. start transaction;
4. update test_wl set num = 40 where id = 1;
6. commit;
# session2: 会话二
2. start transaction;
3. update test_wl set num = 20 where id = 1;
5. commit;
# session1: 会话一
1. start transaction;
3. select * from test_wl where id=1;
5. update test_wl set num = 40 where id = 1;
7. commit;
# session2: 会话二
2. start transaction;
4. update test_wl set num = 20 where id = 1;
6. commit;
# session1: 会话一
1. start transaction;
3. select * from test_wl where id=1 for update;
5. update test_wl set num = 20 where id = 1;
6. commit;
# session2: 会话二
2. start transaction;
4. update test_wl set num = 30 where id = 1;
7. commit;
# session1: 会话一
1. set session transaction isolation level serializable;
3. start transaction;
5. select * from test_wl where id=1;
7. update test_wl set num = 30 where id = 1;
8. commit;
# session2: 会话二
2. set session transaction isolation level serializable;
4. start transaction;
6. update test_wl set num = 20 where id = 1;
9. commit;
# session1: 会话一
1. set session transaction isolation level serializable;
3. start transaction;
5. select * from test_wl where id=1;
7. update test_wl set num = 40 where id = 1;
8. commit;
# session2: 会话二
2. set session transaction isolation level serializable;
4. start transaction;
6. update test_wl set num = 20 where id = 2;
9. commit;