数据集乐观并发

发布于 2024-12-14 01:43:37 字数 168 浏览 2 评论 0原文

我需要在数据集 C# 客户端中执行哪些操作来处理乐观并发?

这篇文章并未涉及很多内容除了使用时间戳之外的详细信息

What would I need to do in my dataset C# client for handling optimistic concurrency?

This article does not go into many details except from use a timestamp

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

不忘初心 2024-12-21 01:43:37

除了使用时间戳之外,本文不会讨论太多细节

这并不正确,本文仅提到时间戳(前半部分或文章中的用例),并且提供了有关第二个乐观锁实现的更多详细信息 -

测试乐观并发冲突的另一种技术是验证行中的所有原始列值是否仍然与数据库中找到的值匹配

。神奇的是通过以下语句执行的:

UPDATE Table1 Set Col1 = @NewCol1Value,
          Set Col2 = @NewCol2Value,
          Set Col3 = @NewCol3Value
WHERE Col1 = @OldCol1Value AND
    Col2 = @OldCol2Value AND
    Col3 = @OldCol3Value

所以您只需听 RowUpdated 事件,如果 RecordsAffected 为零,则发生了不好的事情。

基于时间戳的实现也非常明显。您将拥有一个日期时间和数据集:

class OptLockDataSet { 
    DataSet _data;
    DateTime LastUpdate {
        return _data.Tables["ChangeTracker"][0][0];//just for example :)
    }
}

要更新,您将有两种方法 - 显式,当您在尝试保存任何内容之前检查时间戳时:

if(GetLastUpdateFromDB(_data) > LastUpdate ) {
    //This means that last update was changed because someone else
    //modified the data.
    //Show error to user and reload data from db.
}

或者隐式,就像文章中描述的第二种方式 - 尝试使用时间戳进行更新条件:

update data set @col1 = @val1 where last_update = @last_update

如果更新了零行,那么您将了解并发异常并相应报告。

This article does not go into muy details except from use a timestamp

That's not really correct, the article just mentions timestamps (the use case in the first half or the article), and alternatively provides more details on the second optimistic lock implementation -

Another technique for testing for an optimistic concurrency violation is to verify that all the original column values in a row still match those found in the database

The magic is performed by the following statement:

UPDATE Table1 Set Col1 = @NewCol1Value,
          Set Col2 = @NewCol2Value,
          Set Col3 = @NewCol3Value
WHERE Col1 = @OldCol1Value AND
    Col2 = @OldCol2Value AND
    Col3 = @OldCol3Value

So you'd just listen to RowUpdated event, and if RecordsAffected is zero then something bad happened.

Timestamp-based implementation is pretty obvious as well. You'll have a datetime along with your dataset:

class OptLockDataSet { 
    DataSet _data;
    DateTime LastUpdate {
        return _data.Tables["ChangeTracker"][0][0];//just for example :)
    }
}

For updating you'll have two ways - explicit, when you check timestamps even before attempting to save anything:

if(GetLastUpdateFromDB(_data) > LastUpdate ) {
    //This means that last update was changed because someone else
    //modified the data.
    //Show error to user and reload data from db.
}

Or implicit, like the second way described in the article - try update using timestamp as a condition:

update data set @col1 = @val1 where last_update = @last_update

and if zero rows are updated then you'll know about concurrency exception and report correspondingly.

冷月断魂刀 2024-12-21 01:43:37

假设你自己处理这一切。

如果您最初从数据库检索的时间戳小于当前在对象上保留的时间戳,则其他人已经保留了数据,并且您不应该允许当前用户保留数据,或者至少提示他们可能会覆盖更改。

如果没有更详细的问题,我真的不知道还能说什么。

Assuming your handling this all yourself.

If the timestamp you retrieved from the DB originally is less then the timestamp currently persisted on the object, then someone else has persisted data and you should not allow the current user to persist, or at least prompt them that they might be overwriting changes.

Without a more detailed question, I don't really know what else to say.

呆橘 2024-12-21 01:43:37

我需要在数据集 C# 客户端中执行哪些操作来处理乐观并发?

没有什么。它不是数据集的 jkob 来处理这个问题 - 数据集是数据的离线缓存。这是您用来将更改写入数据库的工作,以根据数据集中给出的信息处理乐观并发。

数据集根本不处理数据库交互。

What would I need to do in my dataset C# client for handling optimistic concurrency?

Nothing. it is not the jkob of a data set to handle this - the data set is an offline cache of data. It is the job of whatever you use to write the changes out to the database to handle optimisstic concurrency based on the information given in the dataset.

Datasets do not deal with database interaction at all.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文