DependentTransaction 类

定义

描述事务的克隆,该克隆保证在应用程序停止事务上的工作之后才能提交事务。Describes a clone of a transaction providing guarantee that the transaction cannot be committed until the application comes to rest regarding work on the transaction. 此类不能被继承。This class cannot be inherited.

public ref class DependentTransaction sealed : System::Transactions::Transaction, System::Runtime::Serialization::ISerializable
[System.Serializable]
public sealed class DependentTransaction : System.Transactions.Transaction, System.Runtime.Serialization.ISerializable
type DependentTransaction = class
    inherit Transaction
    interface ISerializable
Public NotInheritable Class DependentTransaction
Inherits Transaction
Implements ISerializable
继承
DependentTransaction
属性
实现

示例

下面的示例演示如何创建从属事务。The following example shows you how to create a dependent 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

注解

DependentTransactionTransaction使用方法DependentClone创建的对象的克隆。The DependentTransaction is a clone of a Transaction object created using the DependentClone method. 其唯一目的是让应用程序进入 rest,并保证在事务上仍在执行工作时无法提交事务(例如,在工作线程上)。Its sole purpose is to allow the application to come to rest and guarantee that the transaction cannot commit while work is still being performed on the transaction (for example, on a worker thread).

如果克隆的事务中完成的工作最后完成并准备好进行提交,则它可以使用Complete方法通知事务创建者。When the work done within the cloned transaction is finally complete and ready to be committed, it can inform the creator of the transaction using the Complete method. 因此,你可以保留数据的一致性和正确性。Thus you can preserve the consistency and correctness of data.

DependentCloneOption枚举用于确定提交时的行为。The DependentCloneOption enumeration is used to determine the behavior on commit. 此行为控制允许应用程序进入 rest,并提供并发支持。This behavior control allows an application to come to rest, as well as provides concurrency support. 有关如何使用此枚举的详细信息,请参阅使用 DependentTransaction 管理并发For more information on how this enumeration is used, see Managing Concurrency with DependentTransaction.

属性

IsolationLevel

获取事务的隔离级别。Gets the isolation level of the transaction.

(继承自 Transaction)
PromoterType

对提升事务时由 Promote 方法返回的 byte [] 的格式进行唯一标识。Uniquely identifies the format of the byte[] returned by the Promote method when the transaction is promoted.

(继承自 Transaction)
TransactionInformation

检索有关某个事务的附加信息。Retrieves additional information about a transaction.

(继承自 Transaction)

方法

BeginCommitInternal(AsyncCallback) (继承自 Transaction)
Clone()

创建事务的克隆。Creates a clone of the transaction.

(继承自 Transaction)
Complete()

尝试完成从属事务。Attempts to complete the dependent transaction.

DependentClone(DependentCloneOption)

创建事务的依赖复本。Creates a dependent clone of the transaction.

(继承自 Transaction)
Dispose()

释放由该对象占用的资源。Releases the resources that are held by the object.

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

登记支持两阶段提交的持久资源管理器以参与事务。Enlists a durable resource manager that supports two phase commit to participate in a transaction.

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

登记支持单阶段提交优化的持久资源管理器以参与事务。Enlists a durable resource manager that supports single phase commit optimization to participate in a transaction.

(继承自 Transaction)
EnlistPromotableSinglePhase(IPromotableSinglePhaseNotification)

对资源管理器进行登记,该资源管理器具有使用可提升的单阶段登记 (PSPE) 的内部事务。Enlists a resource manager that has an internal transaction using a promotable single phase enlistment (PSPE).

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

对资源管理器进行登记,该资源管理器具有使用可提升的单阶段登记 (PSPE) 的内部事务。Enlists a resource manager that has an internal transaction using a promotable single phase enlistment (PSPE).

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

登记在事务中支持两阶段提交参与的易失性资源管理器。Enlists a volatile resource manager that supports two phase commit to participate in a transaction.

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

登记支持单阶段提交优化的可变资源管理器以参与事务。Enlists a volatile resource manager that supports single phase commit optimization to participate in a transaction.

(继承自 Transaction)
Equals(Object)

确定此事务和指定的对象是否相等。Determines whether this transaction and the specified object are equal.

(继承自 Transaction)
GetHashCode()

返回此实例的哈希代码。Returns the hash code for this instance.

(继承自 Transaction)
GetPromotedToken()

获取提升事务时由 Promote 方法返回的 byte []Gets the byte[] returned by the Promote method when the transaction is promoted.

(继承自 Transaction)
GetType()

获取当前实例的 TypeGets the Type of the current instance.

(继承自 Object)
MemberwiseClone()

创建当前 Object 的浅表副本。Creates a shallow copy of the current Object.

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

提升和登记支持两阶段提交的持久资源管理器以参与事务。Promotes and enlists a durable resource manager that supports two phase commit to participate in a transaction.

(继承自 Transaction)
Rollback()

回滚(中止)事务。Rolls back (aborts) the transaction.

(继承自 Transaction)
Rollback(Exception)

回滚(中止)事务。Rolls back (aborts) the transaction.

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

设置由非 MSDTC 提升程序生成的分布式事务标识符。Sets the distributed transaction identifier generated by the non-MSDTC promoter.

(继承自 Transaction)
ToString()

返回一个表示当前对象的 string。Returns a string that represents the current object.

(继承自 Object)

事件

TransactionCompleted

指示事务已完成。Indicates that the transaction is completed.

(继承自 Transaction)

显式界面实现

ISerializable.GetObjectData(SerializationInfo, StreamingContext)

适用于

线程安全性

此类型是线程安全的。This type is thread safe.

另请参阅