Question

I have a question - is it possible to create the logic on the C# side with a using( block and TransactionScope, execute more than 1 non-query on a sql server, and to roll back all stored procedures if one fails?

Constraints: I am unable to make a larger stored procedure to execute other stored procedures inside of a TSQL transaction.

-The stored procedures do not have a 'commit' in them but just a return 0; at the end.

Does this mean they are able to be rolled back automatically if the transaction fails/times out/I don't hit transaction.Complete()? Do I need to insert ROLLBACK into the stored proc for this functionality?

I have searched diligently but I am unable to find the exact answer to this somewhat unique question. Thank you for your time. If anyone has any possible tests for me to execute to try, I'd be more than willing.

Was it helpful?

Solution

Yes, TransactionScope is picked up by ADO.NET. If you catch any exceptions and call .Rollback(), everything will be rolled back.

There are fine details in the way it works (e.g. MSDTC can get involved), but what you are suggesting is perfectly valid and the correct way of doing what you want to achieve.

Same thing works for Oracle and other DBs supporting TransactionScope. You can also pass the transaction through a WCF service, where if more DBs are used they are made a part of the transaction, and so on.

With some low-level work, you can even include NTFS operations in the transaction. Windows API supports it, but not .NET without a wrapper, for NTFS.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top