transaction -- save point

begin  tran tran1
select @@trancount
if exists(select 1 from version where trader = 'lg39304')
update version set version = '2.0.40.1' where trader = 'lg39304' and application = 'SafeSQL.AdHocEditor'
save tran tran2
select @@trancount
update version set version = '2.0.30.1' where trader = 'lg39304' and application = 'SafeSQL.AdHocEditor'
rollback tran tran2
select @@trancount

--Save tran createa a save point to which current transaction can rollback, but it does not actually increment the @@trancount,
--"rollback tran tran2" rollback the transaction to the save point, so the version is updated to 2.0.40.1 not 2.0.30.1.
--If above sql script executed, then the tran1 is always opened, so version table is occupied by this transaction,
so any other operation to version table will not take efffect as version table is locked.


commit tran
select @@trancount

sp_transactions

--@@trancount is not equal to 0, then there must be a resource be occupied by a certain process,
so this resoure won't be used any longer until the tran rollback/committed.
So do we need to check the @@trancount before we use a certain resource(table, view..)?
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章