IEnlistmentNotification Interfaccia

Definizione

Descrive un'interfaccia che un gestore di risorse deve implementare per fornire i callback di notifica commit a due fasi per la gestione delle transazioni in seguito all'integrazione per la partecipazione.

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

Esempio

Nell'esempio seguente viene illustrata un'implementazione di questa interfaccia, nonché l'inserimento dell'oggetto come partecipante in una transazione usando il EnlistVolatile metodo .

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

Commenti

Per consentire a un gestore risorse di partecipare a una transazione, è necessario inserire nella transazione tramite gestione transazioni. La classe Transaction definisce un set di metodi i cui nomi iniziano con Enlist che forniscono questa funzionalità. I diversi metodi Enlist corrispondono ai vari tipi di integrazione disponibili in un gestore di risorse.

Questa classe descrive un'interfaccia che un gestore risorse deve implementare per fornire callback di notifica del commit in due fasi per la gestione transazioni durante l'inserimento della partecipazione. Per ogni implementazione di Resource Manager dell'interfaccia IEnlistmentNotification , è necessario aggiungerla usando il metodo o il EnlistVolatileEnlistDurable metodo della classe, a seconda che la Transaction risorsa sia volatile o durevole. Per altre informazioni sull'inserimento e 2PC, vedere L'inserimento di risorse come partecipanti a una transazione e il commit di una transazione in Single-Phase e multi-fase rispettivamente.

Gestione transazioni notifica all'oggetto elencato in fasi diverse del protocollo two phase commit tramite i metodi seguenti.

Metodo Descrizione
Prepare Questo metodo di un oggetto elencato viene usato come callback da Parte di Transaction Manager durante la prima fase di una transazione, quando il gestore delle transazioni chiede ai partecipanti se possono eseguire il commit della transazione.
Commit Questo metodo di un oggetto elencato viene usato come callback da Parte di Transaction Manager durante la seconda fase di una transazione se la transazione viene eseguito il commit.
Rollback Questo metodo di un oggetto elencato viene usato come callback da Parte di Transaction Manager durante la seconda fase di una transazione se la transazione viene interrotta, ovvero il rollback.
InDoubt Questo metodo di un oggetto elencato viene usato come callback da Parte di Transaction Manager durante la seconda fase di una transazione se la transazione è in dubbio.

Nota

È consigliabile tenere presente che le notifiche potrebbero non essere inviate in sequenza o in un determinato ordine.

Metodi

Commit(Enlistment)

Notifica a un oggetto inserito nell'elenco che è in corso l'esecuzione del commit di una transazione.

InDoubt(Enlistment)

Notifica a un oggetto inserito nell'elenco che lo stato di una transazione è in dubbio.

Prepare(PreparingEnlistment)

Notifica a un oggetto inserito nell'elenco che è in corso la preparazione di una transazione per il commit.

Rollback(Enlistment)

Notifica a un oggetto inserito nell'elenco che è in corso l'esecuzione del rollback (interruzione) di una transazione.

Si applica a

Vedi anche