Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What does a TransactionScope really do

Looking into it I verified that for example the value o "myInt" is not rolledback in the following scenario

int myInt = 10;
using (TransactionScope scope = new TransactionScope(TransactionScopeOption.RequiresNew))
{
    myInt=20;
    Transaction t = Transaction.Current;

    t.Rollback();
}

So it got me thinking "Does a TransactionScope only rollback activities related to the database? Or there are other things that the Transaction can manage and I'm unware of those?"

like image 880
Leonardo Avatar asked Mar 07 '13 18:03

Leonardo


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 an ambient transaction?

The ambient transaction is the transaction that is present in the thread that the current application code is executing within. To obtain a reference to the ambient transaction call the static Current property of Transaction: <em>Transaction ambientTransaction = Transaction.Current;</em>

What are SQL transactions?

A SQL transaction is a grouping of one or more SQL statements that interact with a database. A transaction in its entirety can commit to a database as a single logical unit or rollback (become undone) as a single logical unit. In SQL, transactions are essential for maintaining database integrity.

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

Current transaction affects only specific objects, that are called Resource Managers. Those object must implement specific interfaces to participate in transaction. ADO.NET SqlConnection object is an example. It is not difficult to create an object that works as "Transactional Memory". Those objects are called Volatile Resource Managers. A simple example is here.

like image 131
Slava Avatar answered Sep 21 '22 12:09

Slava