IEnlistmentNotification Interface
Definição
Descreve uma interface que um gerenciador de recursos deve implementar para fornecer dois retornos de chamada de notificação de confirmação de fase para o gerenciador de transação após a inscrição para a participação.Describes an interface that a resource manager should implement to provide two phase commit notification callbacks for the transaction manager upon enlisting for participation.
public interface class IEnlistmentNotification
public interface IEnlistmentNotification
type IEnlistmentNotification = interface
Public Interface IEnlistmentNotification
- Derivado
Exemplos
O exemplo a seguir mostra uma implementação dessa interface, bem como a lista de objetos como um participante em uma transação usando o EnlistVolatile método.The following example shows an implementation of this interface, as well as enlisting the object as a participant in a transaction using the EnlistVolatile method.
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
Comentários
Para que um Gerenciador de recursos participe de uma transação, ele deve se inscrever na transação por meio do Gerenciador de transações.In order for a resource manager to participate in a transaction, it must enlist in the transaction through the transaction manager. O Transaction classe define um conjunto de métodos cujos nomes começam com Enlist que fornecem essa funcionalidade.The Transaction class defines a set of methods whose names begin with Enlist that provide this functionality. As diferentes Enlist métodos correspondem aos diferentes tipos de inscrição que pode ter um Gerenciador de recursos.The different Enlist methods correspond to the different types of enlistment that a resource manager may have.
Essa classe descreve uma interface que um Gerenciador de recursos deve implementar para fornecer dois retornos de chamada de notificação de confirmação de fase para o Gerenciador de transações ao inscrever-se para participação.This class describes an interface that a resource manager should implement to provide two phase commit notification callbacks for the transaction manager upon enlisting for participation. Para cada implementação do Gerenciador de recursos da IEnlistmentNotification interface, você deve inscrever-se usando o EnlistVolatile método ou o EnlistDurable método da Transaction classe, dependendo se seu recurso é volátil ou durável.For each resource manager's implementation of the IEnlistmentNotification interface, you should enlist it using the EnlistVolatile method or the EnlistDurable method of the Transaction class, depending on whether your resource is volatile or durable. Para obter mais informações sobre inscrição e 2PC, consulte Inscrevendo recursos como participantes em uma transação e confirmando uma transação em fase única e várias fases , respectivamente.For more information on enlistment and 2PC, see Enlisting Resources as Participants in a Transaction and Committing a Transaction in Single-Phase and Multi-Phase respectively.
O Gerenciador de transações notifica o objeto inscrito em diferentes fases do protocolo de confirmação de duas fases pelos métodos a seguir.The transaction manager notifies the enlisted object at different phases of the Two Phase Commit Protocol by the following methods.
| MétodoMethod | DescriçãoDescription |
|---|---|
| Prepare | Esse método de um objeto inscrito é usado como um retorno de chamada pelo Gerenciador de transações durante a primeira fase de uma transação, quando o Gerenciador de transações solicita aos participantes se eles podem confirmar a transação.This method of an enlisted object is used as a callback by the Transaction Manager during the first phase of a transaction, when the transaction manager asks participants whether they can commit the transaction. |
| Commit | Esse método de um objeto inscrito é usado como um retorno de chamada pelo Gerenciador de transações durante a segunda fase de uma transação se a transação for confirmada.This method of an enlisted object is used as a callback by the Transaction Manager during the second phase of a transaction if the transaction is committed. |
| Rollback | Esse método de um objeto inscrito é usado como um retorno de chamada pelo Gerenciador de transações durante a segunda fase de uma transação se a transação for anulada (ou seja, revertida).This method of an enlisted object is used as a callback by the Transaction Manager during the second phase of a transaction if the transaction is aborted (that is, rolled back). |
| InDoubt | Esse método de um objeto inscrito é usado como um retorno de chamada pelo Gerenciador de transações durante a segunda fase de uma transação se a transação estiver em dúvida.This method of an enlisted object is used as a callback by the Transaction Manager during the second phase of a transaction if the transaction is in doubt. |
Observação
Você deve estar ciente de que as notificações podem não ser enviadas sequencialmente ou em uma ordem específica.You should be aware that notifications might not be sent sequentially, or in a particular order.
Métodos
| Commit(Enlistment) |
Notifica um objeto inscrito de que uma transação está sendo confirmada.Notifies an enlisted object that a transaction is being committed. |
| InDoubt(Enlistment) |
Notifica um objeto inscrito de que o status de uma transação está duvidoso.Notifies an enlisted object that the status of a transaction is in doubt. |
| Prepare(PreparingEnlistment) |
Notifica um objeto inscrito de que uma transação está sendo preparada para confirmação.Notifies an enlisted object that a transaction is being prepared for commitment. |
| Rollback(Enlistment) |
Notifica um objeto inscrito de que uma transação está sendo revertida (anulada).Notifies an enlisted object that a transaction is being rolled back (aborted). |