[英]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: 其他一些项目:
Our goal is to prevent the following from happening: 我们的目标是防止发生以下情况:
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.