IEnlistmentNotificationとは? わかりやすく解説

IEnlistmentNotification インターフェイス

メモ : このインターフェイスは、.NET Framework version 2.0 で新しく追加されたものです。

参加の登録時に、トランザクション マネージャに対して 2 フェーズ コミットの通知コールバックを行うために、リソース マネージャが実装する必要のあるインターフェイスを示します。

名前空間: System.Transactions
アセンブリ: System.Transactions (system.transactions.dll 内)
構文構文

Public Interface IEnlistmentNotification
Dim instance As IEnlistmentNotification
public interface IEnlistmentNotification
public interface class IEnlistmentNotification
public interface IEnlistmentNotification
public interface IEnlistmentNotification
解説解説

リソース マネージャがトランザクションに参加するには、トランザクション マネージャを介してトランザクションに参加する必要があります。Transaction クラスは、この機能を提供する、名前が Enlist から始まる一連のメソッドを定義します。異なる Enlist メソッドは、リソース マネージャが持つ異なる種類の参加リストにそれぞれ対応しています。

このクラスは、参加の登録時にトランザクション マネージャに対して 2 フェーズ コミットの通知コールバックを行うために、リソース マネージャが実装する必要のあるインターフェイスを示します。リソース マネージャの IEnlistmentNotification インターフェイスの各実装ごとに、リソースが揮発性であるか永続的であるかに応じて、Transaction クラスの EnlistVolatile メソッドまたは EnlistDurable メソッドを使用してリソース マネージャを参加させる必要があります。参加リストと 2PC の詳細については、それぞれ「トランザクションへの参加要素としてのリソースの登録」および「単一フェーズおよびマルチフェーズでのトランザクションのコミット」を参照してください。

トランザクション マネージャは、次のメソッドを使用して、2 フェーズ コミット プロトコルの異なるフェーズで、参加オブジェクトに通知を行います。

メモメモ

通知は、順番に、つまり特定の順序で送信されない可能性があることに注意してください。

使用例使用例

次の例では、このインターフェイスを実装し、EnlistVolatile メソッドを使用して、オブジェクトを参加要素としてトランザクションに参加させます。

    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

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();
    }
}
プラットフォームプラットフォーム
バージョン情報バージョン情報
参照参照

IEnlistmentNotification メソッド


IEnlistmentNotification メンバ




英和和英テキスト翻訳

英語⇒日本語日本語⇒英語

辞書ショートカット

すべての辞書の索引

「IEnlistmentNotification」の関連用語

IEnlistmentNotificationのお隣キーワード
検索ランキング

   

英語⇒日本語
日本語⇒英語
   



IEnlistmentNotificationのページの著作権

   
日本マイクロソフト株式会社日本マイクロソフト株式会社
© 2026 Microsoft.All rights reserved.

©2026 GRAS Group, Inc.RSS