IEnlistmentNotification インターフェイス
定義
重要
一部の情報は、リリース前に大きく変更される可能性があるプレリリースされた製品に関するものです。 Microsoft は、ここに記載されている情報について、明示または黙示を問わず、一切保証しません。
参加登録時にトランザクション マネージャーに対して 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 インターフェイスの実装ごとに、リソースが揮発性か非消耗品かに応じて、EnlistVolatile メソッドまたは EnlistDurable クラスの Transaction メソッドを使用して参加させる必要があります。 参加と 2PC の詳細については、「 リソースをトランザクションの参加者として参加させる 」および「 Single-Phase およびマルチフェーズでのトランザクションのコミット 」を参照してください。
トランザクション マネージャーは、次のメソッドを使用して、2 フェーズ コミット プロトコルのさまざまなフェーズで参加オブジェクトに通知します。
| Method | Description |
|---|---|
| Prepare | 参加オブジェクトのこのメソッドは、トランザクション マネージャーがトランザクションをコミットできるかどうかを参加者に求めるときに、トランザクション マネージャーによってトランザクション の最初のフェーズ中にコールバックとして使用されます。 |
| Commit | 参加オブジェクトのこのメソッドは、トランザクションがコミットされた場合、トランザクション の第 2 フェーズ中にトランザクション マネージャーによってコールバックとして使用されます。 |
| Rollback | 参加オブジェクトのこのメソッドは、トランザクションが中止 (つまりロールバック) された場合、トランザクション の第 2 フェーズ中にトランザクション マネージャーによってコールバックとして使用されます。 |
| InDoubt | 参加オブジェクトのこのメソッドは、トランザクションが不明な場合、トランザクション の第 2 フェーズ中にトランザクション マネージャーによってコールバックとして使用されます。 |
Note
通知が順番に、または特定の順序で送信されない可能性があることに注意する必要があります。
メソッド
| 名前 | 説明 |
|---|---|
| Commit(Enlistment) |
トランザクションがコミットされていることを参加オブジェクトに通知します。 |
| InDoubt(Enlistment) |
参加しているオブジェクトに、トランザクションの状態が不明であることを通知します。 |
| Prepare(PreparingEnlistment) |
トランザクションがコミットメント用に準備されていることを参加オブジェクトに通知します。 |
| Rollback(Enlistment) |
トランザクションがロールバック (中止) されていることを参加オブジェクトに通知します。 |