IEnlistmentNotification Interface

Définition

Décrit une interface qu’un gestionnaire de ressources doit implémenter pour fournir des rappels de notification de validation en deux phases pour le gestionnaire de transactions lors de l’inscription à la participation.

public interface class IEnlistmentNotification
public interface IEnlistmentNotification
type IEnlistmentNotification = interface
Public Interface IEnlistmentNotification
Dérivé

Exemples

L’exemple suivant montre une implémentation de cette interface, ainsi que l’inscription de l’objet en tant que participant à une transaction à l’aide de la EnlistVolatile méthode.

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

Remarques

Pour qu’un gestionnaire de ressources participe à une transaction, il doit s’inscrire dans la transaction par le biais du gestionnaire de transactions. La Transaction classe définit un ensemble de méthodes dont les noms commencent par Enlist fournir cette fonctionnalité. Les différentes Enlist méthodes correspondent aux différents types d’inscription qu’un gestionnaire de ressources peut avoir.

Cette classe décrit une interface qu’un gestionnaire de ressources doit implémenter pour fournir des rappels de notification de validation en deux phases pour le gestionnaire de transactions lors de l’inscription pour la participation. Pour l’implémentation de chaque gestionnaire de ressources de l’interface IEnlistmentNotification , vous devez l’inscrire à l’aide de la EnlistVolatile méthode ou de la EnlistDurable méthode de la Transaction classe, selon que votre ressource est volatile ou durable. Pour plus d’informations sur l’inscription et 2PC, consultez Inscription de ressources en tant que participants à une transaction et validation d’une transaction en Single-Phase et en plusieurs phases respectivement.

Le gestionnaire de transactions notifie l’objet inscrit à différentes phases du protocole de validation en deux phases par les méthodes suivantes.

Méthode Description
Prepare Cette méthode d’un objet inscrit est utilisée comme rappel par le Gestionnaire de transactions pendant la première phase d’une transaction, lorsque le gestionnaire de transactions demande aux participants s’ils peuvent valider la transaction.
Commit Cette méthode d’un objet inscrit est utilisée comme rappel par le Gestionnaire de transactions pendant la deuxième phase d’une transaction si la transaction est validée.
Rollback Cette méthode d’un objet inscrit est utilisée comme rappel par le Gestionnaire de transactions pendant la deuxième phase d’une transaction si la transaction est abandonnée (autrement dit, restaurée).
InDoubt Cette méthode d’un objet inscrit est utilisée comme rappel par le Gestionnaire de transactions pendant la deuxième phase d’une transaction si la transaction est en doute.

Note

Vous devez savoir que les notifications peuvent ne pas être envoyées séquentiellement ou dans un ordre particulier.

Méthodes

Nom Description
Commit(Enlistment)

Avertit un objet inscrit qu’une transaction est validée.

InDoubt(Enlistment)

Avertit un objet inscrit que l’état d’une transaction est en doute.

Prepare(PreparingEnlistment)

Avertit un objet inscrit qu’une transaction est préparée pour l’engagement.

Rollback(Enlistment)

Avertit un objet inscrit qu’une transaction est restaurée (abandonnée).

S’applique à

Voir aussi