IEnlistmentNotification Rozhraní
Definice
Důležité
Některé informace platí pro předběžně vydaný produkt, který se může zásadně změnit, než ho výrobce nebo autor vydá. Microsoft neposkytuje žádné záruky, výslovné ani předpokládané, týkající se zde uváděných informací.
Popisuje rozhraní, které by měl správce prostředků implementovat, aby poskytoval dvě fáze potvrzení oznámení zpětné volání pro správce transakcí při zařazení pro účast.
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í a také zařazení objektu jako účastníka 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
Aby se správce prostředků mohl účastnit transakce, musí zařazení do transakce prostřednictvím správce transakcí. Třída Transaction definuje sadu metod, jejichž názvy začínají Enlist , které poskytují tuto funkci. Různé Enlist metody odpovídají různým typům zařazení, které může mít správce prostředků.
Tato třída popisuje rozhraní, které by správce prostředků měl implementovat, aby poskytoval dvě fáze potvrzení oznámení zpětné volání pro správce transakcí při zařazení pro účast. Pro implementaci IEnlistmentNotification rozhraní každého správce prostředků byste ho měli přidat pomocí EnlistVolatile metody nebo EnlistDurable metody Transaction třídy v závislosti na tom, zda je váš 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é .
Správce transakcí upozorní zařazený objekt v různých fázích dvoufázového commit protokolu následujícími metodami.
| Metoda | Description |
|---|---|
| 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í požádá účastníky, 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 je transakce 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á vrácen zpět). |
| InDoubt | 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 pochybnosti. |
Note
Měli byste vědět, že oznámení se nemusí posílat postupně nebo v určitém pořadí.
Metody
| Name | Description |
|---|---|
| Commit(Enlistment) |
Upozorní zařazený objekt, že transakce je potvrzena. |
| InDoubt(Enlistment) |
Upozorní zařazený objekt, že stav transakce je pochybnosti. |
| Prepare(PreparingEnlistment) |
Upozorní zařazený objekt, že transakce je připravena na závazek. |
| Rollback(Enlistment) |
Upozorní na zařazený objekt, že transakce se vrací zpět (přerušeno). |