SQLSERVER之事务(二)

2015-01-21 11:32:35 · 作者: · 浏览: 10
read committed --或者是set transaction isolation level read uncommitted begin tran select money from table where name='A' waitfor delay '00:00:10' --等待10秒 select money from table where name='A' commit tran

接着马上执行第二个连接语句

begin tran
update table set money=10 where name='A'
commit tran

我们发现第一个连接中两次返回账号的余额不一样,第一次是100,第二次是10,这就是典型的“非重复读”的问题

根据上表所示,如果把事务的隔离级别设置为REPEATABLE READ或者SERIALLZABLE可以防止此类问题

幻象读

根据上表所示,当事务的隔离级别为READ COMMITTED、READ UNCOMMITTED、REPEATABLE READ时就会发生幻象

先看下面的例子(账户余额为100)

第一个连接语句

begin tran
select * from table
waitfor delay '00:00:10'--等待10秒
select * from table
commit tran

接着马上执行第二个连接语句

begin tran
insert into table values(300,'a')
commit tran?

我们发现两次查询的结果不一样,这就是典型的”幻象读”问题,可知解决方法为把隔离级别设置为SERIALLZABLE即可。

小结:在实际应用的时候,采用何种隔离级别应视具体情况而定。