通过


DependentTransaction 类

定义

描述事务的克隆,该克隆可确保在应用程序就事务工作进行休息之前无法提交事务。 无法继承此类。

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
继承
DependentTransaction
属性

示例

以下示例演示如何创建依赖事务。

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 方法返回的格式 byte[]

(继承自 Transaction)
TransactionInformation

检索有关事务的其他信息。

(继承自 Transaction)

方法

名称 说明
Clone()

创建事务的克隆。

(继承自 Transaction)
Complete()

尝试完成依赖事务。

DependentClone(DependentCloneOption)

创建事务的依赖克隆。

(继承自 Transaction)
Dispose()

释放对象持有的资源。

(继承自 Transaction)
EnlistDurable(Guid, IEnlistmentNotification, EnlistmentOptions)

登记一个持久资源管理器,该资源管理器支持两个阶段提交以参与事务。

(继承自 Transaction)
EnlistDurable(Guid, ISinglePhaseNotification, EnlistmentOptions)

登记支持单阶段提交优化以参与事务的持久资源管理器。

(继承自 Transaction)
EnlistPromotableSinglePhase(IPromotableSinglePhaseNotification, Guid)

使用可提升的单阶段登记(PSPE)登记具有内部事务的资源管理器。

(继承自 Transaction)
EnlistPromotableSinglePhase(IPromotableSinglePhaseNotification)

使用可提升的单阶段登记(PSPE)登记具有内部事务的资源管理器。

(继承自 Transaction)
EnlistVolatile(IEnlistmentNotification, EnlistmentOptions)

登记一个可变资源管理器,该资源管理器支持两个阶段提交以参与事务。

(继承自 Transaction)
EnlistVolatile(ISinglePhaseNotification, EnlistmentOptions)

登记一个可变资源管理器,该管理器支持单阶段提交优化以参与事务。

(继承自 Transaction)
Equals(Object)

确定此事务和指定的对象是否相等。

(继承自 Transaction)
GetHashCode()

返回此实例的哈希代码。

(继承自 Transaction)
GetPromotedToken()

byte[]获取在提升事务时由方法返回的函数Promote

(继承自 Transaction)
GetType()

获取当前实例的 Type

(继承自 Object)
MemberwiseClone()

创建当前 Object的浅表副本。

(继承自 Object)
PromoteAndEnlistDurable(Guid, IPromotableSinglePhaseNotification, ISinglePhaseNotification, EnlistmentOptions)

升级并登记一个持久资源管理器,该资源管理器支持两个阶段提交以参与事务。

(继承自 Transaction)
Rollback()

回滚事务(中止)。

(继承自 Transaction)
Rollback(Exception)

回滚事务(中止)。

(继承自 Transaction)
SetDistributedTransactionIdentifier(IPromotableSinglePhaseNotification, Guid)

设置非 MSDTC 升级程序生成的分布式事务标识符。

(继承自 Transaction)
ToString()

返回一个表示当前对象的字符串。

(继承自 Object)

活动

名称 说明
TransactionCompleted

指示事务已完成。

(继承自 Transaction)

显式接口实现

名称 说明
ISerializable.GetObjectData(SerializationInfo, StreamingContext)

获取一个 SerializationInfo 数据,其中包含序列化此事务所需的数据。

(继承自 Transaction)

适用于

线程安全性

此类型是线程安全的。