简体   繁体   English

跨负载平衡应用程序的并发和锁定

[英]Concurrency And Locking Across Load Balanced Application

I am writing an application where users can create items with a start date and an end date and save them to a SQL database hosted in Microsoft Sql Server. 我正在编写一个应用程序,用户可以在其中创建带有开始日期和结束日期的项目,并将它们保存到Microsoft Sql Server中托管的SQL数据库中。 The rule in the application is that only a single item can be active for a given time (no overlapping items). 应用程序中的规则是,在给定的时间内,只有一个项目可以处于活动状态(没有重叠的项目)。 The application also needs to be load balanced, which means (as far as I know) traditional semaphores / locking won't work. 该应用程序还需要实现负载平衡,这(据我所知)意味着传统的信号灯/锁定将不起作用。

A few additional items: 其他一些项目:

  • The records are persisted into two tables (based on a business requirement). 记录被保存到两个表中(基于业务需求)。
  • Users are allowed to "insert" records in the middle of an existing record. 允许用户在现有记录的中间“插入”记录。 Inserted records adjust the start & end dates of any pre-existing records to prevent overlapping items (if necessary). 插入的记录可调整任何现有记录的开始和结束日期,以防止项目重叠(如有必要)。
  • Optimally we want to accomplish this using our ORM and .Net. 理想情况下,我们希望使用我们的ORM和.Net完成此任务。 We don't have as much leeway to make database schema changes but we can create transactions and do other kinds of SQL operations through our ORM. 我们没有太多的余地来更改数据库架构,但是我们可以通过ORM创建事务并执行其他类型的SQL操作。

Our goal is to prevent the following from happening: 我们的目标是防止发生以下情况:

  • Saves from multiple users resulting in overlapping items in either table (ex. users 1 & 2 query the database, see that there aren't overlapping records, and save at the same time) 来自多个用户的保存导致两个表中的项目重叠(例如,用户1和2查询数据库,看到没有重叠的记录,并同时保存)
  • Saves from multiple users resulting in a different state in each of the destination tables (ex. Two users "insert" records, and the action is interleaved between the two tables. Table A looks as though User 1 went first, and table B looks as though User 2 went first.) 从多个用户进行保存,导致每个目标表中的状态不同(例如,两个用户“插入”记录,并且该操作在两个表之间进行交错。表A看起来好像是用户1首先出现,而表B看起来是尽管用户2排名第一。)

My question is how could I lock or prevent multiple users from saving / inserting at the same time across load balanced servers. 我的问题是如何锁定或阻止多个用户同时在负载平衡的服务器上进行保存/插入。

Note: We are currently looking into using sp_getapplock as it seems like it would do what we want, if you have experience with this or feel like it would be a bad decision and want to elaborate that would be appreciated as well! 注意:我们目前正在研究使用sp_getapplock,因为它看起来像我们想要的,如果您有经验,或者觉得这是一个错误的决定,并且想详细说明一下,不胜感激!

Edit: added additional info 编辑:添加了其他信息

There are at least a couple of options: 至少有两个选择:

  • You can create a stored procedure which wraps the INSERT operation in a transaction: 您可以创建将INSERT操作包装在事务中的存储过程:

     begin tran select to see if there is an existing record you can: - invalidate the previous record - insert a new record or - raise an error commit tran catch error rollback tran 
  • You can employ a last-in-wins strategy where you don't employ a write level lock, but rather a read-level pseuodo-lock, essentially ignoring all records except the latest one. 您可以采用“后赢制”策略,在这种策略中,您不使用写级别锁,而是使用读级别伪锁,实际上忽略了除最新记录以外的所有记录。

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

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