Handling nested transactions in Sql Server : Do rolling back out transaction roll back the inner committed transaction too ?????? big question...

SQL Server supports nesting transactions, however, writing nested transactions is usually not a good idea. The problem with nested transactions is that ROLLBACK and COMMIT statements affect multiple open transactions. Nested transactions usually occur when one stored procedure begins a transaction and then calls other procedures also using transactions before commiting the initial transaction. However, a single stored procedure could also have multiple transactions open at the same time.

Despite the common belief that nested transaction problems can be mitigated by using named transactions, ROLLBACK and COMMIT statements don't behave as you would expect from the first glance. ROLLBACK TRANSACTION only maintains the name of the outermost transaction ; if you try rolling back any transaction except the outermost one, your statement will fail. The COMMIT TRAN statement, on the other hand will not fail; however, it will only commit the inner transaction, without releasing the exclusive locks until the outermost transaction is explicitly committed or rolled back. If the outermost transaction is rolled back, then all inner transactions are also rolled back, regardless of whether or not you previously committed the inner transactions.

The transaction nesting level can be examined by querying the global variable @@trancount. In the following example, we'll look at how nested transaction commands behave. This example is extended from the Error Checking with Transactions section:

Comments

Popular posts from this blog

Authorize.net Integration eCommerce Payment Gateway ( Direct Post Method New! )

Get Organised with OneNote

Test your Sql Query Online