简体   繁体   English

如何确保数据的一致性

[英]how to ensure data consistency

c# application. c#应用程序。

I am doing a select followed by update of a column in a table. 我正在执行选择,然后更新表中的列。 I am putting these in a separate transaction with isolation level set to Serializable. 我将这些放在一个单独的事务中,隔离级别设置为Serializable。 I am doing this to achieve data consistency. 我这样做是为了实现数据的一致性。

But still I can check that multiple users are able to read (select) the same value and eventually trying to update with the same value. 但我仍然可以检查多个用户是否能够读取(选择)相同的值并最终尝试使用相同的值进行更新。

Can anyone suggest how i can achieve consistency, such that 任何人都可以建议我如何实现一致性,这样

No two users read the same value. 没有两个用户读取相同的值。
No user read a value which is updated but not yet committed. 没有用户读取已更新但尚未提交的值。

If you want to prevent this it's called pessimistic locking. 如果你想阻止它,那就叫做悲观锁定。 You can do this with (Table|Row) locks but it will kill your performance. 您可以使用(表格|行)锁定来执行此操作,但这会导致您的性能下降。

The 'standard' way is to use optimistic concurrency and solve the problem after it has happened. “标准”方法是使用乐观并发并在问题发生解决问题。

No two users read the same value. 没有两个用户读取相同的值。

You can (only) assure this by allowing only 1 connection at a time. 您可以(仅)通过一次只允许一个连接来确保这一点。

No user read a value which is updated but not yet commited. 没有用户读取已更新但尚未提交的值。

That only requires the (much lighter) ReadCommitted isolation level. 这只需要(更轻)ReadCommitted隔离级别。

I think you are referring to Concurrency . 我认为你指的是并发 Check out the link to get started on understanding it, if that is what you are asking about. 查看链接以开始了解它,如果这是您要问的。 You will probably have to do a little more research depending on your back end and your specific situation. 您可能需要根据您的后端和具体情况进行更多研究。 Again, if this is what you are looking for you may be particullary interested in pessimistic concurrency. 同样,如果这是你正在寻找的,你可能会特别感兴趣的悲观并发。

http://en.wikipedia.org/wiki/Concurrency_control http://en.wikipedia.org/wiki/Concurrency_control

Handling Concurrency Issues in .NET 处理.NET中的并发问题

There are a lot of different ways to achieve this. 有很多不同的方法可以实现这一目标。 You can use timestamps on your records. 您可以在记录上使用时间戳。 When you do an update you make sure that the id matches and the timestamp that your application pulled with the record matches. 当您进行更新时,请确保id匹配,并确保应用程序随记录匹配的时间戳匹配。

To avoid having a user read a record that is uncommitted look at using sql hints in your queries. 为了避免让用户读取未提交的记录,请查看在查询中使用sql提示。

one way to accomplish the task: you will have to add a "lock" field to your table. 完成任务的一种方法:您必须在表中添加“锁定”字段。

then you should write a stored procedure that will take some ID as a parameter, atomically select a row with NULL lock, update the row so that lock will contain the passed id and then return the value. 那么你应该编写一个存储过程,它将一些ID作为参数,原子地选择一个带有NULL锁的行,更新该行,以便锁定将包含传递的id,然后返回该值。

As long as the stored procedure is executed atomically (any other requests for the table will wait until this one is finished) every client calling this procedure will get another value that still had it's lock field empty. 只要存储过程以原子方式执行(对表的任何其他请求将等到这一个完成),每个调用此过程的客户端将获得另一个仍然将其锁定字段为空的值。 Once the value is returned client is confident that the row containing the value has its lock field set to some value an will not be returned. 返回值后,客户端确信包含该值的行的锁定字段设置为某个值,并且不会返回。

After you do modifications you should update the row, writing the new value and setting the lock field to NULL, thus "unlocking" the row for future requests. 在进行修改之后,您应该更新行,写入新值并将lock字段设置为NULL,从而“解锁”该行以备将来的请求。

How about putting a update with returning in a stroed proc and calling it from my application. 如何在stroed proc中返回更新并从我的应用程序中调用它。

Will this take care of all the problems? 这会解决所有问题吗?

No two users read the same value. 没有两个用户读取相同的值。

To do this, in your SET TRANSACTION statement, add RESERVING mytable FOR PROTECTED WRITE (or the local equivalent on your database). 为此,请在SET TRANSACTION语句中添加RESERVING mytable FOR PROTECTED WRITE(或数据库中的本地等效项)。

No user read a value which is updated but not yet committed. 没有用户读取已更新但尚未提交的值。

If you are using SERIALIZABLE transactions you won't have this problem. 如果您使用的是SERIALIZABLE交易,则不会出现此问题。

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

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