DependentTransaction 类
定义
重要
一些信息与预发行产品相关,相应产品在发行之前可能会进行重大修改。 对于此处提供的信息,Microsoft 不作任何明示或暗示的担保。
描述事务的克隆,该克隆保证在应用程序停止事务上的工作之后才能提交事务。 此类不能被继承。
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 |
获取事务的隔离级别。 (继承自 Transaction) |
PromoterType |
对提升事务时由 Promote 方法返回的 |
TransactionInformation |
检索有关某个事务的附加信息。 (继承自 Transaction) |
方法
事件
TransactionCompleted |
指示事务已完成。 (继承自 Transaction) |
显式接口实现
ISerializable.GetObjectData(SerializationInfo, StreamingContext) |
获取含有序列化此事务所需要的数据的 SerializationInfo 。 (继承自 Transaction) |
适用于
线程安全性
此类型是线程安全的。