共用方式為


Transaction.EnlistVolatile 方法

定義

登記暫時性 (Volatile) 資源管理員以參與交易。

多載

EnlistVolatile(IEnlistmentNotification, EnlistmentOptions)

登錄暫時性資源管理員,支援兩階段交易認可參與交易。

EnlistVolatile(ISinglePhaseNotification, EnlistmentOptions)

登記支援單一階段交易認可最佳化的暫時性 (Volatile) 資源管理員以參與交易。

備註

變動性資源管理員無法從失敗復原,無法完成參與的交易。 如需有關揮發性和持久性資源以及如何登記資源的詳細資訊,請參閱實作 A Resource Manager。 如需資源管理員如何回應認可通知並準備認可的詳細資訊,請參閱 在Single-Phase和多階段認可交易

EnlistVolatile(IEnlistmentNotification, EnlistmentOptions)

登錄暫時性資源管理員,支援兩階段交易認可參與交易。

public:
 System::Transactions::Enlistment ^ EnlistVolatile(System::Transactions::IEnlistmentNotification ^ enlistmentNotification, System::Transactions::EnlistmentOptions enlistmentOptions);
public:
 System::Transactions::Enlistment ^ EnlistVolatile(System::Transactions::IEnlistmentNotification ^ notification, System::Transactions::EnlistmentOptions options);
public System.Transactions.Enlistment EnlistVolatile (System.Transactions.IEnlistmentNotification enlistmentNotification, System.Transactions.EnlistmentOptions enlistmentOptions);
public System.Transactions.Enlistment EnlistVolatile (System.Transactions.IEnlistmentNotification notification, System.Transactions.EnlistmentOptions options);
member this.EnlistVolatile : System.Transactions.IEnlistmentNotification * System.Transactions.EnlistmentOptions -> System.Transactions.Enlistment
member this.EnlistVolatile : System.Transactions.IEnlistmentNotification * System.Transactions.EnlistmentOptions -> System.Transactions.Enlistment
Public Function EnlistVolatile (enlistmentNotification As IEnlistmentNotification, enlistmentOptions As EnlistmentOptions) As Enlistment
Public Function EnlistVolatile (notification As IEnlistmentNotification, options As EnlistmentOptions) As Enlistment

參數

enlistmentNotificationnotification
IEnlistmentNotification

該物件會實作 IEnlistmentNotification 介面,以接收兩階段交易認可通知。

enlistmentOptionsoptions
EnlistmentOptions

如果資源管理員想要在準備階段執行額外的工作,即為 EnlistDuringPrepareRequired

傳回

Enlistment

描述登錄的 Enlistment 物件。

範例

下列範例示範 介面的實作 IEnlistmentNotification ,以及使用 EnlistVolatile 方法將 物件登記為交易中的參與者。

static void Main(string[] args)
{
    try
    {
        using (TransactionScope scope = new TransactionScope())
        {
        
            //Create an enlistment object
            myEnlistmentClass myElistment = new myEnlistmentClass();

            //Enlist on the current transaction with the enlistment object
            Transaction.Current.EnlistVolatile(myElistment, EnlistmentOptions.None);

            //Perform transactional work here.

            //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'))
                {
                    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;
    }
}

class myEnlistmentClass : IEnlistmentNotification
{
    public void Prepare(PreparingEnlistment preparingEnlistment)
    {
        Console.WriteLine("Prepare notification received");

        //Perform transactional work

        //If work finished correctly, reply prepared
        preparingEnlistment.Prepared();

        // otherwise, do a ForceRollback
        preparingEnlistment.ForceRollback();
    }

    public void Commit(Enlistment enlistment)
    {
        Console.WriteLine("Commit notification received");

        //Do any work necessary when commit notification is received

        //Declare done on the enlistment
        enlistment.Done();
    }

    public void Rollback(Enlistment enlistment)
    {
        Console.WriteLine("Rollback notification received");

        //Do any work necessary when rollback notification is received

        //Declare done on the enlistment
        enlistment.Done();
    }

    public void InDoubt(Enlistment enlistment)
    {
        Console.WriteLine("In doubt notification received");

        //Do any work necessary when indout notification is received
        
        //Declare done on the enlistment
        enlistment.Done();
    }
}
    Public Shared Sub Main()
        Try
            Using scope As TransactionScope = New TransactionScope()

                'Create an enlistment object
                Dim myEnlistmentClass As New EnlistmentClass

                'Enlist on the current transaction with the enlistment object
                Transaction.Current.EnlistVolatile(myEnlistmentClass, EnlistmentOptions.None)

                'Perform transactional work here.

                '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
End Class

Public Class EnlistmentClass
    Implements IEnlistmentNotification

    Public Sub Prepare(ByVal myPreparingEnlistment As PreparingEnlistment) Implements System.Transactions.IEnlistmentNotification.Prepare
        Console.WriteLine("Prepare notification received")

        'Perform transactional work

        'If work finished correctly, reply with prepared
        myPreparingEnlistment.Prepared()
    End Sub

    Public Sub Commit(ByVal myEnlistment As Enlistment) Implements System.Transactions.IEnlistmentNotification.Commit
        Console.WriteLine("Commit notification received")

        'Do any work necessary when commit notification is received

        'Declare done on the enlistment
        myEnlistment.Done()
    End Sub

    Public Sub Rollback(ByVal myEnlistment As Enlistment) Implements System.Transactions.IEnlistmentNotification.Rollback
        Console.WriteLine("Rollback notification received")

        'Do any work necessary when rollback notification is received

        'Declare done on the enlistment
        myEnlistment.Done()
    End Sub

    Public Sub InDoubt(ByVal myEnlistment As Enlistment) Implements System.Transactions.IEnlistmentNotification.InDoubt
        Console.WriteLine("In doubt notification received")

        'Do any work necessary when indout notification is received

        'Declare done on the enlistment
        myEnlistment.Done()
    End Sub
End Class

備註

變動性資源管理員無法從失敗復原,無法完成參與的交易。 若要在交易中取得永久性登記,請使用 EnlistDurable 方法。

透過此方法登記參與交易的資源管理員會收到兩個階段認可通知,這些通知會對應至介面上 IEnlistmentNotification 定義的方法。

適用於

EnlistVolatile(ISinglePhaseNotification, EnlistmentOptions)

登記支援單一階段交易認可最佳化的暫時性 (Volatile) 資源管理員以參與交易。

public:
 System::Transactions::Enlistment ^ EnlistVolatile(System::Transactions::ISinglePhaseNotification ^ singlePhaseNotification, System::Transactions::EnlistmentOptions enlistmentOptions);
public:
 System::Transactions::Enlistment ^ EnlistVolatile(System::Transactions::ISinglePhaseNotification ^ notification, System::Transactions::EnlistmentOptions options);
public System.Transactions.Enlistment EnlistVolatile (System.Transactions.ISinglePhaseNotification singlePhaseNotification, System.Transactions.EnlistmentOptions enlistmentOptions);
public System.Transactions.Enlistment EnlistVolatile (System.Transactions.ISinglePhaseNotification notification, System.Transactions.EnlistmentOptions options);
member this.EnlistVolatile : System.Transactions.ISinglePhaseNotification * System.Transactions.EnlistmentOptions -> System.Transactions.Enlistment
member this.EnlistVolatile : System.Transactions.ISinglePhaseNotification * System.Transactions.EnlistmentOptions -> System.Transactions.Enlistment
Public Function EnlistVolatile (singlePhaseNotification As ISinglePhaseNotification, enlistmentOptions As EnlistmentOptions) As Enlistment
Public Function EnlistVolatile (notification As ISinglePhaseNotification, options As EnlistmentOptions) As Enlistment

參數

singlePhaseNotificationnotification
ISinglePhaseNotification

實作 ISinglePhaseNotification 介面的物件,該介面必須可以接收單一階段交易認可和兩階段交易認可告知。

enlistmentOptionsoptions
EnlistmentOptions

如果資源管理員想要在準備階段執行額外的工作,即為 EnlistDuringPrepareRequired

傳回

Enlistment

描述登錄的 Enlistment 物件。

備註

變動性資源管理員無法從失敗復原,無法完成參與的交易。 若要在交易中取得永久性登記,請使用 EnlistDurable 方法。 如需有關揮發性和持久性資源以及如何登記資源的詳細資訊,請參閱實作 A Resource Manager。 如需資源管理員如何回應認可通知並準備認可的詳細資訊,請參閱 在Single-Phase和多階段認可交易

您應該注意,即使您的資源管理員實作使用此方法登記,也不保證它會收到單一階段認可。 交易管理員仍然可以改為傳送兩個階段認可通知。 如需單一階段認可優化的詳細資訊,請參閱使用單一階段認可 優化和可提升單一階段通知

另請參閱

適用於