怎么使用innodb行锁
三种级别
record lock 锁住某一行记录
gap lock 锁住某一段范围中的记录
next key lock 是前两者效果的叠加
实验环境
mysql> select @@tx_isolation;
+-----------------+
| @@tx_isolation |
+-----------------+
| REPEATABLE-READ |
+-----------------+
1 row in set (0.00 sec)
mysql> select * from t;
+------+
| id|
+------+
|2 |
|8 |
|19 |
|22 |
+------+
mysql> create index t_ind on t(id);
Query OK, 0 rows affected (8.52 sec)
同时打开两个会话,并打开事物
会话一
mysql> start transaction;
Query OK, 0 rows affected (0.00 sec)
mysql> delete from t where id=19;
Query OK, 1 row affected (0.00 sec)
会话二
mysql> start transaction;
Query OK, 0 rows affected (0.00 sec)
mysql> insert into t values(55);
Query OK, 1 row affected (0.00 sec)
mysql> insert into t values(10);
^C^C -- query aborted
ERROR 1317 (70100): Query execution was interrupted
mysql>
mysql> insert into t values(1);
Query OK, 1 row affected (0.00 sec)
mysql> insert into t values(12);
^C^C -- query aborted
ERROR 1317 (70100): Query execution was interrupted
mysql>
会话二说明8-22之间被锁住了,无法插入数据.这就是gap lock
取消gap lock
修改事物的隔离级别到READ COMMITTED