SQL SERVER之事务
时间:2022-03-10 17:17
begin try
begin transaction
--使用try语句进行捕捉错误
insert into accounttable values(5000,'A')
save tran a1
insert into accounttable values(5000,'C')
commit
end try
begin catch
--当发生错误时,进行回滚
rollback tran a1
end catch
<span style="font-family:SimSun;font-size:18px;">begin tran
update table set money=103 where name='A'
waitfor delay '00:00:10' --等待10秒
update table set money=104 where name='A'
commit tran</span>
<span style="font-family:SimSun;font-size:18px;">set transaction isolation level read uncommitted
begin tran
select money from table where name='A'
commit tran</span>
<span style="font-family:SimSun;font-size:18px;">set transaction isolation level 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
</span>
<span style="font-family:SimSun;font-size:18px;">begin tran
update table set money=10 where name='A'
commit tran</span>
<span style="font-family:SimSun;font-size:18px;">begin tran
select * from table
waitfor delay '00:00:10'--等待10秒
select * from table
commit tran</span>
<span style="font-family:SimSun;font-size:18px;">begin tran
insert into table values(300,'a')
commit tran</span>
我们发现两次查询的结果不一样,这就是典型的”幻象读”问题,可知解决方法为把隔离级别设置为SERIALLZABLE即可。
小结:在实际应用的时候,采用何种隔离级别应视具体情况而定。
SQL SERVER之事务,布布扣,bubuko.com