zoukankan      html  css  js  c++  java
  • mysql update 等值 锁区间

    CREATE TABLE `p300` (
      `sn` int(11) NOT NULL AUTO_INCREMENT,
      `uuid` int(11) NOT NULL,
      `mobilePhone` int(11) NOT NULL,
      PRIMARY KEY (`sn`),
      KEY `p100_idx1` (`uuid`)
    ) ENGINE=InnoDB AUTO_INCREMENT=1 DEFAULT CHARSET=utf8
    
    mysql> insert into p300 select sn,sn,mobilePhone from p300;
    Query OK, 17736 rows affected (0.20 sec)
    Records: 17736  Duplicates: 0  Warnings: 0
    
    
    
    Session 1:
    
    mysql> select connection_id();  
    +-----------------+
    | connection_id() |
    +-----------------+
    |               1 |
    +-----------------+
    1 row in set (0.00 sec)
    
    mysql> select * from p300 where sn<20;
    +----+------+-------------+
    | sn | uuid | mobilePhone |
    +----+------+-------------+
    |  1 |    1 |         111 |
    |  3 |    3 |        3333 |
    |  5 |    5 |        5555 |
    | 10 |   10 |         222 |
    +----+------+-------------+
    4 rows in set (0.00 sec)
    
    
    mysql> start transaction;
    Query OK, 0 rows affected (0.00 sec)
    
    mysql> select * from p300 where uuid = 6 for update;
    Empty set (0.00 sec)
    
    
    
    Session 2:
    mysql> select connection_id();
    +-----------------+
    | connection_id() |
    +-----------------+
    |               2 |
    +-----------------+
    1 row in set (0.00 sec)
    
    mysql> start transaction;
    Query OK, 0 rows affected (0.00 sec)
    
    mysql> start transaction;
    Query OK, 0 rows affected (0.00 sec)
    
    mysql> insert into p300 values(1,1,1);
    ERROR 1062 (23000): Duplicate entry '1' for key 'PRIMARY'
    mysql> insert into p300(uuid,mobilePhone) values(1,1);
    Query OK, 1 row affected (0.00 sec)
    
    mysql> insert into p300(uuid,mobilePhone) values(2,1);
    Query OK, 1 row affected (0.00 sec)
    
    mysql> insert into p300(uuid,mobilePhone) values(3,1);
    Query OK, 1 row affected (0.00 sec)
    
    mysql> insert into p300(uuid,mobilePhone) values(4,1);
    Query OK, 1 row affected (0.00 sec)
    
    mysql> insert into p300(uuid,mobilePhone) values(5,1);
    ERROR 1205 (HY000): Lock wait timeout exceeded; try restarting transaction
    mysql> insert into p300(uuid,mobilePhone) values(6,1);
    ERROR 1205 (HY000): Lock wait timeout exceeded; try restarting transaction
    mysql> insert into p300(uuid,mobilePhone) values(7,1);
    ERROR 1205 (HY000): Lock wait timeout exceeded; try restarting transaction
    mysql> insert into p300(uuid,mobilePhone) values(8,1);
    ERROR 1205 (HY000): Lock wait timeout exceeded; try restarting transaction
    mysql> insert into p300(uuid,mobilePhone) values(9,1);
    ERROR 1205 (HY000): Lock wait timeout exceeded; try restarting transaction
    mysql> insert into p300(uuid,mobilePhone) values(10,1);
    Query OK, 1 row affected (0.00 sec)
    
    mysql> insert into p300(uuid,mobilePhone) values(11,1);
    Query OK, 1 row affected (0.00 sec)
    
    
    
    虽然select * from p300 where uuid = 6 for update; uud=6并不存在,但是还是锁了[5,10)
    
    
    
    
    那么有值的情况 mysql 是怎么锁区间的呢?
    
    
    Session 1:
    mysql> select connection_id(); 
    +-----------------+
    | connection_id() |
    +-----------------+
    |               1 |
    +-----------------+
    1 row in set (0.00 sec)
    
    mysql> select * from p300 where sn<20;
    +----+------+-------------+
    | sn | uuid | mobilePhone |
    +----+------+-------------+
    |  1 |    1 |         111 |
    |  3 |    3 |        3333 |
    |  5 |    5 |        5555 |
    | 10 |   10 |         222 |
    +----+------+-------------+
    4 rows in set (0.00 sec)
    
    
    
    mysql> start transaction;
    Query OK, 0 rows affected (0.00 sec)
    
    mysql>  select * from p300 where uuid =5 for update;
    +----+------+-------------+
    | sn | uuid | mobilePhone |
    +----+------+-------------+
    |  5 |    5 |        5555 |
    +----+------+-------------+
    1 row in set (0.00 sec)
    
    
    
    
    Session 2:
    
    mysql> select connection_id(); 
    +-----------------+
    | connection_id() |
    +-----------------+
    |               2 |
    +-----------------+
    1 row in set (0.00 sec)
    
    mysql>  insert into p300(uuid,mobilePhone) values(1,1);
    Query OK, 1 row affected (0.00 sec)
    
    mysql>  insert into p300(uuid,mobilePhone) values(2,1);
    Query OK, 1 row affected (0.00 sec)
    
    mysql>  insert into p300(uuid,mobilePhone) values(3,1);
    ERROR 1205 (HY000): Lock wait timeout exceeded; try restarting transaction
    mysql>  insert into p300(uuid,mobilePhone) values(3,1);
    ERROR 1205 (HY000): Lock wait timeout exceeded; try restarting transaction
    mysql>  insert into p300(uuid,mobilePhone) values(4,1);
    ERROR 1205 (HY000): Lock wait timeout exceeded; try restarting transaction
    mysql>  insert into p300(uuid,mobilePhone) values(5,1);
    ERROR 1205 (HY000): Lock wait timeout exceeded; try restarting transaction
    mysql>  insert into p300(uuid,mobilePhone) values(6,1);
    ERROR 1205 (HY000): Lock wait timeout exceeded; try restarting transaction
    mysql>  insert into p300(uuid,mobilePhone) values(7,1);
    ERROR 1205 (HY000): Lock wait timeout exceeded; try restarting transaction
    mysql>  insert into p300(uuid,mobilePhone) values(8,1);
    ERROR 1205 (HY000): Lock wait timeout exceeded; try restarting transaction
    mysql>  insert into p300(uuid,mobilePhone) values(9,1);
    ERROR 1205 (HY000): Lock wait timeout exceeded; try restarting transaction
    mysql>  insert into p300(uuid,mobilePhone) values(10,1);
    Query OK, 1 row affected (0.00 sec)
    
    mysql>  insert into p300(uuid,mobilePhone) values(11,1);
    Query OK, 1 row affected (0.00 sec)
    
    
    select * from p300 where uuid =5 for update; 有值 此时锁住的区间是[5,10);
    

  • 相关阅读:
    Invalid bound statement (not found)解决方法
    MySQL的sum()函数
    关于Mybatis的java.lang.UnsupportedOperationException异常处理
    博客迁移
    Building Blog(个性化博客)2
    走一波服务器
    JZ高中OJ 1036. [SCOI2009]迷路
    JZ初中OJ 2296. [noip普及组2]神殿
    JZ初中OJ 2295. [noip普及组2]栈
    JZ初中OJ 2298. [noip普及组2]异或
  • 原文地址:https://www.cnblogs.com/hzcya1995/p/13349953.html
Copyright © 2011-2022 走看看