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. PHP----预定义数组

    预定义数组    超全局数组 <?php  /* 预定义数组:  * 自动全局变量---超全局数组  *  * 1.包含了来自WEB服务器,客户端,运行环境和用户输入的数据  * 2.这些数组比 ...

  2. tp3.2上一篇下一篇功能

    1. 后台 //上一页 $map1['a_id'] = array('gt',$a_id); $map1['cate_id'] = array('eq',$cate_id); $front=$arc- ...

  3. css3的代替图片的三角形

    1.小三角形(与边框结合,不兼容IE8) .callout{ position: relative; width: 100px; height: 100px; background: #fce6ed; ...

  4. Windows 下制作CentOS7安装U盘

    本文属于另类的U盘制作方法(更多U盘安装见U盘安装CentOS ),如何安装CentOS,请参考<安装指南> 以下列出了,完整的制作步骤: 1.下载安装镜像 选择一个合适的镜像网站,比如网 ...

  5. PAT——年会抽奖(错位 排序)

    题目描述 今年公司年会的奖品特别给力,但获奖的规矩却很奇葩: 1. 首先,所有人员都将一张写有自己名字的字条放入抽奖箱中:2. 待所有字条加入完毕,每人从箱中取一个字条:3. 如果抽到的字条上写的就是 ...

  6. input输入框中list属性不能没有效果

    网页代码入下,就是出不来效果. 人物:zslsww 解决办法: 将option中的value属性去掉: 张山李四王五爪留 显示效果,问题得到解决.

  7. 【oracle】关于创建表时用default指定默认值的坑

    刚开始学create table的时候没注意,学到后面发现可以指定默认值.于是写了如下语句: 当我查询的时候发现,查出来的结果是这样的.. 很纳闷有没有,我明明指定默认值了呀,为什么创建出来的表还是空 ...

  8. python中 的继承

    1.Python的类可以继承多个类,Java和C#中则只能继承一个类. 2.Python的类如果继承了多个类,那么其寻找方法的方式有两种,分别是:深度优先和广度优先. 当类是经典类时,多继承情况下,会 ...

  9. 『ACM C++』 PTA 天梯赛练习集L1 | 036-037

    这几天比较忙,所以随便做做水题了,得赶紧把英剧搞完啊啊啊啊啊啊 ------------------------------------------------L1-036-------------- ...

  10. ubuntu—终端安装mysql

    ---恢复内容开始--- Step 1 : 安装指令 ~$ sudo apt-get install mysql-server Step 2: 查看是否正常安装 ~$ ps aux | grep my ...