IEnlistmentNotification Interfaccia

Definizione

Descrive un'interfaccia che un gestore risorse deve implementare per fornire callback di notifica del commit in due fasi per il gestore transazioni al momento dell'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'integrazione dell'oggetto come partecipante in una transazione tramite 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

Affinché un gestore di risorse partecipi a una transazione, deve essere integrato nella transazione tramite gestione transazioni. La Transaction classe definisce un set di metodi i cui nomi iniziano con Enlist che forniscono questa funzionalità. I diversi Enlist metodi corrispondono ai diversi tipi di registrazione che un gestore di risorse può avere.

Questa classe descrive un'interfaccia che un gestore risorse deve implementare per fornire callback di notifica del commit in due fasi per il gestore transazioni al momento dell'integrazione per la partecipazione. Per l'implementazione di ogni resource manager dell'interfaccia IEnlistmentNotification , è necessario integrarla usando il EnlistVolatile metodo o il EnlistDurable metodo della classe, a seconda che la Transaction risorsa sia volatile o durevole. Per altre informazioni sull'integrazione e 2PC, vedere Integrazione di risorse come partecipanti a una transazione e commit di una transazione rispettivamente in Single-Phase e in più fasi .

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

metodo Description
Prepare Questo metodo di un oggetto incluso viene utilizzato come callback da Gestione transazioni durante la prima fase di una transazione, quando il gestore transazioni chiede ai partecipanti se è possibile eseguire il commit della transazione.
Commit Questo metodo di un oggetto incluso viene utilizzato come callback da Gestione transazioni durante la seconda fase di una transazione se viene eseguito il commit della transazione.
Rollback Questo metodo di un oggetto incluso viene utilizzato come callback da Parte di Gestione transazioni durante la seconda fase di una transazione se la transazione viene interrotta, ovvero viene eseguito il rollback.
InDoubt Questo metodo di un oggetto incluso viene utilizzato come callback da Gestione transazioni durante la seconda fase di una transazione se la transazione è in dubbio.

Note

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

Metodi

Nome Descrizione
Commit(Enlistment)

Notifica a un oggetto inserito nell'elenco che viene eseguito il commit di una transazione.

InDoubt(Enlistment)

Notifica a un oggetto elencato che lo stato di una transazione è in dubbio.

Prepare(PreparingEnlistment)

Notifica a un oggetto inserito nell'elenco che una transazione viene preparata per l'impegno.

Rollback(Enlistment)

Notifica a un oggetto incluso che viene eseguito il rollback di una transazione (interrotto).

Si applica a

Vedi anche