关于TransactionScope.Complete方法(链接)

相信有些同学会困扰,当我们执行TransactionScope.Complete()方法的时候,是否Transaction被真正提交到数据库了。

对于这一点,MSDN上有这么一段描述:

If the TransactionScope object created the transaction initially, the actual work of committing the transaction by the transaction manager occurs after the last line of code in the using block. If it did not create the transaction, the commit occurs whenever Commit is called by the owner of the CommittableTransaction object. At that point the transaction manager calls the resource managers and informs them to either commit or rollback, based on whether the Complete method was called on the TransactionScope object.

请查看:Implementing an Implicit Transaction using Transaction Scope 特别是这个部分。同样TransactionScope.Complete的介绍文档也有说到这一点。

同样在TransactionScope.Dispose的介绍文档,也有说到:

Calling this method marks the end of the transaction scope. If the TransactionScope object created the transaction and Complete was called on the scope, the TransactionScope object attempts to commit the transaction when this method is called. 

所以,实际上TransactionScope提交Transaction到数据库的时间点是在using语句块的最后,也就是在调用TransactionScope.Dispose()方法的时候。

原文地址:https://www.cnblogs.com/OpenCoder/p/12268032.html