Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/sql/84.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 事务作用域不适用于.net中的多个操作_C#_Sql_Asp.net_.net_Transactions - Fatal编程技术网

C# 事务作用域不适用于.net中的多个操作

C# 事务作用域不适用于.net中的多个操作,c#,sql,asp.net,.net,transactions,C#,Sql,Asp.net,.net,Transactions,我在DoWork()方法中有多个操作,其中我使用了事务作用域,但它没有按预期工作 每当DataInsert2()中出现故障时,它都应该恢复DataInsert1()和DataInsert2()。但目前它只恢复DataInsert2() 如果我在这里犯了什么错误,请告诉我 //DoWork() public void DoWork() { try { Transaction

我在DoWork()方法中有多个操作,其中我使用了事务作用域,但它没有按预期工作

每当DataInsert2()中出现故障时,它都应该恢复DataInsert1()和DataInsert2()。但目前它只恢复DataInsert2()

如果我在这里犯了什么错误,请告诉我

//DoWork()

public void DoWork()
            {
                try
                {

                  TransactionOptions tranOptions = UtilityBL.GetTransOptions();
                    using (TransactionScope scope = new TransactionScope(TransactionScopeOption.Required, tranOptions))
                    {
                        if (DataInsert1())
                        {
                          DataInsert2() ;
                        }
                        scope.Complete();
                    }
                }
                catch (Exception ex)
                {
                    log.Info(string.Format(UploadMessages.FailedMsg));
                   if (ContextUtil.IsInTransaction)
                        ContextUtil.SetAbort();
                }
          }
//数据插入1

public bool DataInsert1()
        {
           bool fileUploadStatus=false;
           try
            {

                DAL.InsertDetails1() 
                fileUploadStatus=true;

            }
            catch (Exception ex)
            {
                log.Info(string.Format(UploadMessages.FailedMsg));
           }
            return fileUploadStatus;
        }
//DataInsert2

public bool DataInsert2()
        {
           try
            { 
                DAL.InsertDetails2() 
            }
            catch (Exception ex)
            {
                log.Info(string.Format(UploadMessages.FailedMsg));
            }
        }

您应该能够按如下方式简化代码。您的
DataInsert
方法正在吞咽
DAL.InsertDetails
方法引发的任何异常

public void DoWork()
{
  TransactionOptions tranOptions = UtilityBL.GetTransOptions();
  using (TransactionScope scope = new TransactionScope(TransactionScopeOption.Required, tranOptions))
  {
    try {
      DAL.InsertDetails1();
      DAL.InsertDetails2();
      scope.Complete();
    }
    catch (Exception ex)
    {
        log.Info(string.Format(UploadMessages.FailedMsg));

        if (ContextUtil.IsInTransaction)
          ContextUtil.SetAbort();
    }
  }
}

如果不知道DAL中的内容,就不可能知道为什么这可能不起作用。特别是,
TransactionScope
本身并没有真正做任何事情,它内部运行的代码必须是事务感知的,并且可以选择加入。如果(例如)它跨越新线程来执行工作,或者您正在执行的工作实际上不支持事务,则可能不会发生这种情况。看起来您依赖于在
DoWork()中捕获的异常
回滚您的事务,但在数据插入方法中,您正在吞咽它们。@ChrisPickford我也尝试在数据插入捕获块中添加事务中止()。但还是一样的问题。您不需要显式回滚,因此您甚至可以将try-catch块放在事务范围之外。它是开箱即用的,您只需要完成()。让我试试这个。