DependentTransaction 類別
定義
重要
部分資訊涉及發行前產品,在發行之前可能會有大幅修改。 Microsoft 對此處提供的資訊,不做任何明確或隱含的瑕疵擔保。
描述交易的複製品 (Clone),其可保證在交易上執行工作時,必須等到應用程式停止後才能認可交易。 此類別無法獲得繼承。
public ref class DependentTransaction sealed : System::Transactions::Transaction
public sealed class DependentTransaction : System.Transactions.Transaction
[System.Serializable]
public sealed class DependentTransaction : System.Transactions.Transaction
type DependentTransaction = class
inherit Transaction
[<System.Serializable>]
type DependentTransaction = class
inherit Transaction
Public NotInheritable Class DependentTransaction
Inherits Transaction
- 繼承
- 屬性
範例
下列範例示範如何建立相依交易。
static void Main(string[] args)
{
try
{
using (TransactionScope scope = new TransactionScope())
{
// Perform transactional work here.
//Queue work item
ThreadPool.QueueUserWorkItem(new WaitCallback(WorkerThread), Transaction.Current.DependentClone(DependentCloneOption.BlockCommitUntilComplete));
//Display transaction information
Console.WriteLine("Transaction information:");
Console.WriteLine("ID: {0}", Transaction.Current.TransactionInformation.LocalIdentifier);
Console.WriteLine("status: {0}", Transaction.Current.TransactionInformation.Status);
Console.WriteLine("isolationlevel: {0}", Transaction.Current.IsolationLevel);
//Call Complete on the TransactionScope based on console input
ConsoleKeyInfo c;
while (true)
{
Console.Write("Complete the transaction scope? [Y|N] ");
c = Console.ReadKey();
Console.WriteLine();
if ((c.KeyChar == 'Y') || (c.KeyChar == 'y'))
{
//Call complete on the scope
scope.Complete();
break;
}
else if ((c.KeyChar == 'N') || (c.KeyChar == 'n'))
{
break;
}
}
}
}
catch (System.Transactions.TransactionException ex)
{
Console.WriteLine(ex);
}
catch
{
Console.WriteLine("Cannot complete transaction");
throw;
}
}
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();
}
Public Shared Sub Main()
Try
Using scope As TransactionScope = New TransactionScope()
'Perform transactional work here.
'Queue work item
ThreadPool.QueueUserWorkItem(AddressOf WorkerThread, Transaction.Current.DependentClone(DependentCloneOption.BlockCommitUntilComplete))
'Display transaction information
Console.WriteLine("Transaction information:")
Console.WriteLine("ID: {0}", Transaction.Current.TransactionInformation.LocalIdentifier)
Console.WriteLine("status: {0}", Transaction.Current.TransactionInformation.Status)
Console.WriteLine("isolationlevel: {0}", Transaction.Current.IsolationLevel)
'Call Complete on the TransactionScope based on console input
Dim c As ConsoleKeyInfo
While (True)
Console.Write("Complete the transaction scope? [Y|N] ")
c = Console.ReadKey()
Console.WriteLine()
If (c.KeyChar = "Y") Or (c.KeyChar = "y") Then
scope.Complete()
Exit While
ElseIf ((c.KeyChar = "N") Or (c.KeyChar = "n")) Then
Exit While
End If
End While
End Using
Catch ex As TransactionException
Console.WriteLine(ex)
Catch
Console.WriteLine("Cannot complete transaction")
Throw
End Try
End Sub
Public Shared Sub WorkerThread(ByVal myTransaction As Object)
'Create a DependentTransaction from the object passed to the WorkerThread
Dim dTx As DependentTransaction
dTx = CType(myTransaction, DependentTransaction)
'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 ts As TransactionScope = New TransactionScope(dTx)
'Perform transactional work here.
'Call complete on the transaction scope
ts.Complete()
End Using
'Call complete on the dependent transaction
dTx.Complete()
End Sub
備註
DependentTransaction是使用 DependentClone 方法建立之物件的複製Transaction品。 其唯一目的是讓應用程式進入待用,並保證交易在工作仍在 (交易上執行時無法認可,例如,在背景工作線程上) 。
當複製交易內完成的工作完成且準備好認可時,可以使用 方法通知建立者交易 Complete 。 因此,您可以保留數據的一致性和正確性。
列舉 DependentCloneOption 可用來判斷認可的行為。 此行為控制可讓應用程式待用,並提供並行支援。 如需如何使用這個列舉的詳細資訊,請參閱 使用 DependentTransaction 管理並行存取。
屬性
IsolationLevel |
取得交易的隔離等級 (Isolation Level)。 (繼承來源 Transaction) |
PromoterType |
可唯一識別 |
TransactionInformation |
擷取交易的其他資訊。 (繼承來源 Transaction) |
方法
事件
TransactionCompleted |
指出交易已完成。 (繼承來源 Transaction) |
明確介面實作
ISerializable.GetObjectData(SerializationInfo, StreamingContext) |
取得 SerializationInfo,其中含有序列化這個交易所需的資料。 (繼承來源 Transaction) |
適用於
執行緒安全性
此型別具備執行緒安全。