Transaction.EnlistVolatile Metoda

Definicja

Zapisuje nietrwałego menedżera zasobów do udziału w transakcji.

Przeciążenia

EnlistVolatile(IEnlistmentNotification, EnlistmentOptions)

Enlists nietrwały menedżer zasobów, który obsługuje dwa zatwierdzenia fazy, aby uczestniczyć w transakcji.

EnlistVolatile(ISinglePhaseNotification, EnlistmentOptions)

Zapisuje nietrwałego menedżera zasobów, który obsługuje optymalizację zatwierdzania pojedynczego etapu, aby uczestniczyć w transakcji.

Uwagi

Nietrwałe menedżery zasobów nie mogą odzyskać sprawności po niepowodzeniu ukończenia transakcji, w której uczestniczyli. Aby uzyskać więcej informacji na temat nietrwałych i trwałych zasobów, a także jak zarejestrować zasób, zobacz Implementowanie Resource Manager. Aby uzyskać więcej informacji na temat reagowania menedżera zasobów na powiadomienie o zatwierdzeniu i przygotowania zatwierdzenia, zobacz Commiting A Transaction In Single-Phase and Multi-Phase (Zatwierdzanie transakcji w Single-Phase i wielofazowej).

EnlistVolatile(IEnlistmentNotification, EnlistmentOptions)

Enlists nietrwały menedżer zasobów, który obsługuje dwa zatwierdzenia fazy, aby uczestniczyć w transakcji.

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

Parametry

enlistmentNotificationnotification
IEnlistmentNotification

Obiekt, który implementuje IEnlistmentNotification interfejs do odbierania powiadomień o zatwierdzeniach dwufazowych.

enlistmentOptionsoptions
EnlistmentOptions

EnlistDuringPrepareRequired jeśli menedżer zasobów chce wykonać dodatkową pracę w fazie przygotowania.

Zwraca

Enlistment

Enlistment Obiekt opisujący rejestrację.

Przykłady

W poniższym przykładzie przedstawiono implementację interfejsu IEnlistmentNotification , a także rejestrowanie obiektu jako uczestnika transakcji przy użyciu EnlistVolatile metody .

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

Uwagi

Nietrwałe menedżery zasobów nie mogą odzyskać sprawności po niepowodzeniu ukończenia transakcji, w której uczestniczyli. Aby uzyskać trwałą rejestrację w transakcji, użyj EnlistDurable metody .

Menedżerowie zasobów wymienieni na udział w transakcji za pośrednictwem tej metody otrzymują dwa powiadomienia zatwierdzenia fazy odpowiadające metodom zdefiniowanym w interfejsie IEnlistmentNotification .

Dotyczy

EnlistVolatile(ISinglePhaseNotification, EnlistmentOptions)

Zapisuje nietrwałego menedżera zasobów, który obsługuje optymalizację zatwierdzania pojedynczego etapu, aby uczestniczyć w transakcji.

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

Parametry

singlePhaseNotificationnotification
ISinglePhaseNotification

Obiekt, który implementuje ISinglePhaseNotification interfejs, który musi mieć możliwość odbierania jednofazowych zatwierdzeń i powiadomień o zatwierdzeniach dwufazowych.

enlistmentOptionsoptions
EnlistmentOptions

EnlistDuringPrepareRequired jeśli menedżer zasobów chce wykonać dodatkową pracę w fazie przygotowania.

Zwraca

Enlistment

Obiekt Enlistment opisujący rejestrację.

Uwagi

Menedżerowie nietrwałych zasobów nie mogą odzyskać sprawności po niepowodzeniu ukończenia transakcji, w której uczestniczyli. Aby uzyskać trwałą rejestrację w transakcji, użyj EnlistDurable metody . Aby uzyskać więcej informacji na temat nietrwałych i trwałych zasobów, a także sposobu rejestracji zasobu, zobacz Implementowanie Resource Manager. Aby uzyskać więcej informacji na temat sposobu odpowiadania menedżera zasobów na powiadomienie o zatwierdzeniu i przygotowaniu zatwierdzenia, zobacz Commiting A Transaction In Single-Phase and Multi-Phase (Zatwierdzanie transakcji w Single-Phase i wielofazowym).

Należy pamiętać, że nawet jeśli implementacja menedżera zasobów zawiera listę przy użyciu tej metody, nie ma gwarancji, że otrzyma jedno zatwierdzenie fazy. Menedżer transakcji nadal może wysyłać powiadomienia o zatwierdzeniu dwóch faz. Aby uzyskać więcej informacji na temat optymalizacji zatwierdzania jednofazowego, zobacz Optymalizacja przy użyciu zatwierdzania jednofazowego i promotable jednofazowego powiadomienia.

Zobacz też

Dotyczy