InnoDB锁冲突案例演示(续)
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.
- 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锁冲突案例演示(续)的更多相关文章
- InnoDB锁冲突案例演示
Preface As we know,InnoDB is index organized table.InnoDB engine supports row-level lock bas ...
- MySQL记录锁、间隙锁、临键锁小案例演示
生成间隙(gap)锁.临键(next-key)锁的前提条件 是在 RR 隔离级别下. 有关Mysql记录锁.间隙(gap)锁.临键锁(next-key)锁的一些理论知识之前有写过,详细内容可以看这篇文 ...
- KingbaseES R6 集群主机锁冲突导致的主备切换案例
案例说明: 主库在业务高峰期间,客户执行建表等DDL操作,主库产生"AccessExclusiveLock "锁,导致大量的事务产生锁冲突,大量的会话堆积,客户端session ...
- InnoDB锁机制分析
InnoDB锁机制常常困扰大家,不同的条件下往往表现出不同的锁竞争,在实际工作中经常要分析各种锁超时.死锁的问题.本文通过不同条件下的实验,利用InnoDB系统给出的各种信息,分析了锁的工作机制.通过 ...
- [转载] 数据库分析手记 —— InnoDB锁机制分析
作者:倪煜 InnoDB锁机制常常困扰大家,不同的条件下往往表现出不同的锁竞争,在实际工作中经常要分析各种锁超时.死锁的问题.本文通过不同条件下的实验,利用InnoDB系统给出的各种信息,分析了锁的工 ...
- MySQL- InnoDB锁机制
InnoDB与MyISAM的最大不同有两点:一是支持事务(TRANSACTION):二是采用了行级锁.行级锁与表级锁本来就有许多不同之处,另外,事务的引入也带来了一些新问题.下面我们先介绍一点背景知识 ...
- MySQL InnoDB锁机制
概述: 锁机制在程序中是最常用的机制之一,当一个程序需要多线程并行访问同一资源时,为了避免一致性问题,通常采用锁机制来处理.在数据库的操作中也有相同的问题,当两个线程同时对一条数据进行操作,为了保证数 ...
- 【锁】Innodb锁
InnoDB与MyISAM的最大不同有两点:一是支持事务(TRANSACTION):二是采用了行级锁.行级锁与表级锁本来就有许多不同之处,另外,事务的引入也带来了一些新问题.下面我们先介绍一点背景知识 ...
- MySQL基础篇(06):事务管理,锁机制案例详解
本文源码:GitHub·点这里 || GitEE·点这里 一.锁概念简介 1.基础描述 锁机制核心功能是用来协调多个会话中多线程并发访问相同资源时,资源的占用问题.锁机制是一个非常大的模块,贯彻MyS ...
随机推荐
- MongoDB的角色作用(1)
MongoDB的角色作用: 经过大量血的教训,一个分片配置两个副本集时(一个是primary一个是secondary),如果primary挂掉,secondary是不会升级的,必须要加上一个不存储数据 ...
- java字符串类型和时间类型的转换
类型转换 //reqeust.getParameter获取字符串直接赋值 1 public static Date date(String date_str) { try { Calendar zca ...
- 使用jQuery实现伪分页
在之前的项目中遇到一个这样的问题,页面分为上下两部分(分别称为未选中设备信息部分和选中设备信息部分),上面是从数据库拿出来的所有的设备信息,下面是显式已选中的设备信息,页面如下所示: 可以选中其中的任 ...
- 关于session序列化和session钝化和活化
在第一次启动服务器后,在session中放入一个对象.在页面可以获得,当重启服务器,但是没有关闭浏览器的情况下刷新页面仍然能够获得这个对象,前提是这个对象必须实现了java.io.Serializab ...
- sudo命令: 在其他用户下操作root用户权限
一. 场景: 在某个远程服务器 A 上,用 账户1 登陆, 想要在root用户的目录下创建一个 .sh文件, 如果直接 用 touch test.sh 创建,会提示权限不足 此时可以用sudo命令: ...
- Spring知识点小结(二)
一.配置非自定义的Bean(数据源DataSource模型) DBCP数据源: 导入dbcp的jar包:dbcp+pool+connector 代码实现: ...
- C语言的乱七八糟
Note For C Linux下C编程基础(gcc/gdb/make使用) 一.vi学习 二.初探emacs 三.gcc编译器 3.1 gcc所支持后缀名解释 后缀名 解释 后缀名 解释 .c C原 ...
- Linux 实时查看进程网络的使用情况
一行代码实现 linux 指定进程网络的使用情况 pid=4203;count=0;while true;do info2=`sed -n '4,100p' /proc/$pid/net/dev |a ...
- LogViewer超大文本浏览工具
官方下载 LogViewer 是一款简单好用的log日志文件查看工具.您想要查看log日志吗?那么不妨来看看这款LogViewer .该款工具可以在短短数秒内打开上G的LOG文件,支持高亮某行文字(例 ...
- LAMP+Varnish的实现
基于Keepalived+Varnish+Nginx实现的高可用LAMP架构 注意:各节点的时间需要同步(ntpdate ntp1.aliyun.com),关闭firewalld(systemctl ...