Multiple Transaction Issue

I want to create a transaction, write some data to a sub-transaction, read the data back and cancel the transaction.

using(var transaction = new TransactionScope()) { using(var transaction = new TransactionScope()) { // save data via LINQ / DataContext transaction.Complete(); } // Get back for assertions var tempItem = // read data via LINQ / DataContext THROWS EXCEPTION } 

But when reading, I get "System.Transactions.TransactionException: operation is not valid for transaction status."

How do I set transaction properties to avoid this?

+6
c # transactions
source share
2 answers

This exception cannot be debugged without a full stack trace. It depends on the context. This usually means that you are doing something that should not be inside the transaction, but without seeing the db calls or the stack trace, all you can do is guess. Some common reasons that I know about (and this is far from complete, I'm sure) include:

  • Access to multiple data sources (i.e. different connection strings) inside a nested TransactionScope . This causes a move towards a distributed transaction, and if you do not perform DTC, it will not work. The response, as a rule, does not include DTC, but to clear a transaction or transfer another data access using the new TransactionScope(TransactionOptions.RequiresNew) .
  • Unhandled exceptions in TransactionScope .
  • Any operation that violates the isolation level, such as reading rows inserted or updated.
  • SQL deadlocks; Transactions can even come to a standstill in certain cases, but if No. 1 is applied, allocating other operations in new transactions can lead to locks if you are not careful.
  • Transaction timeouts.
  • Any other error from the database.

I definitely do not know every possible reason, but if you put the full stack trace and the actual db calls in your code, I will take a look and let you know if I see anything.

+9
source share

Do you have two nested TransactionScope objects?

And do not try to block catch.

http://msdn.microsoft.com/en-us/library/system.transactions.transactionscope.aspx

I think you will find that the specific answer is that you cannot complete a transaction that did not start anything, it is in an invalid state. Do you really have the code, where are your LINQ comments? Is the connection really established?

+4
source share

All Articles