【问题标题】:What is the concept behind creating instance of TransactionScope which uses DependentTransaction创建使用 DependentTransaction 的 TransactionScope 实例背后的概念是什么
【发布时间】:2017-07-04 03:58:44
【问题描述】:

想问一下DependentTransaction方面的问题,以下代码来自MSDN,https://msdn.microsoft.com/en-us/library/system.transactions.dependenttransaction

private static void WorkerThread(object transaction)
{
    //Create a DependentTransaction from the object passed to the WorkerThread
    DependentTransaction dTx = (DependentTransaction)transaction;

    //Sleep for 1 second to force the worker thread to delay
    Thread.Sleep(1000);

    //Pass the DependentTransaction to the scope, so that work done in the scope becomes part of the transaction passed to the worker thread
    using (TransactionScope ts = new TransactionScope(dTx))
    {
        //Perform transactional work here.

        //Call complete on the transaction scope
        ts.Complete();
    }

    //Call complete on the dependent transaction
    dTx.Complete();
}

为什么我们需要在使用 DependentTransaction 的地方再次创建 TransactionScope 的实例?仅仅依赖 DependentTransaction 并调用 DependentTransaction.Complete() 还不够吗?

【问题讨论】:

    标签: c# entity-framework dbcontext transactionscope


    【解决方案1】:

    TransactionScope 为 Transaction 创建实际的构造函数(1 Transaction Constructor:Many Transactions)。 DependentTransaction 有点不同,因为它依赖于使用范围 TransactionScope 来在特定 Transaction 完成之前结束。这意味着销毁后将完成几乎 1 个或许多事务。尝试使用指令事务,然后创建一个 DependentTransaction 对象。其余的应该是不言自明的。

    【讨论】:

      猜你喜欢
      • 2012-04-17
      • 1970-01-01
      • 1970-01-01
      • 2011-01-15
      • 2022-01-10
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多