Linq版本的SELECT FOR UPDATE [英] Linq version of SELECT FOR UPDATE

查看:232
本文介绍了Linq版本的SELECT FOR UPDATE的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

当代码更新某个表中的某一行时,我在Web应用程序中收到一个 ChangeConflictException 。最好的我可以告诉它似乎两个用户正在完成事务在同一确切的时间和乐观的并发只影响 SubmitChanges()方法,而不是做锁定



换句话说,我有一个这样的事务:

  Dim query = From表中的行_ 
其中row.ID =< blah> _
选择行
Dim result = query.Single()
result.COLUMN = 2
dataContext.SubmitChanges()
pre>

内部乐观并发锁定 SubmitChanges(),但如果记录被更改在 Single()之后和 SubmitChanges()之前抛出一个错误。



...至少这是我的理论...



有人知道一种方法来启动锁定 Single()而不是 SubmitChanges()

解决方案

不,你必须将你的事务包装在一个try / catch块,并处理抛出的异常。 Linq to SQL提供了大量的信息,使您能够以智能的方式处理异常。请参见 http://elsharpo.blogspot.com/2008 /04/handling-concurrency-conflicts-in-linq.html


I'm getting a ChangeConflictException in my web application when the code updates a certain row within a certain table. The best I can tell it seems as though two users are completing the transaction at the same exact time and optimistic concurrency only affect the SubmitChanges() method instead of doing the lock when the row is selected.

So in other words I have a transaction like this:

Dim query = From row in Table _
            Where row.ID = <blah> _
            Select row
Dim result = query.Single()
result.COLUMN = 2
dataContext.SubmitChanges()

The built-in optimistic concurrency locks the record when SubmitChanges() is called but if the record is changed after Single() and before SubmitChanges() then an error is thrown.

...at least that's my theory...

Does anyone know of a way to start the lock at the Single() call instead of just at SubmitChanges()?

解决方案

No, you have to wrap your transaction in a try/catch block, and deal with the exception that is thrown. Linq to SQL provides plenty of information to allow you to deal with the exception in an intelligent manner. See http://elsharpo.blogspot.com/2008/04/handling-concurrency-conflicts-in-linq.html

这篇关于Linq版本的SELECT FOR UPDATE的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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