2017-07-04 82 views
0

我想请教一下DependentTransaction方面,下面的代码从MSDN了,https://msdn.microsoft.com/en-us/library/system.transactions.dependenttransaction是什么概念背后产生的TransactionScope的实例,它使用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(); 
} 

,为什么我们需要再次创建TransactionScope的实例它在哪里使用DependentTransaction?仅仅依靠DependentTransaction并调用DependentTransaction.Complete()是不够的?

回答

0

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