TransactionScope:如何读回已提交事务的结果?

发布于 2024-11-06 06:27:03 字数 350 浏览 0 评论 0原文

我想在分布式事务中工作,提交它,然后能够读取结果。类似于:

using( var ts = new TransactionScope() )
{
do work on connection A
do work on connection B

ts.Complete();
}

read back work on A
read back work on B

这不能一致地工作,因为一旦所有资源都表示将提交(第 2 阶段开始),TransactionScope 就会结束,而不是它们已经提交(第 2 阶段结束)并且提交发生在不同的线程上。

有没有办法强制同步提交?或者我应该使用其他一些模式来读回结果?

I want to do work in a distributed transaction, commit it, then be able to read the results. Something like:

using( var ts = new TransactionScope() )
{
do work on connection A
do work on connection B

ts.Complete();
}

read back work on A
read back work on B

This doesn't work consistently because the TransactionScope ends as soon as all the resources have said they will commit (start of phase 2), not that they have committed (end of phase 2) and the commits happen on a different thread.

Is there some way to force the commit to be synchronous? Or some other pattern I should be using to read back the results?

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

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

发布评论

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

评论(1

生来就爱笑 2024-11-13 06:27:03

通常,我希望这能起作用,因为在提交(或回滚)之前您应该被阻止。您是否可能使用锁避免机制(nolock 等)?您始终可以在另一个可序列化事务(即第二个 TransactionScope)中进行读取 - 这应该确保读取是正确的。

Normally, I would expect this to work, since you should be blocked until it is committed (or rolled back). Are you perhaps using lock-avoiding mechanisms (nolock etc)? You could always do the reads in another Serializable transaction (i.e. a second TransactionScope) - that should ensure the reads are true.

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