• RR模式下利用区间锁防止幻读,RC模式没有区间锁会出现幻读


    Session 1:
    mysql> start transaction;
    Query OK, 0 rows affected (0.00 sec)
    
    mysql> select * from SmsTest where phoneNo between 30 and 40 for update;
    +----+---------+-------------+--------+
    | sn | phoneNo | channelType | status |
    +----+---------+-------------+--------+
    | 30 |      30 |           2 |      1 |
    | 31 |      31 |           2 |      1 |
    | 32 |      32 |           2 |      1 |
    | 33 |      33 |           2 |      1 |
    | 34 |      34 |           2 |      1 |
    | 35 |      35 |           2 |      1 |
    | 36 |      36 |           2 |      1 |
    | 37 |      37 |           2 |      1 |
    | 38 |      38 |           2 |      1 |
    | 39 |      39 |           2 |      1 |
    | 40 |      40 |           2 |      1 |
    +----+---------+-------------+--------+
    11 rows in set (0.00 sec)
    
    mysql> explain select * from SmsTest where phoneNo between 30 and 40 for update;
    +----+-------------+---------+-------+---------------+--------------+---------+------+------+-----------------------+
    | id | select_type | table   | type  | possible_keys | key          | key_len | ref  | rows | Extra                 |
    +----+-------------+---------+-------+---------------+--------------+---------+------+------+-----------------------+
    |  1 | SIMPLE      | SmsTest | range | SmsTest_idx1  | SmsTest_idx1 | 4       | NULL |   11 | Using index condition |
    +----+-------------+---------+-------+---------------+--------------+---------+------+------+-----------------------+
    1 row in set (0.00 sec)
    
    
    
    Session 2:
    
    mysql>  insert into zjzc.SmsTest(PhoneNo,channelType,status)  values(28,1,1);
    Query OK, 1 row affected (0.00 sec)
    
    mysql>  insert into zjzc.SmsTest(PhoneNo,channelType,status)  values(29,1,1);  --hang
    
    mysql>  insert into zjzc.SmsTest(PhoneNo,channelType,status)  values(30,1,1);  --hang
    
    mysql>  insert into zjzc.SmsTest(PhoneNo,channelType,status)  values(31,1,1); --hang
    
    mysql>  insert into zjzc.SmsTest(PhoneNo,channelType,status)  values(32,1,1); --hang
    
    mysql>  insert into zjzc.SmsTest(PhoneNo,channelType,status)  values(33,1,1); --hang
    
    mysql>  insert into zjzc.SmsTest(PhoneNo,channelType,status)  values(34,1,1);--hang
    
    mysql>  insert into zjzc.SmsTest(PhoneNo,channelType,status)  values(40,1,1); --hang
    
    mysql>  insert into zjzc.SmsTest(PhoneNo,channelType,status)  values(41,1,1);
    Query OK, 1 row affected (0.01 sec)
    
    mysql>  insert into zjzc.SmsTest(PhoneNo,channelType,status)  values(42,1,1);
    Query OK, 1 row affected (0.04 sec)
    
    
    在30-40这个区间无法插入,RR需要区间锁来防止幻读
    
    
    
    修改事务隔离为transaction-isolation =READ-COMMITTED  
    
    mysql> select @@tx_isolation; 
    +----------------+
    | @@tx_isolation |
    +----------------+
    | READ-COMMITTED |
    +----------------+
    1 row in set (0.00 sec)
    
    mysql> start transaction;
    Query OK, 0 rows affected (0.00 sec)
    
    mysql> select * from SmsTest where phoneNo between 30 and 40 for update;
    +----+---------+-------------+--------+
    | sn | phoneNo | channelType | status |
    +----+---------+-------------+--------+
    | 30 |      30 |           2 |      1 |
    | 31 |      31 |           2 |      1 |
    | 32 |      32 |           2 |      1 |
    | 33 |      33 |           2 |      1 |
    | 34 |      34 |           2 |      1 |
    | 35 |      35 |           2 |      1 |
    | 36 |      36 |           2 |      1 |
    | 37 |      37 |           2 |      1 |
    | 38 |      38 |           2 |      1 |
    | 39 |      39 |           2 |      1 |
    | 40 |      40 |           2 |      1 |
    +----+---------+-------------+--------+
    11 rows in set (0.01 sec)
    
    mysql> explain select * from SmsTest where phoneNo between 30 and 40 for update;
    +----+-------------+---------+-------+---------------+--------------+---------+------+------+-----------------------+
    | id | select_type | table   | type  | possible_keys | key          | key_len | ref  | rows | Extra                 |
    +----+-------------+---------+-------+---------------+--------------+---------+------+------+-----------------------+
    |  1 | SIMPLE      | SmsTest | range | SmsTest_idx1  | SmsTest_idx1 | 4       | NULL |   11 | Using index condition |
    +----+-------------+---------+-------+---------------+--------------+---------+------+------+-----------------------+
    1 row in set (0.01 sec)
    
    
    
    Session2:
    
    
    
    Database changed
    mysql> start transaction;
    Query OK, 0 rows affected (0.00 sec)
    
    mysql>  insert into zjzc.SmsTest(PhoneNo,channelType,status)  values(28,1,1);
    Query OK, 1 row affected (0.00 sec)
    
    mysql>  insert into zjzc.SmsTest(PhoneNo,channelType,status)  values(29,1,1); 
    Query OK, 1 row affected (0.00 sec)
    
    mysql>  insert into zjzc.SmsTest(PhoneNo,channelType,status)  values(30,1,1);
    Query OK, 1 row affected (0.00 sec)
    
    mysql> commit;
    Query OK, 0 rows affected (0.00 sec)
    
    mysql>  insert into zjzc.SmsTest(PhoneNo,channelType,status)  values(31,1,1);
    Query OK, 1 row affected (0.00 sec)
    
    
    Session 1:
    
    mysql> select * from SmsTest where phoneNo between 30 and 40 for update;
    +-------+---------+-------------+--------+
    | sn    | phoneNo | channelType | status |
    +-------+---------+-------------+--------+
    |    30 |      30 |           2 |      1 |
    | 45292 |      30 |           1 |      1 |
    |    31 |      31 |           2 |      1 |
    |    32 |      32 |           2 |      1 |
    |    33 |      33 |           2 |      1 |
    |    34 |      34 |           2 |      1 |
    |    35 |      35 |           2 |      1 |
    |    36 |      36 |           2 |      1 |
    |    37 |      37 |           2 |      1 |
    |    38 |      38 |           2 |      1 |
    |    39 |      39 |           2 |      1 |
    |    40 |      40 |           2 |      1 |
    +-------+---------+-------------+--------+
    12 rows in set (0.00 sec)
    
    mysql> select * from SmsTest where phoneNo between 30 and 40 for update;
    +-------+---------+-------------+--------+
    | sn    | phoneNo | channelType | status |
    +-------+---------+-------------+--------+
    |    30 |      30 |           2 |      1 |
    | 45292 |      30 |           1 |      1 |
    |    31 |      31 |           2 |      1 |
    | 45293 |      31 |           1 |      1 |
    |    32 |      32 |           2 |      1 |
    |    33 |      33 |           2 |      1 |
    |    34 |      34 |           2 |      1 |
    |    35 |      35 |           2 |      1 |
    |    36 |      36 |           2 |      1 |
    |    37 |      37 |           2 |      1 |
    |    38 |      38 |           2 |      1 |
    |    39 |      39 |           2 |      1 |
    |    40 |      40 |           2 |      1 |
    +-------+---------+-------------+--------+
    13 rows in set (0.01 sec)
    
    
    结论 RR模式下 需要区间锁来防止幻读,
    
         RC模式下,没有区间锁,会出现幻读

  • 相关阅读:
    函数式编程(二):curry
    函数式编程(一):纯函数
    用 gulp 建一个服务器
    深度学习-Tensorflow2.2-预训练网络{7}-迁移学习基础针对小数据集-19
    深度学习-Tensorflow2.2-自定义训练综合实例与图片增强{6}-猫狗数据集实例-18
    深度学习-Tensorflow2.2-Tensorboard可视化{5}-可视化基础-17
    深度学习-Tensorflow2.2-Eager模式与自定义训练{4}-微分运算训练练习-16
    深度学习-Tensorflow2.2-卷积神经网络{3}-电影评论数据分类/猫狗数据集实例-15
    深度学习-Tensorflow2.2-批标准化简介-14
    深度学习-Tensorflow2.2-卷积神经网络{3}-卫星图像识别卷积综合实例(二分类)-13
  • 原文地址:https://www.cnblogs.com/zhaoyangjian724/p/6199084.html
Copyright © 2020-2023  润新知