CommittableTransaction.Commit 方法

定义

尝试提交事务。Attempts to commit the transaction.

public:
 void Commit();
public void Commit ();
member this.Commit : unit -> unit
Public Sub Commit ()

例外

在事务上调用了 Commit(),并且该事务变为 InDoubtCommit() is called on a transaction and the transaction becomes InDoubt.

调用了 Commit(),并且该事务第一次回滚。Commit() is called and the transaction rolls back for the first time.

注解

调用此方法时,将轮询已注册到事务中的所有对象,并可单独指示它们对提交或回滚事务的投票。When this method is called, all objects that have registered to participate in the transaction are polled and can independently indicate their vote to either commit or roll back the transaction. 如果任何参与者投票要回滚事务,则会回滚该事务,并且此方法会引发 TransactionException 异常。If any participant votes to roll back the transaction, it is rolled back and this method throws a TransactionException exception. 这是事务的正常出现,你的代码应捕获并处理此类异常。This is a normal occurrence for a transaction and your code should catch and process such exceptions.

CommitEndCommit 会一直阻止,直到完成事务处理的第一个阶段。Commit and EndCommit block until the first phase of transaction processing is complete. 第一阶段在事务中的所有资源管理器和登记已经对事务结果进行了投票之后结束,并且 TransactionManager 持久决定提交或中止事务。The first phase ends after all resource managers and enlistments in the transaction have voted on the transaction outcome and the TransactionManager has durably decided to commit or abort the transaction. 处理过程的第二个阶段始终是异步的。The second phase of processing is always asynchronous. 因此,无法保证刚从给定事务中提交的数据在不使用另一个事务查看此数据时立即可用。Therefore, there is no guarantee that data just committed from within a given transaction will be immediately available afterwards when not using another transaction to view this data.

由于此方法在事务处理的第一阶段完成之前一直处于阻止状态,因此在 Windows 窗体(WinForm)应用程序中使用此方法时应非常小心,否则会发生死锁。Because this method blocks until the first phase of transaction processing is complete, you should be extremely careful when using this method in a Windows Form (WinForm) application, or a deadlock can occur. 如果在一个 WinForm 控制事件(例如单击按钮)中调用此方法,并使用同步 Invoke 方法指示控件在处理事务过程中执行某些 UI 任务(例如,更改颜色),则会发生死锁。If you call this method inside one WinForm Control event (for example, clicking a button), and use the synchronous Invoke method to direct the control to perform some UI tasks (for example, changing colors) in the middle of processing the transaction, a deadlock will happen. 这是因为 Invoke 方法是同步的,并会阻止工作线程,直到 UI 线程完成其作业为止。This is because the Invoke method is synchronous and blocks the worker thread until the UI thread finishes its job. 但是,在我们的方案中,UI 线程还会等待工作线程提交事务。However, in our scenario, the UI thread is also waiting for the worker thread to commit the transaction. 结果是,无可以继续,并且范围会无限期等待提交完成。The result is that none is able to proceed and the scope waits indefinitely for the Commit to finish. 你应尽可能使用 BeginInvoke 而不是 Invoke,因为它是异步的,因此不容易出现死锁。You should use BeginInvoke rather than Invoke wherever possible, because it is asynchronous and thus less prone to deadlock.

适用于