sql server - set xact_abort on and try catch together -
i have try catch block in sp insert statement in try. catch check error code if pk violation, if update. times "the current transaction cannot committed , cannot support operations write log file. roll transaction.
uncommittable transaction detected @ end of batch. transaction rolled back." added xact_abort on, keep getting "transaction count after execute indicates mismatching number of begin , commit statements." , found this. http://www.ashishsheth.com/post/2009/08/14/set-xact_abort-on-and-trycatch-block-in-sql-server-2005.aspx
if true. catch code not run if there error in try block xact_abort on?
it not true, @ least sql server 2008, set xact_abort on cause error skip catch block:
here code tried using northwind database
set xact_abort off begin try select 1, @@trancount begin tran update [dbo].[categories] set description='blah' [categoryid]=2 select 2, @@trancount select 1/0 whoops commit select 3, @@trancount end try begin catch select 'in catch. error occured', 4, @@trancount if (xact_state()) = 0 begin select n'there no transaction' end; if (xact_state()) = -1 begin select n'the transaction in uncommittable state.' + 'rolling transaction.' rollback transaction; end; -- test whether transaction committable. if (xact_state()) = 1 begin select n'the transaction committable.' + 'committing transaction.' commit transaction; end; end catch
this will, obviously, force error when hits select 1/0 statement. set xact_abort off, when catch block reached, value returned xact_state() function 1, causing code run commits transaction. when set xact_abort on, value returned, in catch block -1 code rolls transaction executed.
this based on:
Comments
Post a Comment