If one session has a shared or exclusive lock on record R in an index, another session cannot insert

If one session has a shared or exclusive lock on record R in an index, another session cannot insert a new index 

record in the gap immediately before R in the index order. 

如果一个session 有一个共享或者排它锁在记录R上,另外的session 不能插入新的index record 在before R这个区间



Session 1:

mysql> start transaction;
Query OK, 0 rows affected (0.00 sec)

mysql> select * from SmsTest where phoneNo=10 for update;
+-------+---------+-------------+--------+
| sn    | phoneNo | channelType | status |
+-------+---------+-------------+--------+
|    10 |      10 |           2 |      1 |
| 45239 |      10 |           1 |      1 |
| 45252 |      10 |           1 |      1 |
+-------+---------+-------------+--------+
3 rows in set (0.01 sec)




Session 2:

Database changed
mysql>  insert into zjzc.SmsTest(PhoneNo,channelType,status)  values(1,1,1);
Query OK, 1 row affected (0.01 sec)

mysql>  insert into zjzc.SmsTest(PhoneNo,channelType,status)  values(2,1,1);
Query OK, 1 row affected (0.00 sec)

mysql>  insert into zjzc.SmsTest(PhoneNo,channelType,status)  values(3,1,1);
Query OK, 1 row affected (0.01 sec)

mysql>  insert into zjzc.SmsTest(PhoneNo,channelType,status)  values(4,1,1);
Query OK, 1 row affected (0.00 sec)

mysql>  insert into zjzc.SmsTest(PhoneNo,channelType,status)  values(5,1,1);
Query OK, 1 row affected (0.00 sec)

mysql>  insert into zjzc.SmsTest(PhoneNo,channelType,status)  values(6,1,1);
Query OK, 1 row affected (0.00 sec)

mysql>  insert into zjzc.SmsTest(PhoneNo,channelType,status)  values(7,1,1);
Query OK, 1 row affected (0.00 sec)

mysql>  insert into zjzc.SmsTest(PhoneNo,channelType,status)  values(8,1,1);
Query OK, 1 row affected (0.01 sec)

mysql>  insert into zjzc.SmsTest(PhoneNo,channelType,status)  values(9,1,1);
^[[A
ERROR 1205 (HY000): Lock wait timeout exceeded; try restarting transaction
mysql>  insert into zjzc.SmsTest(PhoneNo,channelType,status)  values(9,1,1);
ERROR 1205 (HY000): Lock wait timeout exceeded; try restarting transaction
mysql>  insert into zjzc.SmsTest(PhoneNo,channelType,status)  values(10,1,1); --hang

mysql>  insert into zjzc.SmsTest(PhoneNo,channelType,status)  values(11,1,1);
Query OK, 1 row affected (0.01 sec)

原文地址:https://www.cnblogs.com/hzcya1995/p/13350133.html