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

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

问题描述

下面是一个小实验我所做的:

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

当我命中该断点3行后,我去一个SQL查询窗口并手动更改为更新的价值。然后,我继续运行。 LINQ的不重装我的对象,而是重新使用一个它以前曾在内存!这是一个数据并发问题!

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!

你如何禁用的LINQ显然是保持在内存中的对象这个隐藏缓存?

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

修改 - 在反思,这简直不可想象,微软可能会在LINQ的框架已经留下了这样一个巨大的鸿沟。上面的代码是什么我实际上做了简单化下来的版本,而且有可能是我已经错过了一点微妙之处。总之,我会很感激,如果你会做自己的实验来验证上述我的发现是正确的。另外,必须有某种秘密开关,使LINQ的健壮的并发数据更新。不过什么?

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?

推荐答案

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

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-缓存-gotcha.aspx

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

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