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. HDU 6386 Age of Moyu 【BFS + 优先队列优化】

    任意门:http://acm.hdu.edu.cn/showproblem.php?pid=6386 Age of Moyu Time Limit: 5000/2500 MS (Java/Others ...

  2. ASP.NET Web API编程——文件上传

    首先分别介绍正确的做法和错误的做法,然后分析他们的不同和错误之处,以便读者在实现此功能时可避开误区 1正确的做法 public class AvaterController : BaseApiCont ...

  3. 记录一次Git问题及其解决方案

    错误信息:fatal: refusing to merge unrelated histories 错误产生背景:我将原先测试的项目本地删除后提交,然后将新的项目按照git的提交步骤进行提交,在最后一 ...

  4. 十七、IntelliJ IDEA 中的 Maven 项目初体验及搭建 Spring MVC 框架

    我们已经将 IntelliJ IDEA 中的 Maven 项目的框架搭建完成.接着上文,在本文中,我们更近一步,利用 Tomcat 运行我们的 Web 项目. 如上图所示,我们进一步扩展了项目的结构, ...

  5. 【题解】洛谷P4180 [BJWC2010] 严格次小生成树(最小生成树+倍增求LCA)

    洛谷P4180:https://www.luogu.org/problemnew/show/P4180 前言 这可以说是本蒟蒻打过最长的代码了 思路 先求出此图中的最小生成树 权值为tot 我们称这棵 ...

  6. 【Git】Git使用小结

    Git与SVN及TFS这类传统的版本管理的区别: 本地机器也会有分支.代码库的概念 SVN常用的做法是每次写一些代码就提交到仓库,但是Git是先提交到本地(commit),然后当本地有个稳定的版本的时 ...

  7. 微信小程序,搜索结果关键词高亮 wxml不能动态识别html标签

    wxml中使用rich-text标签放置动态html标签 js:

  8. 『ACM C++』 PTA 天梯赛练习集L1 | 034-035

    在一个团队里,一群人一起为一件事情努力奋斗的过程,真的很值得享受,真希望我能拥有很多这样的团队. ------------------------------------------------L1- ...

  9. tomcat启动startup.bat一闪而过【亲测有效】

    遇到很多次运行startup.bat后,一个窗口一闪而过的问题,但是从来没去纠正怎样修改配置才是正确的,现在从网上查阅的资料整理如下:tomcat在启动时,会读取环境变量的信息,需要一个CATALIN ...

  10. CentOS7 使用chrony搭建集群中的时间同步服务

    一.集群环境: 系统:CentOS7-minimal 集群中的两台主机ip:10.132.226.103/24  10.132.226.104/24 二.CentOS7中时间相关命令timedatec ...