如何组织交易?

发布于 2024-11-02 06:36:40 字数 1284 浏览 0 评论 0原文

我通过“Guardian”方法调用了服务,该方法为每个请求打开 TransactionScope,并在一切正常的情况下完成该事务:

void ExecuteWorker(...)
{
    using (TransactionScope scope = new TransactionScope(TransactionScopeOption.RequiresNew))
    {
        ...CallLogicMethods...

        scope.Complete();
    }
}

其中一种方法与“外部”服务交互,如果该交互失败,我的所有事务都会失败还。因此,我没有保存所需的数据(在请求外部服务之前已计算)。

void DoLogic1(...)
{
    CalculateSomeData(...);
    SaveCalculatedData(...);

    DoRequestToExternalService(...);
}

解决该问题的最佳方法是什么?

应用程序是使用 C#、.NET 4.0、MS SQL 2008 编写的。

我自己看到两种解决方案

  1. < p>使用 try/catch:

    void DoLogic11(...) { 计算一些数据(...); 保存计算数据(...);

    <前><代码>尝试 { DoRequestToExternalService(...); } 捕获(异常除外) { 日志错误(...); }

    }

方法的缺点是我向调用者隐藏了异常。但我想将错误作为异常传递到外部(要记录等)。

  1. 使用“嵌套交易”,但我不确定它是如何工作的。

这是我的愿景应该是:

void DoLogic12(...)
{
    using (TransactionScope scopeNested = new TransactionScope(TransactionScopeOption.Suppress))
    {
        CalculateSomeData(...);
        SaveCalculatedData(...);
        scopeNested.Complete()
    }

    DoRequestToExternalService(...);        
}

我已经实现了它,尝试使用,但似乎只有在也提交外部的情况下才提交嵌套事务。

请指教。

I have services been called through the 'Guardian' method, that has TransactionScope opened for each request and complete that transaction if everything is fine:

void ExecuteWorker(...)
{
    using (TransactionScope scope = new TransactionScope(TransactionScopeOption.RequiresNew))
    {
        ...CallLogicMethods...

        scope.Complete();
    }
}

One of the methods interacts with 'External' service, and in case if that interaction fails all my transaction fails also. As a result, I don't save required data (been calculated before request to external service.

void DoLogic1(...)
{
    CalculateSomeData(...);
    SaveCalculatedData(...);

    DoRequestToExternalService(...);
}

What is the best way to resolve that issue?

Application is written using C#, .NET 4.0, MS SQL 2008.

Myself I see two solutions

  1. Using try/catch:

    void DoLogic11(...)
    {
    CalculateSomeData(...);
    SaveCalculatedData(...);

    try
    {
        DoRequestToExternalService(...);
    }
    catch(Exception exc)
    {
        LogError(...);
    }
    

    }

The lack of this approach is that I'm hiding exception from the caller. But I would like to pass error outside as an exception (to be logged, etc).

  1. Using 'Nested transcation', but I not sure how that works.

Here is my vision it should be:

void DoLogic12(...)
{
    using (TransactionScope scopeNested = new TransactionScope(TransactionScopeOption.Suppress))
    {
        CalculateSomeData(...);
        SaveCalculatedData(...);
        scopeNested.Complete()
    }

    DoRequestToExternalService(...);        
}

I've implemented that, tried to use, but it seems that nested transcation is committed only in case when external is committed also.

Please advise.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

许仙没带伞 2024-11-09 06:36:40

我不确定我理解是否正确。你能把所有的逻辑方法放在一个 try-catch 中吗?每次调用都是使用 TransactionScopeOption.RequiresNew 的单独事务

void DoLogic1(...)
{
    try
    {
        CalculateSomeData(...);
        SaveCalculatedData(...);
        DoRequestToExternalService(...);
    }
    catch(Exception ex)
    {
        LogException(...)
        throw;
    }
}

但我想将错误传递到外部
作为例外(要记录等)。

你可以使用投掷吗?

I am not sure I understood it correctly. Can you put all your logic methods in one try-catch? Each call is a separate transaction using TransactionScopeOption.RequiresNew

void DoLogic1(...)
{
    try
    {
        CalculateSomeData(...);
        SaveCalculatedData(...);
        DoRequestToExternalService(...);
    }
    catch(Exception ex)
    {
        LogException(...)
        throw;
    }
}

But I would like to pass error outside
as an exception (to be logged, etc).

Can you use throw?

春庭雪 2024-11-09 06:36:40

我决定更改“ExecuteWorker”方法以有条件地创建事务。因此,我能够在“DoLogicX”方法本身中创建事务。

I've decided to change my 'ExecuteWorker' method to create transaction conditionally. Therefore I'm able to create transaction in the 'DoLogicX' method itself.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文