2

I have code inside my DatabaseClient class that checks whether or not there is an active TransactionScope by examining the ThreadStatic property, Transaction.Current:

if (Transaction.Current == null)
{
    // open a new connection and do things
}

I have code consuming this class that creates a TransactionScope, executes two database operations, and then completes it. The application then moves on to do further database work. But now when it calls the code above, I get an exception:

System.InvalidOperationException: The current TransactionScope is already complete.

What do I need to do in order to "reset" the current transaction so that I can check Transaction.Current safely again?

Dan
  • 901
  • 11
  • 25
  • 1
    TransactionScope.Current ? I am not aware of any static method or property on the [TransactionScope class](https://msdn.microsoft.com/en-us/library/system.transactions.transactionscope(v=vs.110).aspx). Could you explain what is TransactionScope in your code? – Steve May 01 '18 at 20:30
  • @Steve that was a typo... it's [System.Transactions.Transaction.Current](https://msdn.microsoft.com/en-us/library/system.transactions.transaction.current). – Dan May 01 '18 at 20:50

1 Answers1

0

When a transaction has been rolled back or committed, it can't be reused. Create a new one.

Some sample code here: https://learn.microsoft.com/en-gb/dotnet/api/system.transactions.transactionscope?view=netframework-4.7

Robin
  • 616
  • 3
  • 9