Linq缓存数据值 - 主要并发问题? [英] Linq caching data values - major concurrency problem?

查看:168
本文介绍了Linq缓存数据值 - 主要并发问题?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这里有一个小实验:

  MyClass obj = dataContext.GetTable< MyClass>()。其中​​(x = > x.ID = 1)。 
Console.WriteLine(obj.MyProperty); // output =initial
Console.WriteLine(Waiting ...); //在这行之后放一个断点
obj = null;
obj = dataContext.GetTable< MyClass>()。其中​​(x => x.ID = 1).Single(); //与以前一样,但重新加载$ ​​b $ b Console.WriteLine(obj.MyProperty); // output still =initial
obj.MyOtherProperty =foo;
dataContext.SubmitChanges(); // throws concurrency exception

当我在第3行之后点击断点时,我去一个SQL查询窗口并手动将值更改为更新。然后我继续跑。 Linq不重新加载我的对象,但重新使用它以前在内存中的一个!这是数据并发的巨大问题!



如何禁用Linq显然在内存中保存的对象的隐藏缓存? p>

编辑 - 反思,微软可能在Linq框架中留下这样的鸿沟,这是不可想象的。上面的代码是我真正在做的一个dumbed-down版本,可能有一些微妙的,我错过了。总之,如果你自己做实验来验证我上面的发现是正确的,我很感激。或者,必须有某种秘密开关,使Linq鲁棒防止并发数据更新。但是什么?

解决方案

这不是我之前遇到的问题(因为我不倾向于保持DataContext开放很长一段时间),但看起来像是别人拥有:



http://www.rocksthoughts.com/blog/archive/2008/01/14/linq-to-sql -caching-gotcha.aspx


Here's a little experiment I did:

MyClass obj = dataContext.GetTable<MyClass>().Where(x => x.ID = 1).Single();
Console.WriteLine(obj.MyProperty); // output = "initial"
Console.WriteLine("Waiting..."); // put a breakpoint after this line
obj = null;
obj = dataContext.GetTable<MyClass>().Where(x => x.ID = 1).Single(); // same as before, but reloaded
Console.WriteLine(obj.MyProperty); // output still = "initial"
obj.MyOtherProperty = "foo";
dataContext.SubmitChanges(); // throws concurrency exception

When I hit the breakpoint after line 3, I go to a SQL query window and manually change the value to "updated". Then I carry on running. Linq does not reload my object, but re-uses the one it previously had in memory! This is a huge problem for data concurrency!

How do you disable this hidden cache of objects that Linq obviously is keeping in memory?

EDIT - On reflection, it is simply unthinkable that Microsoft could have left such a gaping chasm in the Linq framework. The code above is a dumbed-down version of what I'm actually doing, and there may be little subtleties that I've missed. In short, I'd appreciate if you'd do your own experimentation to verify that my findings above are correct. Alternatively, there must be some kind of "secret switch" that makes Linq robust against concurrent data updates. But what?

解决方案

This isn't an issue I've come across before (since I don't tend to keep DataContexts open for long periods of time), but it looks like someone else has:

http://www.rocksthoughts.com/blog/archive/2008/01/14/linq-to-sql-caching-gotcha.aspx

这篇关于Linq缓存数据值 - 主要并发问题?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆