Transaction.EnlistVolatile 方法
定义
登记可变资源管理器以参与事务。Enlists a volatile resource manager to participate in a transaction.
重载
| EnlistVolatile(IEnlistmentNotification, EnlistmentOptions) |
登记在事务中支持两阶段提交参与的易失性资源管理器。Enlists a volatile resource manager that supports two phase commit to participate in a transaction. |
| EnlistVolatile(ISinglePhaseNotification, EnlistmentOptions) |
登记支持单阶段提交优化的可变资源管理器以参与事务。Enlists a volatile resource manager that supports single phase commit optimization to participate in a transaction. |
注解
可变资源管理器无法从故障中恢复,因而无法完成参与的事务。Volatile resource managers cannot recovery from failure to complete a transaction in which they were participating. 有关可变资源和持久资源以及如何登记资源的详细信息,请参阅 实现资源管理器。For more information on volatile and durable resources, as well as how to enlist a resource, see Implementing A Resource Manager. 有关资源管理器如何响应提交通知并准备提交的详细信息,请参阅 在 Single-Phase 和多阶段中提交事务。For more information on how a resource manager responds to commit notification and prepare the commit, see Committing A Transaction In Single-Phase and Multi-Phase.
EnlistVolatile(IEnlistmentNotification, EnlistmentOptions)
登记在事务中支持两阶段提交参与的易失性资源管理器。Enlists a volatile resource manager that supports two phase commit to participate in a transaction.
public:
System::Transactions::Enlistment ^ EnlistVolatile(System::Transactions::IEnlistmentNotification ^ enlistmentNotification, System::Transactions::EnlistmentOptions enlistmentOptions);
public:
System::Transactions::Enlistment ^ EnlistVolatile(System::Transactions::IEnlistmentNotification ^ notification, System::Transactions::EnlistmentOptions options);
public System.Transactions.Enlistment EnlistVolatile (System.Transactions.IEnlistmentNotification enlistmentNotification, System.Transactions.EnlistmentOptions enlistmentOptions);
public System.Transactions.Enlistment EnlistVolatile (System.Transactions.IEnlistmentNotification notification, System.Transactions.EnlistmentOptions options);
member this.EnlistVolatile : System.Transactions.IEnlistmentNotification * System.Transactions.EnlistmentOptions -> System.Transactions.Enlistment
member this.EnlistVolatile : System.Transactions.IEnlistmentNotification * System.Transactions.EnlistmentOptions -> System.Transactions.Enlistment
Public Function EnlistVolatile (enlistmentNotification As IEnlistmentNotification, enlistmentOptions As EnlistmentOptions) As Enlistment
Public Function EnlistVolatile (notification As IEnlistmentNotification, options As EnlistmentOptions) As Enlistment
参数
- enlistmentNotificationnotification
- IEnlistmentNotification
实现 IEnlistmentNotification 接口,以接收两阶段提交通知的对象。An object that implements the IEnlistmentNotification interface to receive two-phase commit notifications.
- enlistmentOptionsoptions
- EnlistmentOptions
如果资源管理器在准备阶段想要执行额外工作,则为 EnlistDuringPrepareRequired。EnlistDuringPrepareRequired if the resource manager wants to perform additional work during the prepare phase.
返回
描述登记的 Enlistment 对象。An Enlistment object that describes the enlistment.
示例
下面的示例演示接口的实现 IEnlistmentNotification ,并使用方法将对象作为事务中的参与者登记 EnlistVolatile 。The following example shows an implementation of IEnlistmentNotification 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
注解
可变资源管理器无法从故障中恢复,因而无法完成参与的事务。Volatile resource managers cannot recovery from failure to complete a transaction in which they were participating. 若要获取事务中的持久登记,请使用 EnlistDurable 方法。To obtain a durable enlistment in a transaction, use the EnlistDurable method.
通过此方法登记以参与事务的资源管理器接收两阶段提交通知,它们对应于接口上定义的方法 IEnlistmentNotification 。Resource managers enlisted for participation in a transaction through this method receive two phase commit notifications that correspond to the methods defined on the IEnlistmentNotification interface.
适用于
EnlistVolatile(ISinglePhaseNotification, EnlistmentOptions)
登记支持单阶段提交优化的可变资源管理器以参与事务。Enlists a volatile resource manager that supports single phase commit optimization to participate in a transaction.
public:
System::Transactions::Enlistment ^ EnlistVolatile(System::Transactions::ISinglePhaseNotification ^ singlePhaseNotification, System::Transactions::EnlistmentOptions enlistmentOptions);
public:
System::Transactions::Enlistment ^ EnlistVolatile(System::Transactions::ISinglePhaseNotification ^ notification, System::Transactions::EnlistmentOptions options);
public System.Transactions.Enlistment EnlistVolatile (System.Transactions.ISinglePhaseNotification singlePhaseNotification, System.Transactions.EnlistmentOptions enlistmentOptions);
public System.Transactions.Enlistment EnlistVolatile (System.Transactions.ISinglePhaseNotification notification, System.Transactions.EnlistmentOptions options);
member this.EnlistVolatile : System.Transactions.ISinglePhaseNotification * System.Transactions.EnlistmentOptions -> System.Transactions.Enlistment
member this.EnlistVolatile : System.Transactions.ISinglePhaseNotification * System.Transactions.EnlistmentOptions -> System.Transactions.Enlistment
Public Function EnlistVolatile (singlePhaseNotification As ISinglePhaseNotification, enlistmentOptions As EnlistmentOptions) As Enlistment
Public Function EnlistVolatile (notification As ISinglePhaseNotification, options As EnlistmentOptions) As Enlistment
参数
- singlePhaseNotificationnotification
- ISinglePhaseNotification
实现 ISinglePhaseNotification 接口的对象,该对象必须能够接收单阶段提交和两阶段提交通知。An object that implements the ISinglePhaseNotification interface that must be able to receive single phase commit and two phase commit notifications.
- enlistmentOptionsoptions
- EnlistmentOptions
如果资源管理器在准备阶段想要执行额外工作,则为 EnlistDuringPrepareRequired。EnlistDuringPrepareRequired if the resource manager wants to perform additional work during the prepare phase.
返回
描述登记的 Enlistment 对象。An Enlistment object that describes the enlistment.
注解
可变资源管理器无法从故障中恢复,因而无法完成参与的事务。Volatile resource managers cannot recovery from failure to complete a transaction in which they were participating. 若要获取事务中的持久登记,请使用 EnlistDurable 方法。To obtain a durable enlistment in a transaction, use the EnlistDurable method. 有关可变资源和持久资源以及如何登记资源的详细信息,请参阅 实现资源管理器。For more information on volatile and durable resources, as well as how to enlist a resource, see Implementing A Resource Manager. 有关资源管理器如何响应提交通知并准备提交的详细信息,请参阅 在 Single-Phase 和多阶段中提交事务。For more information on how a resource manager responds to commit notification and prepare the commit, see Committing A Transaction In Single-Phase and Multi-Phase.
请注意,即使资源管理器实现使用此方法登记,也不能保证它收到单阶段提交。You should note that even when your resource manager implementation enlists with this method, it is not guaranteed that it receives a single phase commit. 事务管理器仍可以改为发送两阶段提交通知。The transaction manager can still send two phase commit notifications instead. 有关单阶段提交优化的详细信息,请参阅 使用单阶段提交和可提升的单阶段通知进行优化。For more information on the single phase commit optimization, see Optimization Using Single Phase Commit and Promotable Single Phase Notification.