当前位置:网站首页>MySQL transaction isolation level experiment

MySQL transaction isolation level experiment

2022-06-13 03:18:00 flyweight_ visitor

Quick start

command

  1. Connect to database
mysql -uroot -p
mysql> use testdb
  1. Prepare the data
mysql> CREATE TABLE child (id int(11) NOT NULL, PRIMARY KEY(id)) ENGINE=InnoDB;
mysql> INSERT INTO child (id) values (90),(102);
  1. View the current database isolation level
SELECT @@global.tx_isolation;

set global transaction isolation level read committed;
set global transaction isolation level repeatable read;
  1. conversation 1
mysql> START TRANSACTION;
mysql> SELECT * FROM child WHERE id > 100 FOR UPDATE;
  1. Conversation two
mysql> START TRANSACTION;
mysql> INSERT INTO child (id) VALUES (101);
  1. Conversation 3 ( View lock wait )
SELECT   waiting_trx_id,   waiting_pid,   waiting_query,   blocking_trx_id,   blocking_pid,   blocking_query FROM sys.innodb_lock_waits;

 Insert picture description here

SELECT
  r.trx_id waiting_trx_id,
  r.trx_mysql_thread_id waiting_thread,
  r.trx_query waiting_query,
  b.trx_id blocking_trx_id,
  b.trx_mysql_thread_id blocking_thread,
  b.trx_query blocking_query
FROM       information_schema.innodb_lock_waits w
INNER JOIN information_schema.innodb_trx b
  ON b.trx_id = w.blocking_trx_id
INNER JOIN information_schema.innodb_trx r
  ON r.trx_id = w.requesting_trx_id;
  1. conversation 1
mysql> SELECT * FROM child WHERE id > 100 FOR UPDATE;

 Insert picture description here

mysql> SELECT * FROM child WHERE id > 100;

 Insert picture description here
8. conversation 3

mysql> SELECT trx_id, trx_state, trx_started, trx_wait_started, trx_mysql_thread_id, trx_rows_locked,  trx_isolation_level FROM information_schema.innodb_trx;

 Insert picture description here

mysql> SELECT * FROM information_schema.innodb_locks;

 Insert picture description here

mysql> SELECT * FROM information_schema.innodb_lock_waits;

 Insert picture description here

mysql> SHOW ENGINE INNODB STATUS;

 Insert picture description here

Reference material

  1. Talk about MVCC and Next-key Locks
  2. InnoDB Locking
  3. Using InnoDB Transaction and Locking Information
原网站

版权声明
本文为[flyweight_ visitor]所创,转载请带上原文链接,感谢
https://yzsam.com/2022/02/202202280532045139.html