简体   繁体   English

并发问题,同时更新SQL Server表列

[英]concurrency issue while updating sql server table column

I am working on accounting project and facing concurrency update issue. 我正在从事会计项目并面临并发更新问题。 I have followed below scenario 我遵循以下情况
1) User1 retrieves column1 data 1)User1检索column1数据
2) User2 also retrieves column1 data 2)User2还检索column1数据
3) User1 updates the column1 data 3)User1更新column1数据
4) while updating User2 column1 data i have to tell the user column1 data is already updated please refresh the details & update if modified from original. 4)在更新User2 column1数据时,我必须告诉用户column1数据已更新,请刷新详细信息并更新(如果从原始版本进行了修改)。

and i don't want to restrict the User2 from retrieving the data which is already retrieved by User1 , how to handle this type concurrency or any builtin function available in SQL Server? 并且我不想限制User2检索User1已经检索到的数据,如何处理这种类型的并发性或SQL Server中可用的任何内置函数?

Reiterate your problem as I understand it: 根据我的理解,重申您的问题:

  1. User1 is retrived column1 data User1已检索的column1数据
  2. User2 is also retrived column1 data 用户2也检索了列1的数据
  3. User1 is updated the column1 data User1更新为column1数据
  4. User2 wants to update the stale data on column1. User2要更新column1上的陈旧数据。 You want to alert user 2 that the data is stale, user 2 should do a refresh (get latest value) and then can update again. 您想提醒用户2数据已过时,用户2应该刷新(获取最新值),然后可以再次更新。

This is exactly what rowversion is for. 这正是rowversion的用途。 You include this on the record as a new column. 您将此作为新列包含在记录中。 Sql server will handle assigning values and incrementing them each time data changes on the record. 每当记录上的数据发生更改时,SQL Server将处理分配值并递增它们。 You should retrieve this value along with the other relevant details of that record and then include it in your update statement. 您应该检索该值以及该记录的其他相关详细信息,然后将其包括在更新语句中。 If the values do not match you have a concurrency error, the record was updated in between the last time that user retrieved the data and the time they want to update the data. 如果这些值不匹配,那么您会遇到并发错误,那么记录将在用户最后一次检索数据与他们想要更新数据的时间之间进行更新。

Also note that in this case rowversion is altered any time any alteration is made on a record, not just for that one particular column. 还要注意,在这种情况下,只要对记录进行任何更改,行版本都会被更改,而不仅是针对该特定列。

Logic after implementing rowversion in the table. 在表中实现行版本化后的逻辑。

  1. Any user retrieves column1 data also retrieves the record's rowversion value 任何用户检索column1数据也将检索记录的rowversion值
  2. Any user updates column1 data includes the rowversion value in the WHERE clause of the UPDATE statement. 任何用户更新column1数据都在UPDATE语句的WHERE子句中包含rowversion值。
    1. If the number of records affected (updated) is 0 the update did not occur because the record was updated by another connection between the retrieve and the update. 如果受影响(更新)的记录数为0,则不会发生更新,因为该记录是由检索和更新之间的另一个连接更新的。 In this case execute logic to display an error message and retrieve the updated data as well as the new rowversion value. 在这种情况下执行的逻辑来显示错误消息并检索更新后的数据,以及新的rowversion值。 ( alternatively the record was deleted but lets assume this is not the case ). 或者,删除记录,但是假设情况并非如此 )。
    2. If the number of records affected (updated) was 1 then the update succeeded. 如果受影响(更新)的记录数为1,则更新成功。 The rowversion value of the record has also changed. 记录的rowversion值也已更改。 Be sure to return the updated rowversion to the user if you want to allow additional updates without a complete data refresh. 如果要允许其他更新而不完全刷新数据,请确保将更新的行版本返回给用户。

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

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