Preface
 
    I've demontstrated several InnoDB locking cases in my previous blog.I'm gonna do the rest tests about InnoDB locks.
 
Procedure
 
Test table information and the relevant variables.
 zlm@192.168.56.100: [zlm]>show create table t1\G
*************************** . row ***************************
Table: t1
Create Table: CREATE TABLE `t1` (
`c1` int() unsigned NOT NULL DEFAULT '',
`c2` int() unsigned NOT NULL DEFAULT '',
`c3` int() unsigned NOT NULL DEFAULT '',
`c4` int() unsigned NOT NULL DEFAULT '',
PRIMARY KEY (`c1`),
KEY `c2` (`c2`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8
row in set (0.00 sec) zlm@192.168.56.100: [zlm]>select * from t1;
+----+----+----+----+
| c1 | c2 | c3 | c4 |
+----+----+----+----+
| | | | |
| | | | |
| | | | |
| | | | |
| | | | |
| | | | |
| | | | |
+----+----+----+----+
rows in set (0.01 sec) zlm@192.168.56.100: [zlm]>select @@transaction_isolation;
+-------------------------+
| @@transaction_isolation |
+-------------------------+
| REPEATABLE-READ |
+-------------------------+
row in set (0.00 sec) zlm@192.168.56.100: [(none)]>show variables like 'innodb_status_output_locks';
+----------------------------+-------+
| Variable_name | Value |
+----------------------------+-------+
| innodb_status_output_locks | ON |
+----------------------------+-------+
row in set (0.00 sec)

Test 1: session1 update while session2 insert.

 //Session1:
zlm@192.168.56.100: [zlm]>begin;update t1 set c4= where c2>=;select * from t1 where c2>=;
Query OK, rows affected (0.00 sec) Query OK, rows affected (0.00 sec)
Rows matched: Changed: Warnings: +----+----+----+----+
| c1 | c2 | c3 | c4 |
+----+----+----+----+
| | | | |
| | | | |
+----+----+----+----+
rows in set (0.00 sec) //Lock information of session1.
TABLE LOCK table `zlm`.`t1` trx id lock mode IX
RECORD LOCKS space id page no n bits index c2 of table `zlm`.`t1` trx id lock_mode X
Record lock, heap no PHYSICAL RECORD: n_fields ; compact format; info bits
: len ; hex 73757072656d756d; asc supremum;; Record lock, heap no PHYSICAL RECORD: n_fields ; compact format; info bits
: len ; hex ; asc ;;
: len ; hex ; asc ;; Record lock, heap no PHYSICAL RECORD: n_fields ; compact format; info bits
: len ; hex ; asc ;;
: len ; hex 0000000a; asc ;; RECORD LOCKS space id page no n bits index PRIMARY of table `zlm`.`t1` trx id lock_mode X locks rec but not gap
Record lock, heap no PHYSICAL RECORD: n_fields ; compact format; info bits
: len ; hex ; asc ;;
: len ; hex 0000002dbd28; asc - (;;
: len ; hex 3a0000012727bb; asc : '' ;;
: len ; hex ; asc ;;
: len ; hex ; asc ;;
: len ; hex ; asc ;; Record lock, heap no PHYSICAL RECORD: n_fields ; compact format; info bits
: len ; hex 0000000a; asc ;;
: len ; hex 0000002dbd28; asc - (;;
: len ; hex 3a000001272799; asc : '' ;;
: len ; hex ; asc ;;
: len ; hex ; asc ;;
: len ; hex ; asc ;; //Session2:
zlm@192.168.56.100: [zlm]>begin;insert into t1 select ,,,;
Query OK, rows affected (0.00 sec) ERROR (HY000): Lock wait timeout exceeded; try restarting transaction //Session2 was block because of the gap lock(c2>=4 hold a supremum lock) which was holed by session 1.The value c2=5 which session2 want to insert is conficted with the range lock.

Test 2: session1 update while session2 insert.

 //Session1:
zlm@192.168.56.100: [zlm]>begin;update t1 set c4= where c2>=;select * from t1 where c2>=;
Query OK, rows affected (0.00 sec) Query OK, rows affected (0.00 sec)
Rows matched: Changed: Warnings: +----+----+----+----+
| c1 | c2 | c3 | c4 |
+----+----+----+----+
| | | | |
| | | | |
+----+----+----+----+
rows in set (0.00 sec) //Session2:
zlm@192.168.56.100: [zlm]>begin;insert into t1 select ,,,;
Query OK, rows affected (0.00 sec) Query OK, row affected (0.00 sec)
Records: Duplicates: Warnings: //This time the transaction in session2 was committed immediately.The value c2=2 didn't conflict with the range lock in session1.

Test 3: session1  update while session2 insert.

 //Session1:
zlm@192.168.56.100: [zlm]>begin;update t1 set c4= where c1>=;select * from t1 where c1>=;
Query OK, rows affected (0.00 sec) Query OK, rows affected (0.00 sec)
Rows matched: Changed: Warnings: +----+----+----+----+
| c1 | c2 | c3 | c4 |
+----+----+----+----+
| | | | |
| | | | |
| | | | |
+----+----+----+----+
rows in set (0.00 sec) //Lock information of session1.
---TRANSACTION , ACTIVE sec
lock struct(s), heap size , row lock(s), undo log entries
MySQL thread id , OS thread handle , query id zlm1 192.168.56.100 zlm
Trx read view will not see trx with id >= , sees <
TABLE LOCK table `zlm`.`t1` trx id lock mode IX
RECORD LOCKS space id page no n bits index PRIMARY of table `zlm`.`t1` trx id lock_mode X locks rec but not gap
Record lock, heap no PHYSICAL RECORD: n_fields ; compact format; info bits
: len ; hex ; asc ;;
: len ; hex 0000002dbd2f; asc - /;;
: len ; hex 3e0000012d180e; asc > - ;;
: len ; hex ; asc ;;
: len ; hex ; asc ;;
: len ; hex ; asc ;; RECORD LOCKS space id page no n bits index PRIMARY of table `zlm`.`t1` trx id lock_mode X
Record lock, heap no PHYSICAL RECORD: n_fields ; compact format; info bits
: len ; hex 73757072656d756d; asc supremum;; Record lock, heap no PHYSICAL RECORD: n_fields ; compact format; info bits
: len ; hex ; asc ;;
: len ; hex 0000002dbd2f; asc - /;;
: len ; hex 3e0000012d1830; asc > - ;;
: len ; hex ; asc ;;
: len ; hex ; asc ;;
: len ; hex ; asc ;; Record lock, heap no PHYSICAL RECORD: n_fields ; compact format; info bits
: len ; hex 0000000a; asc ;;
: len ; hex 0000002dbd2f; asc - /;;
: len ; hex 3e0000012d1852; asc > - R;;
: len ; hex ; asc ;;
: len ; hex ; asc ;;
: len ; hex ; asc ;; //Session2:
zlm@192.168.56.100: [zlm]>begin;insert into t1 select ,,,;
Query OK, rows affected (0.00 sec) ERROR (HY000): Lock wait timeout exceeded; try restarting transaction //Locks information of session2.
---TRANSACTION , ACTIVE sec inserting
mysql tables in use , locked
LOCK WAIT lock struct(s), heap size , row lock(s)
MySQL thread id , OS thread handle , query id zlm1 192.168.56.100 zlm executing
insert into t1 select ,,,
------- TRX HAS BEEN WAITING SEC FOR THIS LOCK TO BE GRANTED:
RECORD LOCKS space id page no n bits index PRIMARY of table `zlm`.`t1` trx id lock_mode X locks gap before rec insert intention waiting
Record lock, heap no PHYSICAL RECORD: n_fields ; compact format; info bits
: len ; hex 0000000a; asc ;;
: len ; hex 0000002dbd63; asc - c;;
: len ; hex 400000012f17f3; asc @ / ;;
: len ; hex ; asc ;;
: len ; hex ; asc ;;
: len ; hex ; asc ;; ------------------
TABLE LOCK table `zlm`.`t1` trx id lock mode IX
RECORD LOCKS space id page no n bits index PRIMARY of table `zlm`.`t1` trx id lock_mode X locks gap before rec insert intention waiting
Record lock, heap no PHYSICAL RECORD: n_fields ; compact format; info bits
: len ; hex 0000000a; asc ;;
: len ; hex 0000002dbd63; asc - c;;
: len ; hex 400000012f17f3; asc @ / ;;
: len ; hex ; asc ;;
: len ; hex ; asc ;;
: len ; hex ; asc ;; zlm@192.168.56.100: [zlm]>begin;insert into t1 select ,,,;
Query OK, rows affected (0.00 sec) ERROR (HY000): Lock wait timeout exceeded; try restarting transaction //Locks information of session2.
Trx id counter
Purge done for trx's n:o < 2997603 undo n:o < 0 state: running but idle
History list length
LIST OF TRANSACTIONS FOR EACH SESSION:
---TRANSACTION , not started
lock struct(s), heap size , row lock(s)
---TRANSACTION , ACTIVE sec inserting
mysql tables in use , locked
LOCK WAIT lock struct(s), heap size , row lock(s)
MySQL thread id , OS thread handle , query id zlm1 192.168.56.100 zlm executing
insert into t1 select ,,,
------- TRX HAS BEEN WAITING SEC FOR THIS LOCK TO BE GRANTED:
RECORD LOCKS space id page no n bits index PRIMARY of table `zlm`.`t1` trx id lock_mode X insert intention waiting
Record lock, heap no PHYSICAL RECORD: n_fields ; compact format; info bits
: len ; hex 73757072656d756d; asc supremum;; ------------------
TABLE LOCK table `zlm`.`t1` trx id lock mode IX
RECORD LOCKS space id page no n bits index PRIMARY of table `zlm`.`t1` trx id lock_mode X insert intention waiting
Record lock, heap no PHYSICAL RECORD: n_fields ; compact format; info bits
: len ; hex 73757072656d756d; asc supremum;; zlm@192.168.56.100: [zlm]>begin;insert into t1 select ,,,;
Query OK, rows affected (0.00 sec) ERROR (HY000): Lock wait timeout exceeded; try restarting transaction //locks information of session2.
---TRANSACTION , ACTIVE sec inserting
mysql tables in use , locked
LOCK WAIT lock struct(s), heap size , row lock(s)
MySQL thread id , OS thread handle , query id zlm1 192.168.56.100 zlm executing
insert into t1 select ,,,
------- TRX HAS BEEN WAITING SEC FOR THIS LOCK TO BE GRANTED:
RECORD LOCKS space id page no n bits index PRIMARY of table `zlm`.`t1` trx id lock mode S waiting
Record lock, heap no PHYSICAL RECORD: n_fields ; compact format; info bits
: len ; hex 0000000a; asc ;;
: len ; hex 0000002dbd68; asc - h;;
: len ; hex 4400000da22824; asc D ($;;
: len ; hex ; asc ;;
: len ; hex ; asc ;;
: len ; hex ; asc ;; ------------------
TABLE LOCK table `zlm`.`t1` trx id lock mode IX
RECORD LOCKS space id page no n bits index PRIMARY of table `zlm`.`t1` trx id lock mode S waiting
Record lock, heap no PHYSICAL RECORD: n_fields ; compact format; info bits
: len ; hex 0000000a; asc ;;
: len ; hex 0000002dbd68; asc - h;;
: len ; hex 4400000da22824; asc D ($;;
: len ; hex ; asc ;;
: len ; hex ; asc ;;
: len ; hex ; asc ;; zlm@192.168.56.100: [zlm]>begin;insert into t1 select ,,,;
Query OK, rows affected (0.00 sec) ERROR (HY000): Lock wait timeout exceeded; try restarting transaction
zlm@192.168.56.100: [zlm]>begin;insert into t1 select ,,,;
Query OK, rows affected (0.00 sec) ERROR (HY000): Lock wait timeout exceeded; try restarting transaction
zlm@192.168.56.100: [zlm]>begin;insert into t1 select ,,,;
Query OK, rows affected (0.00 sec) ERROR (HY000): Lock wait timeout exceeded; try restarting transaction
zlm@192.168.56.100: [zlm]>begin;insert into t1 select ,,,;
Query OK, rows affected (0.00 sec) Query OK, row affected (0.00 sec)
Records: Duplicates: Warnings: //Only c1=5 in session2 can be executed immediately.Because c1=5 didn't conflict with the range lock.

Test 4: session1 select for update while session2 delete.

 //Session1:
zlm@192.168.56.100: [zlm]>begin;select * from t1 where c1<= for update;
Query OK, rows affected (0.00 sec) +----+----+----+----+
| c1 | c2 | c3 | c4 |
+----+----+----+----+
| | | | |
| | | | |
| | | | |
| | | | |
+----+----+----+----+
rows in set (0.00 sec) //Locks information of session1.
---TRANSACTION , ACTIVE sec
lock struct(s), heap size , row lock(s)
MySQL thread id , OS thread handle , query id zlm1 192.168.56.100 zlm
TABLE LOCK table `zlm`.`t1` trx id lock mode IX
RECORD LOCKS space id page no n bits index PRIMARY of table `zlm`.`t1` trx id lock_mode X
Record lock, heap no PHYSICAL RECORD: n_fields ; compact format; info bits
: len ; hex ; asc ;;
: len ; hex 0000002dbb07; asc - ;;
: len ; hex a7000002690110; asc i ;;
: len ; hex ; asc ;;
: len ; hex ; asc ;;
: len ; hex ; asc ;; Record lock, heap no PHYSICAL RECORD: n_fields ; compact format; info bits
: len ; hex ; asc ;;
: len ; hex 0000002dbb07; asc - ;;
: len ; hex a700000269011d; asc i ;;
: len ; hex ; asc ;;
: len ; hex ; asc ;;
: len ; hex ; asc ;; Record lock, heap no PHYSICAL RECORD: n_fields ; compact format; info bits
: len ; hex ; asc ;;
: len ; hex 0000002dbb07; asc - ;;
: len ; hex a700000269012a; asc i *;;
: len ; hex ; asc ;;
: len ; hex ; asc ;;
: len ; hex ; asc ;; Record lock, heap no PHYSICAL RECORD: n_fields ; compact format; info bits
: len ; hex ; asc ;;
: len ; hex 0000002dbb07; asc - ;;
: len ; hex a7000002690137; asc i ;;
: len ; hex ; asc ;;
: len ; hex ; asc ;;
: len ; hex ; asc ;; Record lock, heap no PHYSICAL RECORD: n_fields ; compact format; info bits
: len ; hex ; asc ;;
: len ; hex 0000002dbd6c; asc - l;;
: len ; hex 470000025802f4; asc G X ;;
: len ; hex ; asc ;;
: len ; hex ; asc ;;
: len ; hex ; asc ;; //Session2:
zlm@192.168.56.100: [zlm]>begin;delete from t1 where c1=;
Query OK, rows affected (0.00 sec) ERROR (HY000): Lock wait timeout exceeded; try restarting transaction //The lock session2 requested for where c1=6 was locked either.So it was blocked.Even though primary key does not contains the gap lock,but when the condition contains a range like c1<=4,it will block the next-key of c1=6 here.

Test 5: session1 select for update while session2 delete.

 //Session1:
zlm@192.168.56.100: [zlm]>begin;select * from t1 where c1>= for update;
Query OK, rows affected (0.00 sec) +----+----+----+----+
| c1 | c2 | c3 | c4 |
+----+----+----+----+
| | | | |
| | | | |
| | | | |
| | | | |
+----+----+----+----+
rows in set (0.00 sec) //Lock information of session1.
---TRANSACTION , ACTIVE sec
lock struct(s), heap size , row lock(s)
MySQL thread id , OS thread handle , query id zlm1 192.168.56.100 zlm
TABLE LOCK table `zlm`.`t1` trx id lock mode IX
RECORD LOCKS space id page no n bits index PRIMARY of table `zlm`.`t1` trx id lock_mode X locks rec but not gap
Record lock, heap no PHYSICAL RECORD: n_fields ; compact format; info bits
: len ; hex ; asc ;;
: len ; hex 0000002dbb07; asc - ;;
: len ; hex a7000002690137; asc i ;;
: len ; hex ; asc ;;
: len ; hex ; asc ;;
: len ; hex ; asc ;; RECORD LOCKS space id page no n bits index PRIMARY of table `zlm`.`t1` trx id lock_mode X
Record lock, heap no PHYSICAL RECORD: n_fields ; compact format; info bits
: len ; hex 73757072656d756d; asc supremum;; Record lock, heap no PHYSICAL RECORD: n_fields ; compact format; info bits
: len ; hex ; asc ;;
: len ; hex 0000002dbd6c; asc - l;;
: len ; hex 470000025802f4; asc G X ;;
: len ; hex ; asc ;;
: len ; hex ; asc ;;
: len ; hex ; asc ;; Record lock, heap no PHYSICAL RECORD: n_fields ; compact format; info bits
: len ; hex ; asc ;;
: len ; hex 0000002dbd6c; asc - l;;
: len ; hex ; asc G X ;;
: len ; hex ; asc ;;
: len ; hex ; asc ;;
: len ; hex ; asc ;; Record lock, heap no PHYSICAL RECORD: n_fields ; compact format; info bits
: len ; hex 0000000a; asc ;;
: len ; hex 0000002dbd6c; asc - l;;
: len ; hex ; asc G X ;;
: len ; hex ; asc ;;
: len ; hex ; asc ;;
: len ; hex ; asc ;; //Session2:
zlm@192.168.56.100: [zlm]>delete from t1 where c1=;
Query OK, row affected (0.00 sec) //Session2 was not blocked this time.Because c1=3 was not in the gap lock here.

Test 5: session1 update while session2 select for update.

 //Session1:
zlm@192.168.56.100: [zlm]>begin;update t1 set c1= where c1=;
Query OK, rows affected (0.00 sec) Query OK, row affected (0.00 sec)
Rows matched: Changed: Warnings: //lock information of session1.
---TRANSACTION , ACTIVE sec
lock struct(s), heap size , row lock(s), undo log entries
MySQL thread id , OS thread handle , query id zlm1 192.168.56.100 zlm
TABLE LOCK table `zlm`.`t1` trx id lock mode IX
RECORD LOCKS space id page no n bits index PRIMARY of table `zlm`.`t1` trx id lock_mode X locks rec but not gap
Record lock, heap no PHYSICAL RECORD: n_fields ; compact format; info bits
: len ; hex ; asc ;;
: len ; hex 0000002dbd94; asc - ;;
: len ; hex 6000000dab26f6; asc ` & ;;
: len ; hex ; asc ;;
: len ; hex ; asc ;;
: len ; hex ; asc ;; //Session1 holded only 1 record lock with no gap of record c1=4. zlm@192.168.56.100: [zlm]>begin;select * from t1 where c2= for update;
Query OK, rows affected (0.00 sec) ERROR (HY000): Lock wait timeout exceeded; try restarting transaction //Lock information of session2.
---TRANSACTION , ACTIVE sec fetching rows
mysql tables in use , locked
LOCK WAIT lock struct(s), heap size , row lock(s)
MySQL thread id , OS thread handle , query id zlm1 192.168.56.100 zlm Sending data
select * from t1 where c2= for update
------- TRX HAS BEEN WAITING SEC FOR THIS LOCK TO BE GRANTED:
RECORD LOCKS space id page no n bits index PRIMARY of table `zlm`.`t1` trx id lock_mode X waiting
Record lock, heap no PHYSICAL RECORD: n_fields ; compact format; info bits
: len ; hex ; asc ;;
: len ; hex 0000002dbd94; asc - ;;
: len ; hex 6000000dab26f6; asc ` & ;;
: len ; hex ; asc ;;
: len ; hex ; asc ;;
: len ; hex ; asc ;; ------------------
TABLE LOCK table `zlm`.`t1` trx id lock mode IX
RECORD LOCKS space id page no n bits index PRIMARY of table `zlm`.`t1` trx id lock_mode X
Record lock, heap no PHYSICAL RECORD: n_fields ; compact format; info bits
: len ; hex ; asc ;;
: len ; hex 0000002dbb07; asc - ;;
: len ; hex a7000002690110; asc i ;;
: len ; hex ; asc ;;
: len ; hex ; asc ;;
: len ; hex ; asc ;; Record lock, heap no PHYSICAL RECORD: n_fields ; compact format; info bits
: len ; hex ; asc ;;
: len ; hex 0000002dbb07; asc - ;;
: len ; hex a700000269011d; asc i ;;
: len ; hex ; asc ;;
: len ; hex ; asc ;;
: len ; hex ; asc ;; Record lock, heap no PHYSICAL RECORD: n_fields ; compact format; info bits
: len ; hex ; asc ;;
: len ; hex 0000002dbd8b; asc - ;;
: len ; hex db0000019f0110; asc ;;
: len ; hex ; asc ;;
: len ; hex ; asc ;;
: len ; hex ; asc ;; RECORD LOCKS space id page no n bits index PRIMARY of table `zlm`.`t1` trx id lock_mode X waiting
Record lock, heap no PHYSICAL RECORD: n_fields ; compact format; info bits
: len ; hex ; asc ;;
: len ; hex 0000002dbd94; asc - ;;
: len ; hex 6000000dab26f6; asc ` & ;;
: len ; hex ; asc ;;
: len ; hex ; asc ;;
: len ; hex ; asc ;; //Session2 holded 4 record locks on c1 but only c1=4 was conflicted with session1.It was still blocked.
//I'm a little baffled why the session2 holded so many record locks on the primary key column(c1=0,c1=1,c1=3).

Test 6: session1 delete then insert while session2 insert.

 //Session1:
zlm@192.168.56.100: [zlm]>begin;delete from t1 where c1=;insert into t1 select ,,,;
Query OK, rows affected (0.00 sec) Query OK, row affected (0.00 sec) Query OK, row affected (0.00 sec)
Records: Duplicates: Warnings: //Lock information of session1.
---TRANSACTION , ACTIVE sec
lock struct(s), heap size , row lock(s), undo log entries
MySQL thread id , OS thread handle , query id zlm1 192.168.56.100 zlm
TABLE LOCK table `zlm`.`t1` trx id lock mode IX
RECORD LOCKS space id page no n bits index PRIMARY of table `zlm`.`t1` trx id lock_mode X locks rec but not gap
Record lock, heap no PHYSICAL RECORD: n_fields ; compact format; info bits
: len ; hex ; asc ;;
: len ; hex 0000002dbd9b; asc - ;;
: len ; hex 64000001b9298c; asc d ) ;;
: len ; hex ; asc ;;
: len ; hex ; asc ;;
: len ; hex ; asc ;; //Session2:
zlm@192.168.56.100: [zlm]>begin;insert into t1 select ,,,;
Query OK, rows affected (0.00 sec) Query OK, row affected (0.00 sec)
Records: Duplicates: Warnings: //Session2 did not conflict with the "X" record locks holded by session1.So it was executed immediately.Because there was no gap lock on c1 column(primary key).

Test 7: session1 insert while session2 insert.

 //Session1:
zlm@192.168.56.100: [zlm]>begin;insert into t1 select ,,,;
Query OK, rows affected (0.00 sec) Query OK, row affected (0.00 sec)
Records: Duplicates: Warnings: //Lock information of session1.
---TRANSACTION , ACTIVE sec
lock struct(s), heap size , row lock(s), undo log entries
MySQL thread id , OS thread handle , query id zlm1 192.168.56.100 zlm
TABLE LOCK table `zlm`.`t1` trx id lock mode IX //Notice that the c1=9 is not exist in primary key.It only generate a "IX" lock(intention lock). //Session2:
zlm@192.168.56.100: [zlm]>begin;insert into t1 select ,,,;
Query OK, rows affected (0.01 sec) ERROR (HY000): Lock wait timeout exceeded; try restarting transaction //Lock information of session2.
---TRANSACTION , ACTIVE sec inserting
mysql tables in use , locked
LOCK WAIT lock struct(s), heap size , row lock(s)
MySQL thread id , OS thread handle , query id zlm1 192.168.56.100 zlm executing
insert into t1 select ,,,
------- TRX HAS BEEN WAITING SEC FOR THIS LOCK TO BE GRANTED:
RECORD LOCKS space id page no n bits index PRIMARY of table `zlm`.`t1` trx id lock mode S waiting
Record lock, heap no PHYSICAL RECORD: n_fields ; compact format; info bits
: len ; hex ; asc ;;
: len ; hex 0000002dbda2; asc - ;;
: len ; hex e8000001b00110; asc ;;
: len ; hex ; asc ;;
: len ; hex ; asc ;;
: len ; hex ; asc ;; ------------------
TABLE LOCK table `zlm`.`t1` trx id lock mode IX
RECORD LOCKS space id page no n bits index PRIMARY of table `zlm`.`t1` trx id lock mode S waiting
Record lock, heap no PHYSICAL RECORD: n_fields ; compact format; info bits
: len ; hex ; asc ;;
: len ; hex 0000002dbda2; asc - ;;
: len ; hex e8000001b00110; asc ;;
: len ; hex ; asc ;;
: len ; hex ; asc ;;
: len ; hex ; asc ;; ---TRANSACTION , ACTIVE sec
lock struct(s), heap size , row lock(s), undo log entries
MySQL thread id , OS thread handle , query id zlm1 192.168.56.100 zlm
TABLE LOCK table `zlm`.`t1` trx id lock mode IX
RECORD LOCKS space id page no n bits index PRIMARY of table `zlm`.`t1` trx id lock_mode X locks rec but not gap
Record lock, heap no PHYSICAL RECORD: n_fields ; compact format; info bits
: len ; hex ; asc ;;
: len ; hex 0000002dbda2; asc - ;;
: len ; hex e8000001b00110; asc ;;
: len ; hex ; asc ;;
: len ; hex ; asc ;;
: len ; hex ; asc ;; //Session2 was waiting for the "S" record lock and it also request for "X" record lock on record c1=9 in primary key.Although the record c1=9 is not exist in primary key,but they were inserting into the same row.As the session2 cannot get the lock for inserting.It was blocked.

Test 8: session1 select for update while session2 insert.(modify the same row)

 //Session1:
zlm@192.168.56.100: [zlm]>begin;select * from t1 where c1= for update;
Query OK, rows affected (0.00 sec) Empty set (0.00 sec) //Lock information of session1.
---TRANSACTION , ACTIVE sec
lock struct(s), heap size , row lock(s)
MySQL thread id , OS thread handle , query id zlm1 192.168.56.100 zlm
TABLE LOCK table `zlm`.`t1` trx id lock mode IX
RECORD LOCKS space id page no n bits index PRIMARY of table `zlm`.`t1` trx id lock_mode X locks gap before rec
Record lock, heap no PHYSICAL RECORD: n_fields ; compact format; info bits
: len ; hex ; asc ;;
: len ; hex 0000002dbd6c; asc - l;;
: len ; hex ; asc G X ;;
: len ; hex ; asc ;;
: len ; hex ; asc ;;
: len ; hex ; asc ;; //Session1 holded a gap lock(or we can call it next-key lock either) of c1=8.Even though the record of c1=7 was not exist. //Session2:
zlm@192.168.56.100: [zlm]>begin;insert into t1 select ,,,;
Query OK, rows affected (0.00 sec) ERROR (HY000): Lock wait timeout exceeded; try restarting transaction //Lock information of session2.
---TRANSACTION , ACTIVE sec inserting
mysql tables in use , locked
LOCK WAIT lock struct(s), heap size , row lock(s)
MySQL thread id , OS thread handle , query id zlm1 192.168.56.100 zlm executing
insert into t1 select ,,,
------- TRX HAS BEEN WAITING SEC FOR THIS LOCK TO BE GRANTED:
RECORD LOCKS space id page no n bits index PRIMARY of table `zlm`.`t1` trx id lock_mode X locks gap before rec insert intention waiting
Record lock, heap no PHYSICAL RECORD: n_fields ; compact format; info bits
: len ; hex ; asc ;;
: len ; hex 0000002dbd6c; asc - l;;
: len ; hex ; asc G X ;;
: len ; hex ; asc ;;
: len ; hex ; asc ;;
: len ; hex ; asc ;; ------------------
TABLE LOCK table `zlm`.`t1` trx id lock mode IX
RECORD LOCKS space id page no n bits index PRIMARY of table `zlm`.`t1` trx id lock_mode X locks gap before rec insert intention waiting
Record lock, heap no PHYSICAL RECORD: n_fields ; compact format; info bits
: len ; hex ; asc ;;
: len ; hex 0000002dbd6c; asc - l;;
: len ; hex ; asc G X ;;
: len ; hex ; asc ;;
: len ; hex ; asc ;;
: len ; hex ; asc ;; //Session2 generated a insert intention lock and request for the next-key lock holded by session1.So it blocked.

Test 9: session1 insert while session2 select for update.(modify the same row)

 //Session1:
zlm@192.168.56.100: [zlm]>begin;insert into t1 select ,,,;
Query OK, rows affected (0.00 sec) Query OK, row affected (0.00 sec)
Records: Duplicates: Warnings: //Lock information of session1.
---TRANSACTION , ACTIVE sec
lock struct(s), heap size , row lock(s), undo log entries
MySQL thread id , OS thread handle , query id zlm1 192.168.56.100 zlm
TABLE LOCK table `zlm`.`t1` trx id lock mode IX //Session1 generated only a "IX" lock. //Session2:
zlm@192.168.56.100: [zlm]>begin;select * from t1 where c1= for update;
Query OK, rows affected (0.00 sec) ERROR (HY000): Lock wait timeout exceeded; try restarting transaction //Lock information of session2.
---TRANSACTION , ACTIVE sec starting index read
mysql tables in use , locked
LOCK WAIT lock struct(s), heap size , row lock(s)
MySQL thread id , OS thread handle , query id zlm1 192.168.56.100 zlm statistics
select * from t1 where c1= for update
------- TRX HAS BEEN WAITING SEC FOR THIS LOCK TO BE GRANTED:
RECORD LOCKS space id page no n bits index PRIMARY of table `zlm`.`t1` trx id lock_mode X locks rec but not gap waiting
Record lock, heap no PHYSICAL RECORD: n_fields ; compact format; info bits
: len ; hex ; asc ;;
: len ; hex 0000002dbdaa; asc - ;;
: len ; hex ee000001bd0110; asc ;;
: len ; hex ; asc ;;
: len ; hex ; asc ;;
: len ; hex ; asc ;; ------------------
TABLE LOCK table `zlm`.`t1` trx id lock mode IX
RECORD LOCKS space id page no n bits index PRIMARY of table `zlm`.`t1` trx id lock_mode X locks rec but not gap waiting
Record lock, heap no PHYSICAL RECORD: n_fields ; compact format; info bits
: len ; hex ; asc ;;
: len ; hex 0000002dbdaa; asc - ;;
: len ; hex ee000001bd0110; asc ;;
: len ; hex ; asc ;;
: len ; hex ; asc ;;
: len ; hex ; asc ;; //Lock information of session1.
---TRANSACTION , ACTIVE sec
lock struct(s), heap size , row lock(s), undo log entries
MySQL thread id , OS thread handle , query id zlm1 192.168.56.100 zlm
TABLE LOCK table `zlm`.`t1` trx id lock mode IX
RECORD LOCKS space id page no n bits index PRIMARY of table `zlm`.`t1` trx id lock_mode X locks rec but not gap
Record lock, heap no PHYSICAL RECORD: n_fields ; compact format; info bits
: len ; hex ; asc ;;
: len ; hex 0000002dbdaa; asc - ;;
: len ; hex ee000001bd0110; asc ;;
: len ; hex ; asc ;;
: len ; hex ; asc ;;
: len ; hex ; asc ;; //when session2 intended to update the same row,session1 generated a "X" record lock.Then session2 had to wait for the release of the lock by session1.It was blocked.That means InnoDB adds locks only if it detects multiple concurrent transactions are modifying the same rows no mater whether the record is exist or not.

Test 10: session1 select for update while session2 select for update.(modify the same row)

 //Session1:
zlm@192.168.56.100: [zlm]>begin;select * from t1 where c1= for update;
Query OK, rows affected (0.00 sec) Empty set (0.00 sec) //Lock information of session1.
---TRANSACTION , ACTIVE sec
lock struct(s), heap size , row lock(s)
MySQL thread id , OS thread handle , query id zlm1 192.168.56.100 zlm
TABLE LOCK table `zlm`.`t1` trx id lock mode IX
RECORD LOCKS space id page no n bits index PRIMARY of table `zlm`.`t1` trx id lock_mode X locks gap before rec
Record lock, heap no PHYSICAL RECORD: n_fields ; compact format; info bits
: len ; hex ; asc ;;
: len ; hex 0000002dbd6c; asc - l;;
: len ; hex ; asc G X ;;
: len ; hex ; asc ;;
: len ; hex ; asc ;;
: len ; hex ; asc ;; //Session2:
zlm@192.168.56.100: [zlm]>begin;select * from t1 where c1= for update;
Query OK, rows affected (0.00 sec) Empty set (0.00 sec) //Lock information of session2.
---TRANSACTION , ACTIVE sec
lock struct(s), heap size , row lock(s)
MySQL thread id , OS thread handle , query id zlm1 192.168.56.100 zlm
TABLE LOCK table `zlm`.`t1` trx id lock mode IX
RECORD LOCKS space id page no n bits index PRIMARY of table `zlm`.`t1` trx id lock_mode X locks gap before rec
Record lock, heap no PHYSICAL RECORD: n_fields ; compact format; info bits
: len ; hex ; asc ;;
: len ; hex 0000002dbd6c; asc - l;;
: len ; hex ; asc G X ;;
: len ; hex ; asc ;;
: len ; hex ; asc ;;
: len ; hex ; asc ;; //Session1 together with session2 both hold the gap lock(next-key lock) of c1=8.Because gap lock does not block each other.They are actually coexistent util there's a actual inserting operation.Therefore,session2 was not blocked in the case.
Summary
  • In RR transaction isolation level,the InnoDB locking seems more complicated than that in RC transaction isolation level.
  • InnoDB gap locks in differtent transactions are compatible only if the insert intention appears.
  • Generally,primary index and unique index does not generate gap locks like secondary index does.The exception is that when the condition contains a range of scanned indexes.The gap lock will appear according to the range of condition of query.
  • As for the "select ... for update" statement,if the records are in table,it adds "LOCK_REC_NOT_GAP"(secondary index is "LOCK_ORDINARY") otherwise it adds "LOCK_GAP".

InnoDB锁冲突案例演示(续)的更多相关文章

  1. InnoDB锁冲突案例演示

      Preface       As we know,InnoDB is index organized table.InnoDB engine supports row-level lock bas ...

  2. MySQL记录锁、间隙锁、临键锁小案例演示

    生成间隙(gap)锁.临键(next-key)锁的前提条件 是在 RR 隔离级别下. 有关Mysql记录锁.间隙(gap)锁.临键锁(next-key)锁的一些理论知识之前有写过,详细内容可以看这篇文 ...

  3. KingbaseES R6 集群主机锁冲突导致的主备切换案例

    ​ 案例说明: 主库在业务高峰期间,客户执行建表等DDL操作,主库产生"AccessExclusiveLock "锁,导致大量的事务产生锁冲突,大量的会话堆积,客户端session ...

  4. InnoDB锁机制分析

    InnoDB锁机制常常困扰大家,不同的条件下往往表现出不同的锁竞争,在实际工作中经常要分析各种锁超时.死锁的问题.本文通过不同条件下的实验,利用InnoDB系统给出的各种信息,分析了锁的工作机制.通过 ...

  5. [转载] 数据库分析手记 —— InnoDB锁机制分析

    作者:倪煜 InnoDB锁机制常常困扰大家,不同的条件下往往表现出不同的锁竞争,在实际工作中经常要分析各种锁超时.死锁的问题.本文通过不同条件下的实验,利用InnoDB系统给出的各种信息,分析了锁的工 ...

  6. MySQL- InnoDB锁机制

    InnoDB与MyISAM的最大不同有两点:一是支持事务(TRANSACTION):二是采用了行级锁.行级锁与表级锁本来就有许多不同之处,另外,事务的引入也带来了一些新问题.下面我们先介绍一点背景知识 ...

  7. MySQL InnoDB锁机制

    概述: 锁机制在程序中是最常用的机制之一,当一个程序需要多线程并行访问同一资源时,为了避免一致性问题,通常采用锁机制来处理.在数据库的操作中也有相同的问题,当两个线程同时对一条数据进行操作,为了保证数 ...

  8. 【锁】Innodb锁

    InnoDB与MyISAM的最大不同有两点:一是支持事务(TRANSACTION):二是采用了行级锁.行级锁与表级锁本来就有许多不同之处,另外,事务的引入也带来了一些新问题.下面我们先介绍一点背景知识 ...

  9. MySQL基础篇(06):事务管理,锁机制案例详解

    本文源码:GitHub·点这里 || GitEE·点这里 一.锁概念简介 1.基础描述 锁机制核心功能是用来协调多个会话中多线程并发访问相同资源时,资源的占用问题.锁机制是一个非常大的模块,贯彻MyS ...

随机推荐

  1. luogu P3950 部落冲突

    嘟嘟嘟 树剖板子题. #include<cstdio> #include<iostream> #include<algorithm> #include<cma ...

  2. GitBash初始目录的修改

    GitBash初始目录是定为到用户目录的,例如: 所以,每次打开都要手动调试到仓库所在的目录,可以通过修改目标和起始位置来定位到仓储文件夹下. 再次打开git,完美~~

  3. ASP.NET SingalR + MongoDB 实现简单聊天室(二):实现用户信息、聊天室初始化,聊天信息展示完善

    第一篇已经介绍了一大半了,下面就是详细业务了,其实业务部分要注意的地方有几个,剩下的就是js跟html互动处理. 首先在强调一下,页面上不可缺少的js:jquery,singalR.js,hubs . ...

  4. Spring时间(Date)类型转换+自定义

    第一种方法:利用内置的 CustomDateEditor(不推荐,每个类都需要定义) 首先,在我们的 Controller 的 InitBinder 里面,注册 CustomEditor //首先初始 ...

  5. VS2008 工具栏CMFCToolBar的使用总结(转)

    (一)自定义工具栏 自定义工具栏,分两种情况:一是直接添加工具栏,并自己绘制图标:二是,添加工具栏,然后与BMP关联,与VC6.0中的自定义彩色工具栏类似. 1.  自绘工具栏 1)添加Toolbar ...

  6. 教你使用 Reflexil 反编译.NET 转

    转自:http://www.wxzzz.com/711.html http://sebastien.lebreton.free.fr/reflexil/ http://www.aneasystone. ...

  7. iOS定时器-- NSTimer 和CADisplaylink

    iOS定时器-- NSTimer 和CADisplaylink 一.iOS中有两种不同的定时器: 1.  NSTimer(时间间隔可以任意设定,最小0.1ms)// If seconds is les ...

  8. ORA-04044: 此处不允许过程, 函数, 程序包或类型和

    用Orale代码建表时,出现 SQL> comment on column SCORE.cno 2 is '学号(外键)';comment on column SCORE.cno is '学号( ...

  9. 绘图驱动-OSD原理1

    OSD(On Screen Display)是屏幕显示技术的一种,用于在显示终端上显示字符.图形和图像.实现的过程为:存储器(一般为内存的一段)的内容与显示终端上的像素一一对应.这种一一对应的关系一般 ...

  10. 20181030noip模拟赛T1

    YY的矩阵 YY有一个大矩阵(N*M), 矩阵的每个格子里都有一个整数权值W[i,j](1<=i<=M,1<=j<=N) 对于这个矩阵YY会有P次询问,每次询问这个大矩阵的一个 ...