Guidelines for Concurrency Issues in a .Net Application

We are planning to upgrade our existing VB6 application to C # (VS2008). ADO.Net follows more of a disabled strategy when accessing data. I am thinking of giving Entity Framework a shot as using an ORM tool is highly recommended if you want to speed up development. And EF uses optimistic locking.

How do you solve the ancient problem when 2 users open the same "client", for example. ADO.net suggests checking if a record has changed when it was saved using conflict exceptions.

In the old VB6 application we had 2 systems: - Storing the bits called "Locked" and the text field "LockedBy" in the entry This is very easy to implement and use, but when the app crashes or there are problems with the network connection, this entry is still will be blocked. - Since we only use MSSQL as a database, we used the row locking function for MSSQL for those clients with SQL 2000 or higher.

How is this handled in a modern .Net application, how do you solve this ancient problem in .Net applications?

Any ideas, comments or information are appreciated ...

Best regards, Sven Peters

+2


source to share


2 answers


The most common implementation of Optimistic Locking is to have a Version column (usually TIMESTAMP) in each table in addition to the Primary Key.

When you load data from a table, you must load the Version into memory and never modify it (it will usually just be an opaque array of bytes).

When you save the updated table, you start a very short transaction and first read the current value of the Version column and compare it to your value in memory.



  • If they are equal, it means that no one has changed the data, and you can safely save them.
  • If they are different, it means that someone else has changed the data since it was read, and you will have to resolve this conflict.

AFAIR, the Entity framework supports this type of optimistic locking out of the box.

+2


source


The blocking problem is still not clear to me. I find the standard .Net solution is not user friendly (managing conflicts just before saving). In many situations, the user will lose the changes they applied to the recording.

We are now playing with NHibernate and manual write locking. By creating a global temp table called TableName.PrimaryKeyValue. After the save operation, the table will be dropped, if you close the connection, the table will be dropped, if the application stop unexpectedly, SQL Server will clean up after me. We will integrate this manual locking into objects and the object repository.



Best regards, Sven Peters

0


source







All Articles