RIA 真的存在并发问题吗?
在我提到的 Pluralsight RIA 服务的一个视频中,他们有一个这样的更新代码:
public void UpdateProspect(Prospect currentProspect)
{
currentProspect.LastUpdate = DateTime.Now;
ObjectContext.Prospects.AttachAsModified(currentProspect, ChangeSet.GetOriginal(currentProspect));
}
我的第一个问题是这个更新将如何导致问题?为了显示问题,他启动了两个 Silverlight 客户端,然后从第一个客户端更新了该项目,并且更新得很好。然后他转到第二个客户端并进行更新,然后抛出错误。
为什么 RIA 会抛出错误?和第一次更新有什么关系?我认为这确实是有问题的,我们需要再次编写一些特殊的代码来解决实体冲突,然后再次将批次提交到服务器。这合适吗?
他还将 ConcurrencyMode 设置为固定。我的第二个问题是什么时候将 ConcurrencyMode 设置为固定?默认情况下,模式设置为“无”。
In one of the videos of RIA services of Pluralsight I was referring, they had an update code like this:
public void UpdateProspect(Prospect currentProspect)
{
currentProspect.LastUpdate = DateTime.Now;
ObjectContext.Prospects.AttachAsModified(currentProspect, ChangeSet.GetOriginal(currentProspect));
}
My first question is how will this update cause problems? To show the problems he starts two Silverlight clients, then from first client he updates the item and it updates nicely. Then he goes to the second client and makes an update and it throws an error.
Why is RIA throwing errors? What has it got to do with the first update? This is really buggy I think and we need to again write some special code to resolve the EntityConflict and then again submit the batch to the server. Is this appropriate?
He also set the ConcurrencyMode to Fixed. My second question is when would you set the ConcurrencyMode to Fixed? By default, the mode is set to None.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
这是因为陈旧读取引起的竞争条件。考虑以下情况:
Prospect
的副本 - 让我们称之为版本 1LastUpdate
属性,该对象现在是版本 2Prospect
LastUpdate
属性。对于客户 B 来说,版本现在是版本 2,但是客户 B 的版本 2 与客户 A 的不同,这里的问题是 B 无法知道 A 进行了任何更改。抛出异常是为了防止竞争条件意外删除数据。
解决方案是捕获此异常并报告
Prospect
在您编辑时已更改,然后重新加载它。您可以在此处找到有关使用
ConcurrencyMode=fixed
的更多信息< /a>.This is because of a race condition caused by a Stale Read. Consider the following:
Prospect
- lets call it version 1LastUpdate
property, the object is now version 2Prospect
LastUpdate
property. To Client B, the version is now version 2 but Client B's version 2 is different from Client A'sProspect
- this would overwrite Client A's changes!The problem here is that B can't know that A has made any changes. The Exception is thrown to prevent the race condition from accidentally deleting data.
The solution is to catch this exception and report that the
Prospect
was changed while you were editing it, and then reload it.You can find more information about using
ConcurrencyMode=fixed
here.