IEnlistmentNotification 接口

定义

描述一个接口,资源管理器应实现该接口以在登记参与时为事务管理器提供两阶段提交通知回调。

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 方法对应于资源管理器可能具有的不同登记类型。

此类介绍资源管理器应实现的接口,以便在登记参与时为事务管理器提供两阶段提交通知回调。 对于每个资源管理器的IEnlistmentNotification接口实现,应使用 EnlistVolatile 类的 Transaction 方法或 EnlistDurable 方法来登记它,具体取决于资源是易失性资源还是持久性资源。 有关登记和 2PC 的详细信息,请参阅将 资源登记为事务中的参与者 和分别 在Single-Phase和多阶段提交事务

事务管理器通过以下方法通知在两阶段提交协议的不同阶段登记的对象。

方法 描述
Prepare 当事务管理器询问参与者是否可以提交事务时,事务管理器在事务的第一阶段使用登记对象的此方法作为回调。
Commit 如果已提交事务,则事务管理器在事务的第二阶段使用登记对象的此方法作为回调。
Rollback 如果事务在事务的第二阶段中止,则登记对象的此方法将用作事务管理器的回调, (即) 回滚。
InDoubt 如果事务不确定,则事务管理器在事务的第二阶段使用登记对象的此方法作为回调。

注意

应注意,通知可能不会按顺序或按特定顺序发送。

方法

Commit(Enlistment)

通知登记的对象事务正在提交。

InDoubt(Enlistment)

通知登记的对象事务的状态不确定。

Prepare(PreparingEnlistment)

通知登记的对象事务正在为提交做准备。

Rollback(Enlistment)

通知登记的对象事务正在回滚(中止)。

适用于

另请参阅