0

I'm trying to implement transaction functionality.

Code Snippet:

using (TransactionScope transactionScope =
                    new TransactionScope(TransactionScopeOption.Required, TimeSpan.FromMinutes(1)))
{
    UpdateStuff1(); // ConnectionString1
    throw new Exception();
    UpdateStuff2(); // ConnectionString2       
}

FROM MSDN:

you should call this method only once to inform that transaction manager that the state across all resources is consistent.

Failing to call this method aborts the transaction.

the problem:

The transaction manager does not rollback the changes saved using UpdateSuff1().

And even if I don't throw an exception, the changes are still saved in the two databases without calling the Complete method.

Why is that so behaving?

I use the standard ado .net (SqlConnection, SqlCommand, etc.) in the methods.

HoldOffHunger
  • 18,769
  • 10
  • 104
  • 133
Rans
  • 569
  • 7
  • 14
  • As you are using two database you may have to consider using Distributed Transactions, but it depends on your two database environments. https://learn.microsoft.com/en-us/dotnet/framework/data/adonet/distributed-transactions – quaabaam Aug 06 '20 at 00:18
  • I believe it saves when exiting the using block. You should add a try catch inside to roll back any changes. – insane_developer Aug 06 '20 at 00:52
  • Does this answer your question? [C# controlling a transaction across multiple databases](https://stackoverflow.com/questions/22512450/c-sharp-controlling-a-transaction-across-multiple-databases) ... _"Any time you use more than one database connection concurrently, or different connection strings, or multiple technologies, this will require 2 phase commit and escalate to a DTC transaction in order to ensure ACID across the resources."_ – quaabaam Aug 06 '20 at 01:10
  • Do you create the Connection used in UpdateStuff1() inside the using? – PrfctByDsgn Aug 06 '20 at 07:51

0 Answers0