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 用于确定提交时的行为。 此行为控制允许应用程序休息,并提供并发支持。 有关如何使用此枚举的详细信息,请参阅 使用 DependentTransaction 管理并发

属性

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)

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

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

对资源管理器进行登记,该资源管理器具有使用可提升的单阶段登记 (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)

适用于

线程安全性

此类型是线程安全的。

另请参阅