Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How does TransactionScope roll back transactions?

I'm writing an integration test where I will be inserting a number of objects into a database and then checking to make sure whether my method retrieves those objects.

My connection to the database is through NHibernate...and my usual method of creating such a test would be to do the following:

NHibernateSession.BeginTransaction();  //use nhibernate to insert objects into database //retrieve objects via my method //verify actual objects returned are the same as those inserted  NHibernateSession.RollbackTransaction(); 

However, I've recently found out about TransactionScope which apparently can be used for this very purpose...

Some example code I've found is as follows:

public static int AddDepartmentWithEmployees(Department dept) {      int res = 0;      DepartmentAdapter deptAdapter = new DepartmentAdapter();     EmployeeAdapter empAdapter = new EmployeeAdapter();     using (TransactionScope txScope = new TransactionScope())     {          res += deptAdapter.Insert(dept.DepartmentName);         //Custom method made to return Department ID          //after inserting the department "Identity Column"         dept.DepartmentID = deptAdapter.GetInsertReturnValue();         foreach(Employee emp in dept.Employees)         {              emp.EmployeeDeptID = dept.DepartmentID;             res += empAdapter.Insert(emp.EmployeeName, emp.EmployeeDeptID);          }         txScope.Complete();      }     return res;  } 

I believe that if I don't include the line txScope.Complete() that the data inserted will be rolled back. But unfortunately I don't understand how that is possible... how does the txScope object keep a track of the deptAdapter and empAdapter objects and their transactions on the database.

I feel like I'm missing a bit of information here...am I really able to replace my BeginTransaction() and RollbackTransaction() calls by surrounding my code using TransactionScope?

If not, how then does TransactionScope work to roll back transactions?

like image 339
mezoid Avatar asked Jan 30 '09 05:01

mezoid


People also ask

How do you rollback a transaction?

You can use ROLLBACK TRANSACTION to erase all data modifications made from the start of the transaction or to a savepoint. It also frees resources held by the transaction. This does not include changes made to local variables or table variables. These are not erased by this statement.

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 does Entity Framework handle transactions?

In all versions of Entity Framework, whenever you execute SaveChanges() to insert, update or delete on the database the framework will wrap that operation in a transaction. This transaction lasts only long enough to execute the operation and then completes.

What does it mean to rollback a transaction?

A rollback is the operation of restoring a database to a previous state by canceling a specific transaction or transaction set. Rollbacks are either performed automatically by database systems or manually by users.


1 Answers

Essentially TransactionScope doesn't track your Adapter's, what it does is it tracks database connections. When you open a DB connection the connections will looks if there is an ambient transaction (Transaction Scope) and if so enlist with it. Caution if there are more the one connection to the same SQL server this will escalate to a Distribtued Transaction.

What happens since you're using a using block you are ensuring dispose will be called even if an exception occurs. So if dispose is called before txScope.Complete() the TransactionScope will tell the connections to rollback their transactions (or the DTC).

like image 91
JoshBerke Avatar answered Oct 07 '22 01:10

JoshBerke