简体   繁体   English

当两个成员试图访问它时如何锁定记录?

[英]How to lock a record when two members are trying to access it?

I have the scenario like this, 我有这样的场景,

My environment is .Net2.0, VS 2008, Web Application 我的环境是.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, 我们可以用两种方式做到,

  1. 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. 通过前端(将sessionID和记录唯一编号放在字典中并将其保留为静态或应用程序变量),我们将在响应退出该页面时释放,客户端未连接,单击后发布按钮后会议结束了。

  2. By backend (record locking in the DB itself - need to study - my team member is looking ). 通过后端(记录锁定在DB本身 - 需要学习 - 我的团队成员正在寻找)。

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? 我错过了任何条件吗?

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). 实际实现方式取决于您使用的数据库技术(ADO.Net,DataSet,Linq,EF等)。

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 . 这种模式也描述了EAA的乐观乐观离线锁定

If your talking about only reading data from a record from SQL server database, you don't need to do anything!!! 如果您只谈论从SQL服务器数据库中读取记录中的数据,则无需执行任何操作! SQL server will do everything about managing multi access to records. SQL Server将完成有关管理对记录的多重访问的所有操作。 but if you want to manipulate data, you have to use Transaction s. 但如果你想操纵数据,你必须使用Transaction s。

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. 创建一个名称为IsInUse的列作为位类型,如果正在访问,则将其设置为true。 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. 由于其他人同时也需要相同的数据,所以你需要保存你的应用程序免于崩溃..所以在检索数据的每个地方你必须检查IsInUse是否为假。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM