TransactionScope:如何读回已提交事务的结果?
我想在分布式事务中工作,提交它,然后能够读取结果。类似于:
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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
通常,我希望这能起作用,因为在提交(或回滚)之前您应该被阻止。您是否可能使用锁避免机制(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.