如何实现"悲观锁"在asp.net应用程序? [英] How do I implement "pessimistic locking" in an asp.net application?

查看:198
本文介绍了如何实现"悲观锁"在asp.net应用程序?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想一些建议任何人经历了在asp.net应用程序中实现类似悲观锁。这是我在寻找的行为:

I would like some advice from anyone experienced with implementing something like "pessimistic locking" in an asp.net application. This is the behavior I'm looking for:


  1. 用户A打开订单#313

  2. 用户B试图打开订单#313,但被告知,用户A已经具备了为使X分钟独占方式打开。

由于我之前没有实行这个功能,我有几个设计问题:

Since I haven't implemented this functionality before, I have a few design questions:


  • 我应该重视的订单记录哪些数据?我在考虑:

    • LockOwnedBy

    • LockAcquiredTime

    • LockRefreshedTime

    我会考虑如果LockRefreshedTime&LT开锁记录; (现在 - 10分钟)

    I would consider a record unlocked if the LockRefreshedTime < (Now - 10 min).


    • 如何保证锁不持有超过必要的时间却不料要么不会过期?

    我是pretty舒适的使用jQuery所以它使用客户端脚本的方法是值得欢迎的。这将是一个内部的Web应用程序,所以我可以相当自由的与我使用的带宽/周期。我也想知道,如果悲观锁是这个概念的合适的词语。

    I'm pretty comfortable with jQuery so approaches which make use of client script are welcome. This would be an internal web application so I can be rather liberal with my use of bandwidth/cycles. I'm also wondering if "pessimistic locking" is an appropriate term for this concept.

    推荐答案

    这听起来像你最的方式出现。我不认为你真的需要LockRefreshedTime不过,它并没有真正添加任何东西。你可能只是以及使用LockAcquiredTime决定当锁已经变得陈旧。

    It sounds like you are most of the way there. I don't think you really need LockRefreshedTime though, it doesn't really add anything. You may just as well use the LockAcquiredTime to decide when a lock has become stale.

    你会想要做的另一件事是确保你使用的交易。你需要用一个数据库事务中锁定的检查和设置,让你不谁认为他们有一个有效的锁两个用户结束了。

    The other thing you will want to do is make sure you make use of transactions. You need to wrap the checking and setting of the lock within a database transaction, so that you don't end up with two users who think they have a valid lock.

    如果您有需要对多个资源(即给定类型的多个记录或多个类型的记录)获得锁的任务,那么你需要的地方你做的锁定应用锁定在同一顺序。否则,你可以有一个死锁,其中code中的一位有记录的锁定,想要锁定记录B和code具有b个锁定的另一位,正在等待记录中的一个。

    If you have tasks that require gaining locks on more than one resource (i.e. more than one record of a given type or more than one type of record) then you need to apply the locks in the same order wherever you do the locking. Otherwise you can have a dead lock, where one bit of code has record A locked and is wanting to lock record B and another bit of code has B locked and is waiting for record A.

    至于如何确保锁不会意外释放。确保如果您有任何长时间运行的进程可能比你的锁超时运行时间越长,它的运行过程中刷新其锁定。

    As to how you ensure locks aren't released unexpectedly. Make sure that if you have any long running process that could run longer than your lock timeout, that it refreshes its lock during its run.

    的术语显式锁定也用于描述这种时间锁定的

    The term "explicit locking" is also used to describe this time of locking.

    这篇关于如何实现&QUOT;悲观锁&QUOT;在asp.net应用程序?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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