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

查看:48
本文介绍了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();

但是,我最近发现了 TransactionScope 显然可以用于此目的...

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

一些我发现的示例代码如下:

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 对象如何跟踪 deptAdapterempAdapter 对象和他们在数据库上的交易.

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()调用替换代码使用 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 服务器,这将升级为分布式事务.

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.

自从您使用 using 块后会发生什么,您确保即使发生异常也会调用 dispose.因此,如果在 txScope.Complete() 之前调用 dispose,则 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天全站免登陆