cod*_*ike 5 c# nhibernate concurrency sharp-architecture
我有一个S#arp架构应用程序,它实现了一个轻量级的队列处理工具,各种线程从列表中提取实体并设置其状态以标记处理已在这些项上启动的事实.
尽管将开始处理位包装在显式事务中并使用C#lock(),但我仍然有时会在同一时间启动它们.
我后悔没有使用MSMQ ......好吧,是的,但现在这种并发行为让我感到困惑.很明显,我对NHibernate事务和刷新有些不了解.你能帮我吗?
这是相关的代码:
private static object m_lock = new object();
private bool AbleToStartProcessing(int thingId)
{
bool able = false;
try
{
lock (m_lock)
{
this.thingRepository.DbContext.BeginTransaction();
var thing = this.thingRepository.Get(thingId);
if (thing.Status == ThingStatusEnum.PreProcessing)
{
able = true;
thing.Status = ThingStatusEnum.Processing;
}
else
{
logger.DebugFormat("Not able to start processing {0} because status is {1}",
thingId, thing.Status.ToString());
}
this.thingRepository.DbContext.CommitTransaction();
}
}
catch (Exception ex)
{
this.thingRepository.DbContext.RollbackTransaction();
throw ex;
}
if (able)
logger.DebugFormat("Starting processing of {0}",
thingId);
return able;
}
Run Code Online (Sandbox Code Playgroud)
我原以为这可以保证只有一个线程可以同时改变"事物"的状态,但我会在我的日志中定期得到这个:
2011-05-18 18:41:23,557 thread41 DEBUG src:MyApp.Blah.ThingJob - Starting processing of 78090
2011-05-18 18:41:23,557 thread51 DEBUG src:MyApp.Blah.ThingJob - Starting processing of 78090
Run Code Online (Sandbox Code Playgroud)
..然后两个线程尝试操作相同的东西,并创建一个烂摊子.
我错过了什么?谢谢.
编辑:更改代码以反映我的日志记录在真实版本中的工作方式
| 归档时间: |
|
| 查看次数: |
1076 次 |
| 最近记录: |