编程语言
首页 > 编程语言> > c# – 如果在Using块的末尾未调用TransactionScope.Complete,则Transaction会发生什么

c# – 如果在Using块的末尾未调用TransactionScope.Complete,则Transaction会发生什么

作者:互联网

我使用的是SQL Server 2012,并且在TransactionScope Using块中有多个SQL连接到同一个数据库.但是,如果第一次更新SQL操作没有产生所需的输出,那么我将跳过对SQL操作的下一次调用,并且还调用在使用块结束时未调用的TransactionScope.Complete.

解决方法:

您需要的大部分信息都在这里很好地列出了Completing a transaction scope

When your application completes all the work it wants to perform in a
transaction, you should call the Complete method only once to inform
the transaction manager that it is acceptable to commit the
transaction. It is very good practice to put the call to Complete as
the last statement in the using block.

Failing to call this method aborts the transaction, because the
transaction manager interprets this as a system failure, or equivalent
to an exception thrown within the scope of the transaction. However,
calling this method does not guarantee that the transaction wil be
committed. It is merely a way of informing the transaction manager of
your status. After calling the Complete method, you can no longer
access the ambient transaction by using the Current property, and
attempting to do so will result in an exception being thrown.

标签:c,sql-server,ado-net,transactionscope
来源: https://codeday.me/bug/20190607/1194249.html