小编Dyl*_*lan的帖子

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# ×1

transactionscope ×1