Monday, March 5, 2012

Vb.net has the database changed since opening a record (Optimistic Concurrency, ADO.NET)

This is a very interesting MSDN  link for (vb.net) programmers looking for a way to solve a well known problem: How do I know if the data in my database has changed since I got my data from the database, this is also called "concurrency"

For example (from the msdn link)

At 1:00 p.m., User1 reads a row from the database with the following values:

CustID     LastName     FirstName
101          Smith             Bob

At 1:01 p.m., User2 reads the same row.

At 1:03 p.m., User2 changes FirstName from "Bob" to "Robert" and updates the database.
The update succeeds because the values in the database at the time of update match the original values that User2 has.


At 1:05 p.m., User1 changes "Bob"'s first name to "James" and tries to update the row.
At this point, User1 encounters an optimistic concurrency violation because the value in the database ("James") no longer matches the original value that User1 was expecting ("Bob"). 

The concurrency violation simply lets you know that the update failed. The decision now needs to be made whether to overwrite the changes supplied by User2 with the changes supplied by User1, or to cancel the changes by User1.




Here is another link about one of the techniques talked about in the MSDN article

Peter