创建表结构

  1. CREATE TABLE `user_lock` (
  2. `id` int(11) NOT NULL AUTO_INCREMENT,
  3. `name` varchar(45) DEFAULT NULL,
  4. `age_idx` int(11) DEFAULT NULL,
  5. `age_uidx` int(11) DEFAULT NULL,
  6. `age_nidx` int(11) DEFAULT NULL,
  7. PRIMARY KEY (`id`),
  8. UNIQUE KEY `uidx_age` (`age_uidx`),
  9. KEY `idx_name` (`name`),
  10. KEY `idx_age` (`age_idx`),
  11. KEY `idx_name_age` (`name`,`age_idx`)
  12. ) ENGINE=InnoDB DEFAULT CHARSET=latin1;

初始化数据

  1. INSERT INTO `test_isolation`.`user_lock` (`name`, `age_idx`, `age_uidx`, `age_nidx`) VALUES ('10', 10, 10, 10);
  2. INSERT INTO `test_isolation`.`user_lock` (`name`, `age_idx`, `age_uidx`, `age_nidx`) VALUES ('20', 20, 20, 20);
  3. INSERT INTO `test_isolation`.`user_lock` (`name`, `age_idx`, `age_uidx`, `age_nidx`) VALUES ('30', 30, 30, 30);
  4. INSERT INTO `test_isolation`.`user_lock` (`name`, `age_idx`, `age_uidx`, `age_nidx`) VALUES ('40', 40, 40, 40);
  5. INSERT INTO `test_isolation`.`user_lock` (`name`, `age_idx`, `age_uidx`, `age_nidx`) VALUES ('50', 50, 50, 50);

T1 - 设置隔离级别

REPEATABLE READ

  1. SET SESSION TRANSACTION ISOLATION LEVEL REPEATABLE READ;
  2. select @@global.transaction_isolation,@@transaction_isolation;

image.png

T2 - 设置隔离级别

REPEATABLE READ

  1. SET SESSION TRANSACTION ISOLATION LEVEL REPEATABLE READ;
  2. select @@global.transaction_isolation,@@transaction_isolation;

image.png

T1 - 事务1开启,删除或更新多条记录

  1. start transaction;
  2. DELETE FROM test_isolation.user_lock where age_idx > 20 and age_idx < 40;

image.png
or

  1. start transaction;
  2. UPDATE test_isolation.user_lock SET age_idx = 0 WHERE age_idx > 20 and age_idx < 40;

image.png

T2 - 事务2受间隙锁影响的插入操作

image.png

允许范围

  1. start transaction;
  2. INSERT INTO test_isolation.user_lock (`name`, `age_idx`, `age_uidx`, `age_nidx`) VALUES ('9', 9, 9, 9);
  3. INSERT INTO test_isolation.user_lock (`name`, `age_idx`, `age_uidx`, `age_nidx`) VALUES ('10', 10, 100, 10);
  4. INSERT INTO test_isolation.user_lock (`name`, `age_idx`, `age_uidx`, `age_nidx`) VALUES ('11', 11, 11, 11);
  5. INSERT INTO test_isolation.user_lock (`name`, `age_idx`, `age_uidx`, `age_nidx`) VALUES ('19', 19, 19, 19);
  6. INSERT INTO test_isolation.user_lock (`name`, `age_idx`, `age_uidx`, `age_nidx`) VALUES ('40', 40, 400, 40);
  7. INSERT INTO test_isolation.user_lock (`name`, `age_idx`, `age_uidx`, `age_nidx`) VALUES ('41', 41, 41, 41);
  8. INSERT INTO test_isolation.user_lock (`name`, `age_idx`, `age_uidx`, `age_nidx`) VALUES ('49', 49, 49, 49);
  9. INSERT INTO test_isolation.user_lock (`name`, `age_idx`, `age_uidx`, `age_nidx`) VALUES ('50', 50, 500, 50);
  10. INSERT INTO test_isolation.user_lock (`name`, `age_idx`, `age_uidx`, `age_nidx`) VALUES ('51', 51, 51, 51);

image.png

锁的范围

  1. INSERT INTO test_isolation.user_lock (`name`, `age_idx`, `age_uidx`, `age_nidx`) VALUES ('20', 20, 200, 20);
  2. INSERT INTO test_isolation.user_lock (`name`, `age_idx`, `age_uidx`, `age_nidx`) VALUES ('21', 21, 21, 21);
  3. INSERT INTO test_isolation.user_lock (`name`, `age_idx`, `age_uidx`, `age_nidx`) VALUES ('29', 29, 29, 29);
  4. INSERT INTO test_isolation.user_lock (`name`, `age_idx`, `age_uidx`, `age_nidx`) VALUES ('30', 30, 300, 30);
  5. INSERT INTO test_isolation.user_lock (`name`, `age_idx`, `age_uidx`, `age_nidx`) VALUES ('31', 31, 31, 31);
  6. INSERT INTO test_isolation.user_lock (`name`, `age_idx`, `age_uidx`, `age_nidx`) VALUES ('39', 39, 39, 39);

image.png

T2 - 事务2受行锁影响的更新操作操作

允许范围

  1. UPDATE test_isolation.user_lock SET name = '10', age_idx = 10, age_nidx = 10 WHERE age_idx = 10;
  2. UPDATE test_isolation.user_lock SET name = '20', age_idx = 20, age_nidx = 20 WHERE age_idx = 20;
  3. UPDATE test_isolation.user_lock SET name = '50', age_idx = 50, age_nidx = 50 WHERE age_idx = 50;

image.png

锁的范围

  1. UPDATE test_isolation.user_lock SET name = '30', age_idx = 30, age_nidx = 30 WHERE age_idx = 30;
  2. UPDATE test_isolation.user_lock SET name = '40', age_idx = 40, age_nidx = 40 WHERE age_idx = 40;

image.png

T2 - 事务2受行锁影响的删除操作

允许范围

  1. DELETE FROM test_isolation.user_lock WHERE age_idx = 10;
  2. DELETE FROM test_isolation.user_lock WHERE age_idx = 20;
  3. DELETE FROM test_isolation.user_lock WHERE age_idx = 50;

image.png

锁的范围

  1. DELETE FROM test_isolation.user_lock WHERE age_idx = 30;
  2. DELETE FROM test_isolation.user_lock WHERE age_idx = 40;

image.png

T1 - 事务1提交事务释放间隙锁

  1. COMMIT

image.png

T2 - 事务2不再受间隙锁影响了

  1. INSERT INTO test_isolation.user_lock (`name`, `age_idx`, `age_uidx`, `age_nidx`) VALUES ('20', 20, 200, 20);
  2. INSERT INTO test_isolation.user_lock (`name`, `age_idx`, `age_uidx`, `age_nidx`) VALUES ('21', 21, 21, 21);
  3. INSERT INTO test_isolation.user_lock (`name`, `age_idx`, `age_uidx`, `age_nidx`) VALUES ('29', 29, 29, 29);
  4. INSERT INTO test_isolation.user_lock (`name`, `age_idx`, `age_uidx`, `age_nidx`) VALUES ('30', 30, 300, 30);
  5. INSERT INTO test_isolation.user_lock (`name`, `age_idx`, `age_uidx`, `age_nidx`) VALUES ('31', 31, 31, 31);
  6. INSERT INTO test_isolation.user_lock (`name`, `age_idx`, `age_uidx`, `age_nidx`) VALUES ('39', 39, 39, 39);
  7. UPDATE test_isolation.user_lock SET name = '30', age_idx = 30, age_nidx = 30 WHERE age_idx = 30;
  8. UPDATE test_isolation.user_lock SET name = '40', age_idx = 40, age_nidx = 40 WHERE age_idx = 40;
  9. DELETE FROM test_isolation.user_lock WHERE age_idx = 30;
  10. DELETE FROM test_isolation.user_lock WHERE age_idx = 40;
  11. COMMIT

image.png