Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

TransactionScope Complete() doesn't commit the transaction before exiting the USING statement

I am experiencing this weird behavior where the transaction gets committed only when the using exits and not when calling scope.Complete();

using (TransactionScope scope = new TransactionScope(TransactionScopeOption.RequiresNew))
       {
        scope.Complete();
           // data still doesn't show in db
       }
       // now shows in db

How do I commit the transaction before exiting the using statement?

like image 605
user2312219 Avatar asked Apr 25 '13 11:04

user2312219


People also ask

What is the use of TransactionScope in c#?

The TransactionScope class provides a simple way to mark a block of code as participating in a transaction, without requiring you to interact with the transaction itself. A transaction scope can select and manage the ambient transaction automatically.

How TransactionScope works?

TransactionScope provides an implicit programming model in which the transactions are automatically managed by the infrastructure. It provides a simple mechanism for you to specify a code block to participate in a transaction.

What is ambient transaction c#?

A transaction which automatically identifies a code block that needs to support a transaction without explicitly mentioning any transaction related things. An ambient transaction is not tied just to a database, any transaction aware provider can be used. TransactionScope implements an ambient transaction.

What is transaction scope in entity framework?

Entity Framework is already operating within a TransactionScope. The connection object in the transaction passed is null. That is, the transaction is not associated with a connection – usually this is a sign that that transaction has already completed.


1 Answers

from the documentation:

The actual work of commit between the resources manager happens at the End Using statement if the TransactionScope object created the transaction.

So it doesn't look like you can truly commit the transaction before the using statement end.

like image 149
NDJ Avatar answered Oct 02 '22 01:10

NDJ