Monday, November 12, 2012

Doomed(uncommittable) Transaction

Doomed transactions are introduced with TRY/CATCH implementation introduced since SQL Server 2005. When it happens, the request cannot commit the transaction or roll back to a savepoint; it can only request a full rollback of the transaction.


XACT_STATE() can be used to detect the situation. And it's suggested to always check its value in CATCH block.

XACT_STATE():
1: has active user transaction.@@TRANCOUNT>0
0: no active user transaction. @@TRANCOUNT=0
-1: Doomed transaction , need rollback

BEGIN TRAN
BEGIN TRY
--or use raiserror function
select convert(int,'abc')
commit tran
END TRY
BEGIN CATCH
if XACT_STATE()=-1
BEGIN
    print 'uncommittable transaction, roll it back'
    ROLLBACK
END
END CATCH

No comments: