Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How does TransactionScope work?

When Method1() instantiates a TransactionScope and calls Method2() that also instantiates a TransactionScope, how does .NET know both are in the same scope? I believe it doesn't use static methods internally otherwise it wouldn't work well on multithreaded applications like ASP.NET.

Is it possible to create my own TransactionScope-like class or does the original one use special features those just Microsoft knows how they work?

like image 426
Eduardo Avatar asked Aug 05 '10 18:08

Eduardo


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.

What is TransactionScope in Entity Framework?

NET framework it provides management of its own transaction components using TransactionScope class. TransactionScope is a class of System Namespace. It can also be termed as Transactions Namespace. The TransactionScope class supports transactions from code blocks and that is why it plays a key role in the .

What is ambient transaction?

The term "ambient" transaction refers to a transaction that was started higher-up in the call stack. So that this is a per-thread concept. See Transaction.Current and TransactionScope.

What is transaction scope in SQL Server?

Definition: TransactionalScope makes your code block Transactional. You can easily maintain one transaction for multiple databases or a single database with multiple connectionstrings, using TransactionScope. When you use TransactionScope there is no need to close any Database connections in the middle.


2 Answers

Hope this helps:

http://msdn.microsoft.com/en-us/magazine/cc300805.aspx

For those unfamiliar with TransactionScope, it is part of the System.Transactions namespace new to the Microsoft® .NET Framework 2.0. System.Transactions provides a transactions framework fully integrated into the .NET Framework, including but not limited to ADO.NET. The Transaction and TransactionScope classes are two of the most important classes in this namespace. As the question alludes to, you can create a TransactionScope instance, and ADO.NET operations executed within the scope of that TransactionScope will be enlisted automatically (you can also access the current Transaction through the Transaction.Current static property):

using(TransactionScope scope = new TransactionScope()) {     ... // all operations here part of a transaction     scope.Complete(); } 
like image 111
Jordão Avatar answered Sep 19 '22 09:09

Jordão


TransactionScope pretty much builds on top of COM - specifically over MSDTC.

This coordinates transactions, and allows nesting of transactions.

In short, when you first call TransactionScope, a transaction registers with MSDTC, as would all other calls to TransactionScope. MSDTC coordinates them all.

like image 21
Oded Avatar answered Sep 20 '22 09:09

Oded