IEnlistmentNotification Interfejs

Definicja

W tym artykule opisano interfejs, który menedżer zasobów powinien zaimplementować w celu zapewnienia wywołania zwrotnego powiadomień o zatwierdzeniu dwufazowym dla menedżera transakcji podczas rejestracji w celu uczestnictwa.

public interface class IEnlistmentNotification
public interface IEnlistmentNotification
type IEnlistmentNotification = interface
Public Interface IEnlistmentNotification
Pochodne

Przykłady

W poniższym przykładzie pokazano implementację tego interfejsu, a także zaciąganie 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

Aby menedżer zasobów brał udział w transakcji, musi zarejestrować się w transakcji za pośrednictwem menedżera transakcji. Transaction Klasa definiuje zestaw metod, których nazwy zaczynają się od Enlist tę funkcję, która udostępnia. Poszczególne Enlist metody odpowiada różnego rodzaju rejestracji, które mogą mieć Menedżera zasobów.

W tej klasie opisano interfejs, który menedżer zasobów powinien zaimplementować w celu zapewnienia wywołania zwrotnego powiadomienia o zatwierdzeniu dwufazowym dla menedżera transakcji podczas rejestracji w celu uczestnictwa. Dla każdej implementacji interfejsu IEnlistmentNotification menedżera zasobów należy zarejestrować go przy użyciu EnlistVolatile metody lub EnlistDurable metody Transaction klasy, w zależności od tego, czy zasób jest niestabilny, czy trwały. Aby uzyskać więcej informacji na temat rejestracji i 2PC, zobacz Rejestrowanie zasobów jako uczestników transakcji i zatwierdzanie transakcji odpowiednio w Single-Phase i wielofazowych .

Menedżer transakcji powiadamia obiektu wymienionego w różnych fazach dwufazowego protokołu zatwierdzania przy użyciu następujących metod.

Metoda Opis
Prepare Ta metoda obiektu wymienionego jest używana jako wywołanie zwrotne przez Menedżera transakcji w pierwszej fazie transakcji, gdy menedżer transakcji pyta uczestników, czy mogą zatwierdzić transakcję.
Commit Ta metoda obiektu enlisted jest używana jako wywołanie zwrotne przez Menedżera transakcji w drugiej fazie transakcji, jeśli transakcja zostanie zatwierdzona.
Rollback Ta metoda obiektu enlisted jest używana jako wywołanie zwrotne przez Menedżera transakcji w drugiej fazie transakcji, jeśli transakcja zostanie przerwana (to znaczy wycofane).
InDoubt Ta metoda obiektu enlisted jest używana jako wywołanie zwrotne przez Menedżera transakcji w drugiej fazie transakcji, jeśli transakcja jest wątpliwa.

Uwaga

Należy pamiętać, że powiadomienia mogą nie być wysyłane sekwencyjnie lub w określonej kolejności.

Metody

Commit(Enlistment)

Powiadamia obiekt wymieniony, że transakcja jest zatwierdzana.

InDoubt(Enlistment)

Powiadamia o obiekcie wymienionym, że stan transakcji jest wątpliwy.

Prepare(PreparingEnlistment)

Powiadamia obiekt z listy, że transakcja jest przygotowywana do zobowiązania.

Rollback(Enlistment)

Powiadamia obiekt z listy o wycofaniu transakcji (przerwane).

Dotyczy

Zobacz też