简体   繁体   English

更新期间锁定项目的最佳方法

[英]Best way of locking item during update

I am creating a website (c#/SQL Server 2008, .net) that needs to allow updating of some common information that can be shared by users. 我正在创建一个网站(c#/ SQL Server 2008,.net),该网站需要允许更新一些可以由用户共享的常见信息。

For instance, I have 2 users and a table that contains a number of items. 例如,我有2个用户和一个包含许多项目的表。 Much like a moderation queue. 很像一个审核队列。

Lets say it contains rows with ID's #1, #2, #3 可以说它包含ID为#1,#2,#3的行

If user 1 selects #1 first this needs to lock immediately until it is released/and available. 如果用户1首先选择#1,则需要立即锁定,直到释放/可用。

I have a couple of questions as this is uncharted territory for me. 我有几个问题,因为这对我来说是未知的领域。

  • What's the best way of locking this row in SQL? 在SQL中锁定此行的最佳方法是什么?
  • Being stateless, how can user 2 be made aware that user 1 has claimed #1? 由于是无状态的,如何使用户2知道用户1已声明#1? Is it better to use a 'next available' approach? 使用“下一个可用”方法更好吗?

Thanks in advance. 提前致谢。

You have something that is called optimistic locking. 您有一些称为乐观锁定的东西。 But it doesnt, per say, solve your problem of 'reporting' that the row is locked. 但这不能说解决您“报告”该行已锁定的问题。 For most cases reporting is usless, but it can be solved programaticly. 在大多数情况下,报告是无用的,但可以通过编程方式解决。

That locking is kinda 'fun' depend if you are making desktop app, or web one, but mostly it is solved with using of extra column (timestamp) and lock a row for a period of time. 该锁定有点“有趣”,取决于您是要制作桌面应用程序还是Web应用程序,但是大多数情况下,可以通过使用额外的列(时间戳)并在一段时间内锁定一行来解决。

I made one project where i reported a user a row is locked, and i solved it with 2 fields (start time, end time) due i wanted to give variable amount of time to the person to update row. 我做了一个项目,在该项目中,我向用户报告了行被锁定,由于要向人员提供可变的时间来更新行,所以我用2个字段(开始时间,结束时间)解决了该问题。

sample: 样品:

query: select a, b, c, d, start_time, end_time FROM table

Program : if ((datetime.now()-start time > 1min) or (end_time <> null))
{
update table
SET start_time = datetime.now()
set end_time = null
where A = a and B = b and C= c and D = d, and START_TIME = start_time 
--big caps is current value, small caps value from last select ( and that is OPTIMISTIC concurency).
}
else
{
messagebox.show ("You suck, cant be changed") --personal touch :-)
}

when you update row, dont forget to place end_time. 当您更新行时,请不要忘记放置end_time。

OFC, check for time, within update is possible OFC,检查时间,可以在更新内

Edit: i got that idea from row_scn in oracle, so it isnt anything revolutionary 编辑:我从Oracle中的row_scn得到了这个主意,所以它没有任何革命性的意义

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

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