IEnlistmentNotification Interfaz

Definición

Describe una interfaz que un administrador de recursos debe implementar para proporcionar devoluciones de llamada de notificación de confirmación en dos fases para el administrador de transacciones al inscribirse para la participación.

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

Ejemplos

En el ejemplo siguiente se muestra una implementación de esta interfaz, así como la inscripción del objeto como participante en una transacción mediante el EnlistVolatile método .

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

Comentarios

Para que un administrador de recursos participe en una transacción, debe inscribirse en la transacción a través del administrador de transacciones. La clase Transaction define un conjunto de métodos cuyos nombres comienzan con Enlist que proporcionan esta funcionalidad. Los distintos métodos Enlist corresponden a los distintos tipos de inscripción que un administrador de recursos puede tener.

Esta clase describe una interfaz que un administrador de recursos debe implementar para proporcionar devoluciones de llamada de notificación de confirmación en dos fases para el administrador de transacciones al inscribirse para participar. Para la implementación de cada administrador de recursos de la IEnlistmentNotification interfaz, debe inscribirla mediante el EnlistVolatile método o el EnlistDurable método de la Transaction clase, en función de si el recurso es volátil o duradero. Para obtener más información sobre la inscripción y 2PC, vea Inscribir recursos como participantes en una transacción y confirmar una transacción en Single-Phase y multifásica , respectivamente.

El administrador de transacciones notifica al objeto inscrito en distintas fases del Protocolo de confirmación de dos fases mediante los métodos siguientes.

Método Descripción
Prepare Este método de un objeto inscrito se usa como devolución de llamada por el Administrador de transacciones durante la primera fase de una transacción, cuando el administrador de transacciones pregunta a los participantes si pueden confirmar la transacción.
Commit Este método de un objeto inscrito se usa como devolución de llamada por el Administrador de transacciones durante la segunda fase de una transacción si se confirma la transacción.
Rollback Este método de un objeto inscrito se usa como devolución de llamada por el Administrador de transacciones durante la segunda fase de una transacción si se anula la transacción (es decir, se revierte).
InDoubt Este método de un objeto inscrito se usa como devolución de llamada por el Administrador de transacciones durante la segunda fase de una transacción si la transacción está en duda.

Nota

Debe tener en cuenta que es posible que las notificaciones no se envíen secuencialmente o en un orden determinado.

Métodos

Commit(Enlistment)

Notifica a un objeto inscrito que se confirma una transacción.

InDoubt(Enlistment)

Notifica a un objeto inscrito que el estado de una transacción está en duda.

Prepare(PreparingEnlistment)

Notifica a un objeto inscrito que se prepara una transacción para la confirmación.

Rollback(Enlistment)

Notifica a un objeto inscrito que se deshace (se anula) una transacción.

Se aplica a

Consulte también