MySql事务隔离级别-未提交读(READ UNCOMMITTED)

准备工作

DROP TABLE IF EXISTS `t1`;
CREATE TABLE `t1` (
  `id` int(11) NOT NULL,
  `num` int(11) DEFAULT NULL,
  PRIMARY KEY (`id`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8;

执行步骤

  SESSION A SESSION B
T1 SET SESSION TRANSACTION ISOLATION LEVEL READ UNCOMMITTED;  
T2 START TRANSACTION;  
T3   SET SESSION TRANSACTION ISOLATION LEVEL READ UNCOMMITTED;
T4   START TRANSACTION;
T5

SELECT * FROM t1;

-- 结果为空

 
T6   INSERT INTO `t1` VALUES (1,  100);
T7

SELECT * FROM t1;

-- 结果不为空,查询到了T6时刻添加的数据

 
T8   ROLLBACK;
T9

SELECT * FROM t1;

-- 结果为空

 

 

问题所在:SESSION A在 T7 时刻,发生了脏读,读取到了SESSION B未提交的数据。

 

SQL解释

-- 设置会话隔离级别
SET SESSION TRANSACTION ISOLATION LEVEL READ UNCOMMITTED;

-- 开启事务
START TRANSACTION;

-- 提交事务
COMMIT;

-- 回滚事务
ROLLBACK;


 

 

 

 

 

你可能感兴趣的:(MySQL,事务)