可以在当前TransactionScope之外,并插入数据 [英] Is it possible to step outside of the current TransactionScope and insert data

查看:119
本文介绍了可以在当前TransactionScope之外,并插入数据的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

请考虑以下内容:

using (var outerScope = new TransactionScope())
{
    InsertDataInTableOne();
    InsertDataInTableTwo();
    InsertDataInTableThree();
    outerScope.Complete();
}

现在我想要 InsertDataInTableOne outerScope 事务之外运行。这是一个简化的表示,因为 TransactionScope 在链中创建了几个调用,所以我不能只是调用 InsertDataInTableOne 以外的TransactionScope 创建。

Now I want to have InsertDataInTableOne to be run outside of the outerScope transaction. This is a simplified representation, as the TransactionScope is created several calls up the chain, so I can't just put the call to InsertDataInTableOne outside of the TransactionScope creation.

我也知道这可能不是一个好的做法,我们正在做一个体面的修复。但是我们现在需要这个快速修复。

I also know this might not be a good practice, and we're working on a decent fix. But we need this quick fix at this moment.

using (var outerScope = new TransactionScope())
{
    using (var innerScope = new TransactionScope(TransactionScopeOption.RequiresNew))
    {
        InsertDataInTableOne();
        innerScope.Complete();
    }
    InsertDataInTableTwo();
    InsertDataInTableThree();
    outerScope.Complete();
}

这没有用。我甚至试着先创建一个 TransactionScope 抑制,然后 RequiresNew

That didn't work. I even tried with creating a TransactionScope with Suppress first, and then the RequiresNew.

因此,可以立即在数据库中插入数据,有效地忽略了您处于 TransactionScope

So is it possible to insert data immediately in the database, effectively ignoring the fact that you are in a TransactionScope?

这些方法之外的连接(实际上,当输入被调用的服务时)。

The connection is made outside of these methods (actually, when entering the service that is called).

推荐答案

不知道这是否会帮助任何人,但你永远不知道。问题在于一个定制的公司框架。实现使得很难有嵌套事务,我相信。我们现在通过删除外部事务(这是几层向上)并在我们的块中创建两个单独的事务来修复它:

Not sure if this will help anyone, but you never know. The problem lies with a custom company-framework. The implementation makes it hard to have nested transactions, I believe. We fixed it now by removing the outer transaction (which is several layers up) and creating two separate transactions in our block:

using (var scopeOne = new TransactionScope())
{
    InsertDataInTableOne();
    scopeOne.Complete();
}

using (var scopeTwo = new TransactionScope())
{
    InsertDataInTableTwo();
    InsertDataInTableThree();
    scopeTwo.Complete();
}

同样,这是一个非常特定于我们的代码的解决方案。使用 RequiresNew 通常应该做的伎俩。所以如果它不工作,也许先看看你自己的代码);

Again, this is a solution very specific to our code. Using RequiresNew should normally do the trick. So if it doesn't work, maybe look into your own code first ;)

另一个原因为什么我不是一个巨大的粉丝do-it-all-company -frameworks。

Another reason why I'm not a huge fan of do-it-all-company-frameworks.

这篇关于可以在当前TransactionScope之外,并插入数据的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆