当两个成员试图访问一条记录时如何锁定该记录?
我有这样的场景,
我的环境是.Net2.0,VS 2008,Web应用程序
当两个成员试图同时访问时我需要锁定一条记录。
我们可以通过两种方式做到这一点,
通过前端(将 sessionID 并记录唯一编号放入字典中并将其保留为静态或应用程序变量),当响应离开该页面时,客户端将释放未连接,点击发布按钮后,会话退出。
通过后端(数据库本身的记录锁定 - 需要研究 - 我的团队成员正在寻找)。
还有其他方法可以做吗?我是否需要在每一步中考虑其他方法?
我是否缺少任何条件?
I have the scenario like this,
My environment is .Net2.0, VS 2008, Web Application
I need to lock a record when two members are trying to access at the same time.
We can do it in two ways,
By Front end (putting the sessionID and record unique number in the dictionary and keeping it as a static or application variable), we will release when the response is go out of that page, client is not connected, after the post button is clicked and session is out.
By backend (record locking in the DB itself - need to study - my team member is looking ).
Is there any others to ways to do and do I need to look at other ways in each and every steps?
Am I missing any conditions?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
您不会为客户端锁定记录,因为锁定记录超过几毫秒几乎是人们在数据库中可以做的最具破坏性的事情。您应该使用乐观并发:您检测记录自此以来是否发生了更改最后读取并重新尝试事务(例如,向用户重新显示屏幕)。具体如何实现,取决于您使用的数据库技术(ADO.Net、DataSets、Linq、EF 等)。
如果业务领域需要类似锁的行为,那么这些行为总是在数据库中实现为保留逻辑:当显示记录时,它被“保留”,以便其他用户无法尝试进行相同的事务。预订完成或超时或被取消。但是“保留”永远不会使用锁来完成,始终是从“可用”到“保留”或类似状态的显式更新。
EAA 的 P 中也描述了此模式:乐观离线锁定。
You do not lock records for clients, because locking a record for anything more than a few milliseconds is just about the most damaging thing one can do in a database. You should use instead Optimistic Concurrency: you detect if the record was changed since the last read and re-attempt the transaction (eg you re-display the screen to the user). How that is actually implemented, will depend on what DB technology you use (ADO.Net, DataSets, Linq, EF etc).
If the business domain requires lock-like behavior, those are always implemented as reservation logic in the database: when a record is displayed, it is 'reserved' so that no other users can attempt to make the same transaction. The reservation completes or times out or is canceled. But a 'reservation' is never done using locks, is always an explicit update of state from 'available' to 'reserved', or something similar.
This pattern is also describe din P of EAA: Optimistic Offline Lock.
如果您谈论的是仅从 SQL Server 数据库的记录中读取数据,那么您不需要执行任何操作! SQL Server 将完成有关管理对记录的多重访问的所有工作。但如果你想操作数据,你必须使用事务 。
If your talking about only reading data from a record from SQL server database, you don't need to do anything!!! SQL server will do everything about managing multi access to records. but if you want to manipulate data, you have to use Transactions.
我同意拉穆斯的观点。但如果你需要的话仍然可以。创建一个名称类似于 IsInUse 作为位类型的列,如果正在访问,则将其设置为 true。由于其他人也同时需要相同的数据,因此您需要避免应用程序崩溃..因此,在检索数据的每个地方,您都必须检查 IsInUse 是否为 False。
I agree with Ramus. But still if u need it. Create a column with name like IsInUse as bit type and set it true if one is accessing. Since other guys will also need same data at same time then u need to save your app from crash .. so at every place from where the data is retrieved you have to put a check if IsInUse is False or not.