创建表结构

  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 and age_idx < 40 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;
  11. SELECT * FROM test_isolation.user_lock where age_idx = 39;
  12. SELECT * FROM test_isolation.user_lock where age_idx = 40;
  13. SELECT * FROM test_isolation.user_lock where age_idx = 41;
  14. SELECT * FROM test_isolation.user_lock where age_idx = 49;
  15. SELECT * FROM test_isolation.user_lock where age_idx = 50;

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 ('10', 10, 100, 10);
  3. INSERT INTO test_isolation.user_lock (`name`, `age_idx`, `age_uidx`, `age_nidx`) VALUES ('11', 11, 11, 11);
  4. INSERT INTO test_isolation.user_lock (`name`, `age_idx`, `age_uidx`, `age_nidx`) VALUES ('19', 19, 19, 19);
  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 ('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 = '20', age_idx = 20, age_nidx = 20 WHERE age_idx = 20;

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 = 20;

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