悲观与乐观并发(锁定与反馈)

编程入门 行业动态 更新时间:2024-10-26 04:22:28
本文介绍了悲观与乐观并发(锁定与反馈)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

我正在使用以下条件构建应用程序:

  • 工作项:需要通过网络手动工作的项
  • 每个用户都有一个工作项队列
  • 有一个搜索,允许用户查看工作项并将工作项分配给他们的队列
  • 用户可以将工作项
  • 注意:工作项只能工作一次。这不是一个wiki页面,它更多是一个匹配的练习,应该只有一个用户执行一次。一旦工作项目工作,它就从系统(除了一些审计/报告),有点像一个错误跟踪系统

    选项你相信更好吗?您可以援引支持您意见的任何主流应用程式吗?

    选项1:

    • 当用户A查看或工作工作项时,工作项被锁定。
    • 当用户A打开工作项后,其他用户转到工作项,他们将只能看到工作项。
    $ b

    选项2:

    • 任何用户都可以在不锁定的情况下提取工作项。 >
    • 如果用户A通过提交表单来工作工作项,并且用户B工作在相同的工作项,则用户A的工作将在数据库中生效,并且用户B将被告知

    我个人比较喜欢选项2。 / p>

    解决方案

    听起来像你在谈论 pessimistic verses 乐观并发控制。

    两者都被广泛使用,我个人觉得乐观并发更容易处理,但它将取决于你自己的要求和用法。如果编辑(和潜在的冲突)是共同的,那么悲观的并发控制可能是适当的,如果不是,那么乐观并发将更快,更简单的工作。

    让我知道如果您想在SQL Server中查看使用 RowVersion 数据类型的代码示例(这是很简单的,虽然:

    • 所有表格都包含RowVersion列
    • 所有SELECT查询都包含此列(对于可以修改的数据)
    • 所有UPDATE或DELETE查询都包含WHERE RowVersion = @RowVersion。这是乐观的部分,如果返回0行,那么别人已经触摸了该行,没有更新发生,所以告诉用户。注意:如果行已更新,则还应返回RowVersion的新值。这也适用于INSERT查询,很像在插入后返回标识列的ID。

    I'm building an application with the following criteria:

    • work items: items that need to be manually worked via the web by users (short one page form)
    • Multiple users working 'work items'
    • Each user has a queue of 'work items'
    • There is a search that allows users to view 'work items' and assign 'work items' to their queues
    • Users can take 'work items' out of other people's queues by assigning them to themselves

    Note: 'work items' are worked only once. This is not a wiki page, it's more of a matching exercise that should only be performed once by one user. Once the 'work item' is worked, it is gone from the system (aside from some auditing/reporting), Somewhat like a bug tracking system

    Which option do you believe is better? Can you cite any mainstream applications that support your opinion?

    Option 1:

    • When user A goes to view or work a 'work item', the 'work item' becomes locked.
    • When other users go to the 'work item' after User A opens the 'work item', they will only be able to see the 'work item'. They can not write.
    • The lock expires after n minutes at which point another user can lock the 'work item'.

    Option 2:

    • Any user can pull up a 'work item' without locking it.
    • If User A works the 'work item' by submitting the form and User B works the same 'work item', then user A's work will take affect in the database, and User B will be informed that their changes did not take affect because another user has modified the 'work item'.

    I personally like option 2. Thoughts please?

    解决方案

    Sounds like you're talking about pessimistic verses optimistic concurrency control.

    Both are widely used, personally I find optimistic concurrency easier to deal with, but it will depend upon your own requirements and usage. If edits (and potential conflicts) are common then pessimistic concurrency control may be appropriate, if not, then optimistic concurrency will be faster and simpler to work with.

    Let me know if you want to see code examples using RowVersion datatype in SQL Server (that's what I'm currently using), it's pretty simple though:

    • All tables include RowVersion column
    • All SELECT queries include this column (for data which can be modified)
    • All UPDATE or DELETE queries include a WHERE RowVersion = @RowVersion. This is the optimistic part, if 0 rows returned then someone else has touched the row, no update takes place, so tell user about it. NOTE: If row was updated then new value for RowVersion should also be returned. This also applies to INSERT queries, much like you would return the Id of an identity column after an insert.

更多推荐

悲观与乐观并发(锁定与反馈)

本文发布于:2023-11-10 01:56:57,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1574038.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:悲观   乐观   反馈

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!