【发布时间】:2016-05-26 14:21:08
【问题描述】:
我们的 SQL Server 2014 数据库设置为 READ_COMMITTED_SNAPSHOT。
我们使用 MSMQ 和分布式事务(我们使用 MassTransit 2.10)
在我们系统的一部分中,我们从队列中读取一条消息,进行数据库更新,然后将一条新消息发布到队列中(全部在一个事务下)。
我们发现在处理下一条消息时似乎没有提交更新(它从同一个表中读取第一部分更新),即使我希望该消息仅在队列中同时更新数据库。当我们稍后查询表时,更新的数据按预期存在。这似乎只在我们负载高且很少发生时才会发生。
我们的代码的简化版本
// code that processes message 1
using (var scope = new TransactionScope(TransactionScopeOption.RequiresNew, new TransactionOptions() { Timeout = TimeSpan.FromMinutes(30), IsolationLevel = IsolationLevel.ReadCommitted })
{
MethodThatUpdatesTableX();
MethodThatCreatesMessage2();
scope.Complete();
}
// message picked up from MSMQ and then (this runs in different thread):
// code that process message 2
using (var scope = new TransactionScope(TransactionScopeOption.RequiresNew, new TransactionOptions() { Timeout = TimeSpan.FromMinutes(30), IsolationLevel = IsolationLevel.ReadCommitted })
{
MethodThatReadsFromTableX(); // so here it seems that changes made in MethodThatUpdatesTableX is sometimes (though rarely) not read
// other stuff
}
这是我的理解:
当范围被释放时,对表 X 的更改以及发布到队列的消息都会被提交
当MethodThatReadsFromTableX() 从表 XI 中读取时,预计会发生更改(在第一个会话完成之前不应创建会话,因为它无法从队列中获取消息)
我的预期正确吗?可能是什么问题?
【问题讨论】:
标签: c# sql-server msmq msdtc masstransit