创建表结构

  1. CREATE TABLE `user` (
  2. `id` int(11) NOT NULL AUTO_INCREMENT,
  3. `name` varchar(45) DEFAULT NULL,
  4. `age` int(11) DEFAULT NULL,
  5. `text` varchar(45) DEFAULT NULL,
  6. PRIMARY KEY (`id`),
  7. KEY `idx_name` (`name`),
  8. KEY `idx_age` (`age`),
  9. KEY `idx_name_age` (`name`,`age`)
  10. ) ENGINE=InnoDB AUTO_INCREMENT=7 DEFAULT CHARSET=latin1;

初始化数据

  1. INSERT INTO `test_isolation`.`user` (`name`, `age`, `text`) VALUES ('a', '10', 'a');
  2. INSERT INTO `test_isolation`.`user` (`name`, `age`, `text`) VALUES ('b', '20', 'b');
  3. INSERT INTO `test_isolation`.`user` (`name`, `age`, `text`) VALUES ('c', '30', 'c');
  4. INSERT INTO `test_isolation`.`user` (`name`, `age`, `text`) VALUES ('d', '40', 'd');
  5. INSERT INTO `test_isolation`.`user` (`name`, `age`, `text`) VALUES ('e', '50', 'e');

T1 - 设置隔离级别

READ UNCOMMITTED

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

image.png

T2 - 查看隔离级别

image.png

T1 - 事务1开启

T1第一次查询

  1. start transaction;
  2. SELECT * FROM test_isolation.user;

image.png

T2 - 事务2开启

插入一条没有提交的数据F

  1. start transaction;
  2. INSERT INTO test_isolation.user (`name`, `age`, `text`) VALUES ('f', 60, 'f');

image.png

T1 - 第2次查询

事务1读到了事务2尚未提交的数据F,事务T1发生了脏读(当前事务读取到了其他事务还未曾提交的数据)

  1. SELECT * FROM test_isolation.user;

image.png

T2 - 事务2回滚

事务2回滚插入的数据F
image.png

T1 - 第3次查询

事务T2回滚的数据又消失了
image.png