简体   繁体   English

在ASP.NET中存储记录锁定令牌

[英]Storing Record Lock Tokens in ASP.NET

In the application that I'm currently working on, it is possible for multiple users to want to edit something at the same time which means that we need to implement optimistic locking. 在我目前正在处理的应用程序中,多个用户可能希望同时编辑某些内容,这意味着我们需要实现乐观锁定。 However, for this application, the item being edited is a scientific protocol that contains records from multiple different tables in the database. 但是,对于此应用程序,正在编辑的项目是一种科学协议,其中包含来自数据库中多个不同表的记录。

As such, we want to be able to indicate the entire protocol has been locked for editing by a single user which leads to my question: would the preferred method of doing this be to event the edits at the database level (eg have a table with the unique id of the protocol and check to see if it has been locked) or would it be accepted to track the currently locked protocols on the web server itself in memory? 因此,我们希望能够指示整个协议已被锁定以供单个用户编辑,这导致了我的问题:这样做的首选方法是在数据库级别对事件进行编辑(例如,有一个表格,协议的唯一ID并检查它是否已被锁定)或是否可以接受在内存中跟踪Web服务器本身上当前锁定的协议?

Currently we only anticipate around 100 users (20 or so simultaneous) for the application, but that number may increase in the future so we are looking to use the most scalable option. 目前我们只预计应用程序的大约100个用户(同时大约20个),但是这个数字可能会在未来增加,因此我们希望使用最具扩展性的选项。

This question also really hinges on how well architected is your codebase? 这个问题也取决于你的代码库的架构有多好?

If all the calls to modify those records go through a single point of entry then yes I recommend keeping all locking code entirely in your application so you can keep your database as a dumb data store. 如果修改这些记录的所有调用都通过单个入口点,那么我建议将所有锁定代码完全保留在应用程序中,这样您就可以将数据库保存为哑数据存储。

If you have multiple points of entry that can modify your tables you're going to need to implement locking at the database level. 如果您有多个可以修改表的入口点,则需要在数据库级别实现锁定。

I would implement a table in the database that managed the locking. 我将在数据库中实现一个管理锁定的表。 For instance: 例如:

Rows: ProtocolID, EditingBeginDate, EditingEndDate 行:ProtocolID,EditingBeginDate,EditingEndDate

Then you can simply query when an edit function in the application is attempted and if the given protocol doesn't have a completed time frame then you know that it is still being edited by a given user. 然后,您可以简单地查询何时尝试应用程序中的编辑功能,如果给定的协议没有完成的时间范围,那么您知道它仍然由给定用户编辑。 You can implement a specific amount of time that the editing sessions should be closed as to prevent records from being permanently locked. 您可以实施特定时间段来关闭编辑会话,以防止记录被永久锁定。 Just a suggestion :-D 只是一个建议:-D

So, it seems you need coarse grained locks. 所以,你似乎需要粗粒锁。

If you want to have most scalable solution, you need to keep locking information either in database or distributed cache (distributed cache will be faster in this case). 如果您想拥有最具可扩展性的解决方案,则需要将锁定信息保存在数据库或分布式缓存中(在这种情况下,分布式缓存会更快)。 In-memory approach is not scalable at all - it is going to fail in case you need more servers. 内存中的方法根本不可扩展 - 如果您需要更多服务器,它将会失败。 Don't forget also to introduce lock time-outs to prevent possible deadlocks. 不要忘记引入锁定超时以防止可能的死锁。

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

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