相关疑难解决方法(0)

TransactionScope如何回滚交易?

我正在编写一个集成测试,我将把一些对象插入数据库,然后检查以确定我的方法是否检索这些对象.

我与数据库的连接是通过NHibernate ...而我创建这样一个测试的常用方法是执行以下操作:

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();
Run Code Online (Sandbox Code Playgroud)

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

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

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(); …
Run Code Online (Sandbox Code Playgroud)

.net c# nhibernate transactions transactionscope

95
推荐指数
2
解决办法
9万
查看次数

TransactionScope不回滚事务

这是我的事务范围源代码的当前体系结构.第三个插件抛出.NET异常(不是SQL异常),它不会回滚前两个插入语句.我做错了什么?

编辑: 我从insert2和insert3中删除了try/catch.我还从insert1 try/catch中删除了异常处理实用程序并放入"throw ex".它仍然不会回滚事务.

编辑2:我在Insert3方法中添加了try/catch,并在catch语句中添加了"throw".它仍然不会回滚事务.

更新:根据我收到的反馈,"SqlHelper"类使用SqlConnection对象建立与数据库的连接,然后创建一个SqlCommand对象,将CommandType属性设置为"StoredProcedure"并调用SqlCommand的ExecuteNonQuery方法.

我也没有将Transaction Binding = Explicit Unbind添加到当前连接字符串.我将在下一次测试中添加它.

public void InsertStuff()
{
    try
    {
        using(TransactionScope ts = new TransactionScope())
        {
            //perform insert 1
            using(SqlHelper sh = new SqlHelper())
            {
                SqlParameter[] sp = { /* create parameters for first insert */ };

                sh.Insert("MyInsert1", sp);
            }

            //perform insert 2
            this.Insert2();

            //perform insert 3 - breaks here!!!!!
            this.Insert3();

            ts.Complete();            
        }
    }
    catch(Exception ex)
    {
        throw ex;
    }
}

public void Insert2()
{
    //perform insert 2
    using(SqlHelper sh …
Run Code Online (Sandbox Code Playgroud)

.net c# transactions transactionscope

12
推荐指数
2
解决办法
2万
查看次数

TransactionScope是否与预先存在的连接一起使用?

我有这样的代码:

try
{
    using (TransactionScope scope = new TransactionScope())
    {
        some_db_function();

        for (i = 0; i < 10; i++)
        {
            some_other_db_function();
        }

        scope.Complete();
    }
}
catch (Exception ex)
{
   MessageBox.Show(ex.Message + " all done transactions will rollback");   
}
Run Code Online (Sandbox Code Playgroud)

在db函数内部发生这样的事情:

private void some_db_functions()
{
    using (TransactionScope scope = new TransactionScope())
    {
       //some processing on db
       scope.Complete();
    }
}
Run Code Online (Sandbox Code Playgroud)

假设数据库事务中存在任何问题,例如在函数中插入或更新错误; 到目前为止已经完成的所有事务都被回滚.但它并不像那样工作; 虽然它抛出异常并且scope.Complete()父函数永远不会被触发,但仍然没有任何东西被回滚.

问题出在哪儿?

c# transactionscope

6
推荐指数
2
解决办法
6177
查看次数

标签 统计

c# ×3

transactionscope ×3

.net ×2

transactions ×2

nhibernate ×1