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列舉用來判斷提交時的行為。 這種行為控制讓應用程式能夠暫停,並提供並行支援。

屬性

名稱 Description
IsolationLevel

取得交易的隔離層級。

(繼承來源 Transaction)
PromoterType

當交易被推廣時,唯一識別 Promote 方法回傳的 byte[] 格式。

(繼承來源 Transaction)
TransactionInformation

取得交易的額外資訊。

(繼承來源 Transaction)

方法

名稱 Description
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)

事件

名稱 Description
TransactionCompleted

表示交易已完成。

(繼承來源 Transaction)

明確介面實作

名稱 Description
ISerializable.GetObjectData(SerializationInfo, StreamingContext)

會拿 SerializationInfo 到一個,裡面有序列化這個交易所需的資料。

(繼承來源 Transaction)

適用於

執行緒安全性

此類型是安全線程。