IEnlistmentNotification Rozhraní

Definice

Popisuje rozhraní, které by měl správce prostředků implementovat k poskytnutí dvoufázové potvrzení oznámení zpětné volání pro správce transakcí při zařazení k účasti.

public interface class IEnlistmentNotification
public interface IEnlistmentNotification
type IEnlistmentNotification = interface
Public Interface IEnlistmentNotification
Odvozené

Příklady

Následující příklad ukazuje implementaci tohoto rozhraní, stejně jako zařazení objektu jako účastník transakce pomocí 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

Poznámky

Chcete-li správce prostředků k účasti v transakci, musí zařadit do transakce prostřednictvím správce transakcí. Transaction Třída definuje sadu metod, jejichž názvy začínají řetězcem Enlist , poskytují tuto funkci. Různých Enlist metody, které odpovídají různé typy zařazení, které mohou mít správce prostředků.

Tato třída popisuje rozhraní, které by měl správce prostředků implementovat, aby poskytoval dvě fáze potvrzení zpětné volání pro správce transakcí při zařazení k účasti. Pro každou implementaci IEnlistmentNotification rozhraní správce prostředků byste ho měli zařadit pomocí EnlistVolatile metody nebo EnlistDurable metody Transaction třídy v závislosti na tom, jestli je prostředek nestálý nebo trvalý. Další informace o zařazení a 2PC naleznete v tématu Zařazení prostředků jako účastníci transakce a Potvrzení transakce v Single-Phase a multifázové v uvedeném pořadí.

Správce transakcí upozorní zařazený objekt v různých fázích dvoufázového potvrzení protokolu pomocí následujících metod.

Metoda Popis
Prepare Tato metoda zařazeného objektu se používá jako zpětné volání Správce transakcí během první fáze transakce, když správce transakcí se zeptá účastníků, zda mohou potvrdit transakci.
Commit Tato metoda zařazeného objektu se používá jako zpětné volání Správce transakcí během druhé fáze transakce, pokud transakce je potvrzena.
Rollback Tato metoda zařazeného objektu se používá jako zpětné volání Správce transakcí během druhé fáze transakce, pokud je transakce přerušena (to znamená, že vrácena zpět).
InDoubt Tato metoda zařazení objektu je použita jako zpětné volání Správce transakcí během druhé fáze transakce, pokud transakce je v pochybnosti.

Poznámka

Měli byste si uvědomit, že oznámení se nemusí posílat postupně nebo v určitém pořadí.

Metody

Commit(Enlistment)

Oznámí zařazený objekt, že transakce je potvrzena.

InDoubt(Enlistment)

Upozorní zařazený objekt, že stav transakce je v pochybnosti.

Prepare(PreparingEnlistment)

Upozorní zařazený objekt, že transakce je připravována k závazku.

Rollback(Enlistment)

Oznámí zařazený objekt, že transakce je vrácena zpět (přerušeno).

Platí pro

Viz také