简体   繁体   English

数据集乐观并发

[英]Dataset optimistic concurrency

What would I need to do in my dataset C# client for handling optimistic concurrency? 我需要在我的数据集C#客户端中做什么来处理乐观并发?

This article does not go into many details except from use a timestamp 本文除了使用时间戳之外,没有涉及太多细节

This article does not go into muy details except from use a timestamp 本文不涉及muy详细信息,除非使用时间戳记

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. 因此,您只需要听RowUpdated事件,如果RecordsAffected为零,那么就会发生一些不好的事情。

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. 如果更新了零行,那么您将了解并发异常并相应地进行报告。

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. 如果没有更详细的问题,我真的不知道该说些什么。

What would I need to do in my dataset C# client for handling optimistic concurrency? 我需要在我的数据集C#客户端中做什么来处理乐观并发?

Nothing. 没有。 it is not the jkob of a data set to handle this - the data set is an offline cache of data. 它不是处理此问题的数据集的jkob-数据集是数据的脱机缓存。 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. 数据集根本不处理数据库交互。

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

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