如何组织交易?
我通过“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 编写的。
我自己看到两种解决方案
- < p>使用 try/catch:
void DoLogic11(...) { 计算一些数据(...); 保存计算数据(...);
<前><代码>尝试 { DoRequestToExternalService(...); } 捕获(异常除外) { 日志错误(...); }}
方法的缺点是我向调用者隐藏了异常。但我想将错误作为异常传递到外部(要记录等)。
- 使用“嵌套交易”,但我不确定它是如何工作的。
这是我的愿景应该是:
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
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).
- 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
我不确定我理解是否正确。你能把所有的逻辑方法放在一个 try-catch 中吗?每次调用都是使用 TransactionScopeOption.RequiresNew 的单独事务
你可以使用投掷吗?
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
Can you use throw?
我决定更改“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.