read uncommitted(未提交读)设置隔离级别:set session transaction isolation level read uncommitted;
A启动事务:start transaction;
B第一次查询:select * from test where id = 1; 结论:1
3. A修改:update test set num = 10 where id = 1;
B第二次查询查询:select * from test where id = 1; 结论:10A执行回滚:rollback;
B第三次查询:select * from test where id = 1; 结论:1
你会看到:B事务三次查询结果不一致:1-10-1,这就是所谓的脏读。
read committed(已提交读)设置隔离级别:set session transaction isolation level read committed;
A启动事务:start transaction;
B第一次查询:select * from test where id = 1; 结论:1
3. A修改:update test set num = 10 where id = 1;
B第二次查询:select * from test where id = 1; 结论:1
4. A提交事务,commit
B第二次查询:select * from test where id = 1; 结论:10
你会看到:B事务三次查询结果不一致:1-1-10,这就是所谓的幻读。
为什么同样是两次读到的结果不一样,第一种叫做脏读,第二种叫做幻读?第一种是所谓的1-10-1,第二种是所谓的1-1-10