创建表结构

  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. select * FROM test_isolation.user_lock where age_idx = 20 for update;

image.png

T2 - 事务2受行锁影响的查询操作

允许范围

  1. start transaction;
  2. SELECT * FROM test_isolation.user_lock where age_idx = 9;
  3. SELECT * FROM test_isolation.user_lock where age_idx = 10;
  4. SELECT * FROM test_isolation.user_lock where age_idx = 11;
  5. SELECT * FROM test_isolation.user_lock where age_idx = 19;
  6. SELECT * FROM test_isolation.user_lock where age_idx = 20;
  7. SELECT * FROM test_isolation.user_lock where age_idx = 21;
  8. SELECT * FROM test_isolation.user_lock where age_idx = 29;
  9. SELECT * FROM test_isolation.user_lock where age_idx = 30;
  10. SELECT * FROM test_isolation.user_lock where age_idx = 31;

image.png

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

image.png

允许范围

  1. INSERT INTO test_isolation.user_lock (`name`, `age_idx`, `age_uidx`, `age_nidx`) VALUES ('9', 9, 9, 9);
  2. INSERT INTO test_isolation.user_lock (`name`, `age_idx`, `age_uidx`, `age_nidx`) VALUES ('30', 30, 300, 30);
  3. INSERT INTO test_isolation.user_lock (`name`, `age_idx`, `age_uidx`, `age_nidx`) VALUES ('31', 31, 31, 31);
  4. INSERT INTO test_isolation.user_lock (`name`, `age_idx`, `age_uidx`, `age_nidx`) VALUES ('39', 39, 39, 39);
  5. INSERT INTO test_isolation.user_lock (`name`, `age_idx`, `age_uidx`, `age_nidx`) VALUES ('40', 40, 400, 40);
  6. INSERT INTO test_isolation.user_lock (`name`, `age_idx`, `age_uidx`, `age_nidx`) VALUES ('41', 41, 41, 41);
  7. INSERT INTO test_isolation.user_lock (`name`, `age_idx`, `age_uidx`, `age_nidx`) VALUES ('49', 49, 49, 49);
  8. INSERT INTO test_isolation.user_lock (`name`, `age_idx`, `age_uidx`, `age_nidx`) VALUES ('50', 50, 500, 50);
  9. 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 ('10', 10, 100, 10);
  2. INSERT INTO test_isolation.user_lock (`name`, `age_idx`, `age_uidx`, `age_nidx`) VALUES ('11', 11, 11, 11);
  3. INSERT INTO test_isolation.user_lock (`name`, `age_idx`, `age_uidx`, `age_nidx`) VALUES ('19', 19, 19, 19);
  4. INSERT INTO test_isolation.user_lock (`name`, `age_idx`, `age_uidx`, `age_nidx`) VALUES ('20', 20, 200, 20);
  5. INSERT INTO test_isolation.user_lock (`name`, `age_idx`, `age_uidx`, `age_nidx`) VALUES ('21', 21, 21, 21);
  6. INSERT INTO test_isolation.user_lock (`name`, `age_idx`, `age_uidx`, `age_nidx`) VALUES ('29', 29, 29, 29);

image.png

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

允许范围

  1. UPDATE test_isolation.user_lock SET name = 'a', age_idx = 10, age_nidx = 10 WHERE age_idx = 10;
  2. UPDATE test_isolation.user_lock SET name = '11', age_idx = 11, age_nidx = 11 WHERE age_idx = 11;
  3. UPDATE test_isolation.user_lock SET name = '19', age_idx = 19, age_nidx = 19 WHERE age_idx = 19;
  4. UPDATE test_isolation.user_lock SET name = '21', age_idx = 21, age_nidx = 21 WHERE age_idx = 21;
  5. UPDATE test_isolation.user_lock SET name = '29', age_idx = 29, age_nidx = 29 WHERE age_idx = 29;
  6. UPDATE test_isolation.user_lock SET name = 'c', age_idx = 30, age_nidx = 30 WHERE age_idx = 30;

image.png

锁的范围

  1. UPDATE test_isolation.user_lock SET name = '20', age_idx = 20, age_nidx = 20 WHERE age_idx = 20;

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 = 11;
  3. DELETE FROM test_isolation.user_lock WHERE age_idx = 19;
  4. DELETE FROM test_isolation.user_lock WHERE age_idx = 21;
  5. DELETE FROM test_isolation.user_lock WHERE age_idx = 29;
  6. DELETE FROM test_isolation.user_lock WHERE age_idx = 30;

image.png

锁的范围

  1. DELETE FROM test_isolation.user_lock WHERE age_idx = 20;

image.png

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

image.png

T2 - 事务2不受间隙锁和行锁的影响

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

image.png