创建使用DependentTransaction的TransactionScope实例背后的概念是什么

时间:2017-07-04 03:58:45

标签: c# entity-framework dbcontext transactionscope

我想询问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()是不够的?

1 个答案:

答案 0 :(得分:0)

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