1.ru(READ-UNCOMMITED 未提交读) Ru级别造成了脏读: session2可以读取到session1的没有提交的事务的数据(内存中没有提交的脏页). 脏读的发生至少在RU下,而目前几乎所有的数据库几乎都在RC级以上的隔离界别上。 脏读实例: 两个session修改隔离级别: set session transaction isolation level read uncommitted; session1 session2 begin; begin; insert into t1 values(null,'testru') select * from test1.t1;#查询到了testru数据,造成了脏读 commit; commit; ######################################################## 2.rc(read committed 已提交读) Rc级别解决了脏读.但会造成不可重复读和幻读.两者发生方式一样,但由于解决方法不同而区分. 解决了脏读实例:(针对select.) 修改隔离级别: Set session transaction isolation level read committed; session1 session2 begin; begin; insert into test1.t1 values(null,'testrc'); select * from test1.t1;#没有查到 commit; select * from test1.t1; 查询到了 commit; ################################################################ 不可重复读:针对于update/delete来说. 幻读:针对于insert来说的. 幻读实例: Set session transaction isolation level read committed; ##################################################### session1 session2 begin; begin; select * from test1.t1;--7条 insert into t1 values() commit; select * from test1.t1 --8 commit; ##################################################### 不可重复读实例: Set session transaction isolation level read committed; session1 session2 begin; begin; select * from test1.t1 where name=’aa’;--找到1条 Update t1 set name=’bb’ where name=’aa’; commit; select * from test1.t1 where name=’aa’ --没有找到. commit; ################################################################### 加锁实例1:(加锁只针对指定的行,不影响update/insert操作) Set session transaction isolation level read committed; session1 session2 begin; begin; update test1.t1 set name='bbb' ; insert into test1.t1 values(null,'test_rr') 加了锁,但插入成功. commit; select * from test1.t1 1000w+1 Commit; ############################################################################# 3.rr(repeatable read 可重复读) RR隔离级别:可重复读 .repeatable read 在RR模式下GAP_LOCK是默认开启的. 避免幻读实例:(insert) set session transaction isolation level repeatable read; session1 session2 begin; begin; select * from test1.t1; insert into t1 values(null,'testrr'); commit; select * from test1.t1;查询不到testrr commit; select * from test1.t1;查询到testrr #################################################### 避免不可重复读实例:(update /delete) set session transaction isolation level repeatable read; session1 session2 begin; begin; select * from test1.t1; update test1.t1 set name='testrr_upd' where name='testrr' commit; select * from test1.t1;查询不到testrr_upd commit; select * from test1.t1;查询到testrr_upd ################################################################### 锁等待实例:(一定会锁,update后,除了select,其他操作如insert/update都会被锁.) set session transaction isolation level repeatable read; session1 session2 begin; begin; update test1.t1 set name='ccc'; insert into test1.t1 values(null,'test_rr')插入被阻塞,进入锁等待状态 commit; commit; select * from test1.t1 1000w+1 ################################################################### |