IEnlistmentNotification 인터페이스

정의

리소스 관리자가 참여를 위해 등록할 때 트랜잭션 관리자에 대한 2단계 커밋 알림 콜백을 제공하기 위해 구현해야 하는 인터페이스에 대해 설명합니다.

public interface class IEnlistmentNotification
public interface IEnlistmentNotification
type IEnlistmentNotification = interface
Public Interface IEnlistmentNotification
파생

예제

다음 예제에서는 이 인터페이스의 구현을 보여 뿐만 아니라 메서드를 사용 하 여 트랜잭션에 참가자로 개체를 EnlistVolatile 등록 합니다.

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

설명

리소스 관리자가 트랜잭션에 참여하려면 트랜잭션 관리자를 통해 트랜잭션에 참여해야 합니다. 클래스는 Transaction 이 기능을 제공하는 이름으로 시작하는 Enlist 메서드 집합을 정의합니다. 각 Enlist 메서드는 리소스 관리자가 가질 수 있는 다른 인리스트먼트 형식에 해당합니다.

이 클래스는 리소스 관리자가 참여 등록 시 트랜잭션 관리자에 대해 2단계 커밋 알림 콜백을 제공하기 위해 구현해야 하는 인터페이스에 대해 설명합니다. 각 리소스 관리자의 인터페이스 구현 IEnlistmentNotification 에 대해 리소스가 휘발성인지 지속성인지에 따라 클래스의 Transaction 메서드 또는 EnlistDurable 메서드를 사용하여 EnlistVolatile 인리스트먼트해야 합니다. 인리스트먼트 및 2PC에 대한 자세한 내용은 리소스 를 트랜잭션의 참가자로 등록 하고 Single-Phase 및 다단계에서 트랜잭션 커밋을 각각 참조하세요.

트랜잭션 관리자는 다음 방법으로 2단계 커밋 프로토콜의 여러 단계에서 인리스트먼트된 개체를 알 수 있습니다.

메서드 설명
Prepare 인리스트먼트된 개체의 이 메서드는 트랜잭션 관리자가 트랜잭션의 첫 번째 단계에서 트랜잭션 관리자가 참가자에게 트랜잭션을 커밋할 수 있는지 여부를 묻는 콜백으로 사용됩니다.
Commit 인리스트먼트된 개체의 이 메서드는 트랜잭션이 커밋된 경우 트랜잭션의 두 번째 단계에서 트랜잭션 관리자에 의해 콜백으로 사용됩니다.
Rollback 인리스트먼트된 개체의 이 메서드는 트랜잭션이 중단된 경우(즉, 롤백된) 트랜잭션의 두 번째 단계에서 트랜잭션 관리자에 의해 콜백으로 사용됩니다.
InDoubt 인리스트먼트된 개체의 이 메서드는 트랜잭션이 확실하지 않은 경우 트랜잭션의 두 번째 단계에서 트랜잭션 관리자가 콜백으로 사용합니다.

참고

알림은 순차적으로 또는 특정 순서로 전송되지 않을 수 있습니다.

메서드

Commit(Enlistment)

참여한 개체에게 트랜잭션이 커밋 중임을 알립니다.

InDoubt(Enlistment)

트랜잭션이 의심스러운 상태임을 참여한 개체에게 알립니다.

Prepare(PreparingEnlistment)

트랜잭션이 커밋 준비 중임을 참여한 개체에게 알립니다.

Rollback(Enlistment)

트랜잭션이 롤백(중단) 중임을 참여한 개체에게 알립니다.

적용 대상

추가 정보