2017-07-04 47 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對象。其餘的應該是不言自明的。

相關問題