如何TransactionScope的回滚事务? [英] How does TransactionScope roll back transactions?

查看:801
本文介绍了如何TransactionScope的回滚事务?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在写一个集成测试,我会插入一些对象到数据库中,然后检查,以确保我的方法是否获取的对象。

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.

我的数据库连接是通过NHibernate的...和建立这样一个测试我平时的方法是做到以下几点:

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();

不过,我最近发现了<一href="http://msdn.microsoft.com/en-us/library/system.transactions.transactionscope.aspx">TransactionScope这显然​​可用于这个目的...

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

一些<一href="http://dotnetslackers.com/articles/ado_net/Managing_Transactions_using_TransactionScope.aspx">example code我发现如下:

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;

}

我相信,如果我不包括行 txScope.Complete()的插入的数据将被回滚。但不幸的是我不明白这是怎么可能的...如何在 txScope 对象保持跟踪 deptAdapter 和 empAdapter 在数据库中的对象和他们的交易。

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.

我觉得我错过了一下这里的信息......我真的能代替我的的BeginTransaction() RollbackTransaction( )用我的周围code调用的TransactionScope

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?

如果不是,又如何呢的TransactionScope 工作回滚交易?

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

推荐答案

从本质的TransactionScope不跟踪您的适配器,它的作用是它跟踪的数据库连接。当您打开一个数据库连接的连接会如果有一个环境事务(交易范围)如果是用它争取的外观。注意如果有更多的一个连接到相同的SQL服务器这将升级为一个Distribtued交易。

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.

什么,因为你使用的是使用块你是确保处置,即使发生异常将被称为发生。因此,如果处置是txScope.Complete前称为()的TransactionScope的将告诉连接回滚其交易(或DTC)。

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).

这篇关于如何TransactionScope的回滚事务?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆