How to check if a Dotnet transaction has rolled back?

How to check if a dotnet transaction is closed?

+4
source share
3 answers
using(TransactionScope scope = new TransactionScope(TransactionScopeOption.RequiresNew)){ try{ //Do something; scope.Complete(); //denotes the transaction completed successful. } catch(TransactionAbortedException ex) { //scope.Complete(); is never called, the transaction rolls back automatically. } catch(ApplicationException ex) { } } 
+1
source

Your name asks one thing, and your question asks another. therefore, I am going with your name.

If you want to know rollback transaction or rollback only, you can check

 transaction.WasRolledBack // true if transaction is rolled back 

Here transaction is an instance of ITransaction

Edit (based on your comment) :

 var isRolledBack = false; using (var connection = new SqlConnection()) { using (var transaction = connection.BeginTransaction()) { try { // do your stuff here with transaction } catch (Exception ex) { transaction.Rollback(); isRolledBack = true; throw; } } } 

Now you can check the isRolledBack flag to see if the transaction is isRolledBack back.

+3
source

If you are on a SQL server, you can use DBCC OPENTRAN

http://msdn.microsoft.com/en-us/library/ms182792.aspx

0
source

Source: https://habr.com/ru/post/1332286/


All Articles