即使未调用 System.Transactions.TransactionScope.Commit() 也提交了数据

Data committed even though System.Transactions.TransactionScope.Commit() not called(即使未调用 System.Transactions.TransactionScope.Commit() 也提交了数据)
本文介绍了即使未调用 System.Transactions.TransactionScope.Commit() 也提交了数据的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着跟版网的小编来一起学习吧!

问题描述

在什么情况下,包裹在 System.Transactions.TransactionScope 中的代码仍然可以提交,即使抛出了异常并且最外层的作用域从未被调用过?

Under what circumstances can code wrapped in a System.Transactions.TransactionScope still commit, even though an exception was thrown and the outermost scope never had commit called?

using (var tx = new TransactionScope()) 中有一个顶级方法,它以相同的方式调用也使用 TransactionScope 的方法.

There is a top-level method wrapped in using (var tx = new TransactionScope()), and that calls methods that also use TransactionScope in the same way.

我正在使用带有关联表适配器的类型化数据集.可能是由于某种原因适配器中的命令没有被登记吗?你们中有人知道如何检查他们是否在环境 TransactionScope 中登记吗?

I'm using typed datasets with associated tableadapters. Could it be that the commands in the adapter aren't enlisting for some reason? Do any of you know how one might check whether they are enlisting in the ambient TransactionScope or not?

推荐答案

结果是因为我创建了 TransactionScope 对象 after SqlConnection 对象.

The answer turned out to be because I was creating the TransactionScope object after the SqlConnection object.

我是从这里搬过来的:

using (new ConnectionScope())
using (var transaction = new TransactionScope())
{
    // Do something that modifies data

    transaction.Complete();
}

为此:

using (var transaction = new TransactionScope())
using (new ConnectionScope())
{
    // Do something that modifies data

    transaction.Complete();
}

现在它起作用了!

所以这个故事的寓意是创建您的TransactionScope 首先.

So the moral of the story is to create your TransactionScope first.

这篇关于即使未调用 System.Transactions.TransactionScope.Commit() 也提交了数据的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!

本站部分内容来源互联网,如果有图片或者内容侵犯您的权益请联系我们删除!

相关文档推荐

ActiveDirectory error 0x8000500c when traversing properties(遍历属性时 ActiveDirectory 错误 0x8000500c)
search by samaccountname with wildcards(使用通配符按 samaccountname 搜索)
Get the list of Groups for the given UserPrincipal(获取给定 UserPrincipal 的组列表)
Can you find an Active Directory User#39;s Primary Group in C#?(你能在 C# 中找到 Active Directory 用户的主要组吗?)
How to register System.DirectoryServices for use in SQL CLR User Functions?(如何注册 System.DirectoryServices 以在 SQL CLR 用户函数中使用?)
Query From LDAP for User Groups(从 LDAP 查询用户组)