PreparingEnlistment.Prepared 方法
定义
指示可以提交事务。Indicates that the transaction can be committed.
public:
void Prepared();
public void Prepared ();
member this.Prepared : unit -> unit
Public Sub Prepared ()
示例
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 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
注解
在第一个 (准备两阶段提交的) 阶段,实现接口的方法的资源管理器将 Prepare IEnlistmentNotification 调用此方法以指示可以提交事务。In the first (prepare) phase of a two phase commit, a resource manager implementing the Prepare method of the IEnlistmentNotification interface, calls this method to indicate that the transaction can be committed.
资源管理器可以 Done 在调用此方法之前的任何时间调用方法。The resource manager can call the Done method at anytime before it has called this method. 这样,登记就会强制转换为只读投票,这意味着它会在事务上提交,但不需要接收最终结果。By doing so, the enlistment is casting a read only vote, meaning that it votes commit on the transaction but does not need to receive the final outcome.
一旦征用调用此方法,并且在它返回之前,另一个线程或此同一线程可能会调用同一登记方法(如) Rollback 以执行回滚。Once this method is called by an enlistment and before it returns, it is possible that another thread or this same thread could make a call into the same enlistment method such as Rollback to perform a rollback. 如果资源管理器实现在此方法返回之前不释放资源锁,这可能会导致死锁情况。This can result in a deadlock situation if the resource manager implementation does not release resource locks until after this method returns.