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 用于确定提交时的行为。 此行为控制允许应用程序休息,并提供并发支持。
属性
| 名称 | 说明 |
|---|---|
| IsolationLevel |
获取事务的隔离级别。 (继承自 Transaction) |
| PromoterType |
唯一标识提升事务时 Promote 方法返回的格式 |
| TransactionInformation |
检索有关事务的其他信息。 (继承自 Transaction) |
方法
活动
| 名称 | 说明 |
|---|---|
| TransactionCompleted |
指示事务已完成。 (继承自 Transaction) |
显式接口实现
| 名称 | 说明 |
|---|---|
| ISerializable.GetObjectData(SerializationInfo, StreamingContext) |
获取一个 SerializationInfo 数据,其中包含序列化此事务所需的数据。 (继承自 Transaction) |
适用于
线程安全性
此类型是线程安全的。