如何处理Web应用程序中的并发更改? [英] How do I deal with concurrent changes in a web application?

查看:271
本文介绍了如何处理Web应用程序中的并发更改?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

以下是我想在网络应用程序中执行的两个潜在工作流程。

Here are two potential workflows I would like to perform in a web application.

变化1


  • 用户发送请求

  • 服务器读取数据

  • 服务器修改数据

  • 服务器保存修改的数据

  • user sends request
  • server reads data
  • server modifies data
  • server saves modified data

变化2:


  • 用户发送请求

  • 服务器读取数据

  • 服务器向用户发送数据


  • 服务器保存修改的数据

  • user sends request
  • server reads data
  • server sends data to user
  • user sends request with modifications
  • server saves modified data

在每种情况下,我想知道:确保并发访问此服务的标准方法将产生合理的结果? (即没有人的编辑被破坏,值对应于编辑的某种顺序等)

In each of these cases, I am wondering: what are the standard approaches to ensuring that concurrent access to this service will produce sane results? (i.e. nobody's edit gets clobbered, values correspond to some ordering of the edits, etc.)

这种情况是假设的,但这里有一些细节,在实践中处理这种情况:

The situation is hypothetical, but here are some details of where I would likely need to deal with this in practice:


  • 网络应用程序,但语言未指定

  • web框架

  • 数据存储是SQL关系数据库

  • 所涉及的逻辑太复杂,无法在查询中表达value = value + 1

  • web application, but language unspecified
  • potentially, using a web framework
  • data store is a SQL relational database
  • the logic involved is too complex to express well in a query e.g. value = value + 1

我觉得我不想在这里试试和重新发明轮子。当然,这些是众所周知的解决方案的公知问题。请指教。

I feel like I would prefer not to try and reinvent the wheel here. Surely these are well known problems with well known solutions. Please advise.

感谢。

推荐答案

知识,这个问题没有一般的解决方案。

To the best of my knowledge, there is no general solution to the problem.

问题的根源是用户可能在屏幕上检索数据并盯着它很长时间

The root of the problem is that the user may retrieve data and stare at it on the screen for a long time before making an update and saving.

我知道三种基本方法:


  1. 当用户读取数据库时,锁定记录,并且不释放,直到用户保存任何更新。在实践中,这是非常不切实际的。如果用户打开一个屏幕,然后去午饭没有保存怎么办?还是回家一天?

  1. When the user reads the database, lock the record, and don't release until the user saves any updates. In practice, this is wildly impractical. What if the user brings up a screen and then goes to lunch without saving? Or goes home for the day? Or is so frustrated trying to update this stupid record that he quits and never comes back?

将更新更新为deltas而不是目的地。以经典示例为例,假设您有一个记录库存中库存的系统。每次有销售,您必须从库存盘点中减去1(或更多)。

Express your updates as deltas rather than destinations. To take the classic example, suppose you have a system that records stock in inventory. Every time there is a sale, you must subtract 1 (or more) from the inventory count.

现有手数为10.用户A创建销售。当前数量= 10.用户B创建销售。他还获得当前数量= 10.用户A输入两个单元被销售。新数量= 10 - 2 = 8.保存。用户B输入一个销售的单位。新数量= 10(他加载的值) - 1 = 9.保存。

So say the present quantity on hand is 10. User A creates a sale. Current quantity = 10. User B creates a sale. He also gets current quantity = 10. User A enters that two units are sold. New quantity = 10 - 2 = 8. Save. User B enters one unit sold. New quantity = 10 (the value he loaded) - 1 = 9. Save. Clearly, something went wrong.

解决方案:写入更新库存设置数量=数量-1,而不是写入更新库存设置数量= 9其中itemid = 12345 itemid = 12345。然后让数据库对更新进行排队。这与策略#1非常不同,因为数据库只需要锁定记录足够长的时间来读取它,进行更新和写入。它不必等待有人盯着屏幕。

Solution: Instead of writing "update inventory set quantity=9 where itemid=12345", write "update inventory set quantity=quantity-1 where itemid=12345". Then let the database queue the updates. This is very different from strategy #1, as the database only has to lock the record long enough to read it, make the update, and write it. It doesn't have to wait while someone stares at the screen.

当然,这只能用于可以表示为增量的更改。如果你是,比方说,更新客户的电话号码,它不会工作。 (喜欢,旧号码是555-1234,用户A说要将其更改为555-1235,这是一个更改+1,用户B说更改为555-1243,这是一个变化+9,所以总变化+10,客户的新号码是555-1244。:-))但是在这样的情况下,最后一个用户点击enter键胜出可能是最好的你可以做。

Of course, this is only useable for changes that can be expressed as a delta. If you are, say, updating the customer's phone number, it's not going to work. (Like, old number is 555-1234. User A says to change it to 555-1235. That's a change of +1. User B says to change it to 555-1243. That's a change of +9. So total change is +10, the customer's new number is 555-1244. :-) ) But in cases like that, "last user to click the enter key wins" is probably the best you can do anyway.


  1. 更新时,检查数据库中的相关字段是否与from值匹配。例如,假设你为一家律师事务所为你的客户谈判合同工作。您有一个屏幕,用户可以在其中输入关于协商的注释。用户A打开合同记录。用户B显示相同的合同记录。用户A输入他刚在电话上与另一方通话,并且他们同意所提出的条款。用户B也试图呼叫另一方,输入他们没有回应电话,他怀疑他们是石墙。用户A单击保存。我们想让用户B的评论覆盖用户A吗?可能不会。相反,我们会显示一条消息,指出自从读取记录以来,笔记已更改,并允许他在看到新值后决定是继续保存,中止还是输入不同的内容。

[注意:论坛自动重新编号我的编号列表。我不知道如何覆写这个。]

[Note: the forum is automatically renumbering my numbered lists. I'm not sure how to override this.]

这篇关于如何处理Web应用程序中的并发更改?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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