简体   繁体   English

寻找有关分布式系统内记录锁定的建议

[英]Looking for a recommendation on record-locking within a distributed system

We're trying to come up with a recommended design pattern for our team when it comes to record locking. 在记录锁定方面,我们正在尝试为我们的团队提出一种推荐的设计模式。 The typical school of thought goes something like this: 1. User picks a record from a list 2. Lock the record with the user id 3. Load the locked record record (no lock, then someone beat ya to it). 典型的思路如下:1.用户从列表中选择一条记录2.用用户ID锁定该记录3.加载锁定的记录记录(不锁定,然后有人对其打败)。

Am I missing something, or does this appear to be the only way to do this? 我是否缺少某些东西,或者这似乎是唯一的方法? ((In our case Optimistic locking would prove cumbersome and confusing for the end users. Edits are often quite substantial.)) ((在我们的案例中,乐观锁定会给最终用户带来麻烦和混乱。通常,修改量非常大。)

The detail that could make your solution administration intensive is getting rid of locks after crashes or connectivity failures. 可能使您的解决方案管理更加繁琐的细节是在崩溃或连接失败后摆脱锁定。 That's where the tradeoff between optimistic and pessimistic locking really lies. 这就是乐观锁定和悲观锁定之间真正权衡的地方。 Manually merging, or redoing, edits when optimistic locking fails is a pain, but mopping up after crashes on pessimistic and persistent locking models creates its own headaches (as anyone who supported users of Pervasive backed accounting systems in the 90s will tell you at length given the opportunity) 当乐观锁定失败时,手动合并或重做编辑是一件痛苦的事情,但是悲观和持久锁定模型崩溃后出现的麻烦会让人感到头痛(因为任何在90年代支持Pervasive支持的会计系统用户的人都会在很长的时间内告诉您机会)

One answer is to use your RDBMS's mechanisms for managing transactions and concurrency: Grab the record with SELECT FOR UPDATE or whatever syntax your suits your environment and isolation level. 一种答案是使用RDBMS的机制来管理事务和并发:使用SELECT FOR UPDATE或任何适合您的环境和隔离级别的语法来获取记录。 If one of your clients crashes or gets disconnected the transaction rolls back and the lock gets released. 如果您的一个客户端崩溃或断开连接,事务将回滚并释放锁。

In a connectionless environment like the web or an environment where connections get lost and recovered frequently a session based model with a session timeout could also work: 在像网络这样的无连接环境中或连接经常丢失和恢复的环境中,具有会话超时的基于会话的模型也可以工作:

  • Attempt to clear the existing lock on the record if it is for an expired session 如果会话已过期,请尝试清除记录上的现有锁
  • Attempt to lock the record to the sessionid (Fails if the previous step failed) 尝试将记录锁定为sessionid(如果上一步失败,则失败)
  • Select the locked record (No record returned if the previous step failed) 选择锁定的记录(如果上一步失败,则不返回记录)

So the lock gets released when the session expires. 因此,当会话过期时,锁将被释放。 No having to manually remove locks after crashes and some tolerance of client/connectivity problems. 崩溃后无需手动删除锁,并且可以容忍客户端/连接性问题。 It does take a bit more work to code though. 但是,编码确实需要更多的工作。

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

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