Optimistic Concurrency

jamie_pattison

Well-known member
Joined
Sep 9, 2008
Messages
116
Programming Experience
Beginner
I have a Windows Form application. Currently setup to call stored procedures and carry out the task. I need to implement Optimistic Concurrency but a little confused how to go about this since a lot of info is aimed at web applications. Could someone advise or provide a link where i could learn how to implement Optimistic Concurrency in a Windows Form application?

Thanks
 
The type of application is pretty much irrelevant because optimistic concurrency is a data access issue. Basically it means that you retrieve the data without locking it and then, when you try to save, you check whether anyone else has edited the data since you retrieved it. If they haven't then you save it but if they have then you notify the user and decide what to do, which would generally be retrieving the data again. All this is done using ADO.NET and ADO.NET is independent of the UI technology.
 
If your WIndows Forms app uses TableAdapters (read the DW2 link in my signature, section Creating a Simple Data App if you have never heard of a tableadapter) then just right click the tableadapter and configure... it to use (in advanced options) Optimistic Concurrency

The database CommandBuilder factory classes (SqlCommandBuilder etc) might also be able to generate optimistically concurrent queries; i never looked

A non-opti update:

VB.NET:
UPDATE people SET name = 'Fred' WHERE id = 1

Opti version:
VB.NET:
UPDATE people SET name = 'Fred' WHERE id = 1 and name = 'Joe'

Joe was the old value. If someone else edited the name to John while you were editing, then this second Update returns 0. If your tableadapters are enabled for optimistic concurrency then the number of records updated is checked when they save data. If it is 0 a ConcurrencyException is thrown that your program must catch. You must then handle what is to happen to the rows of data that failed. Note that 0 records can be updated for OTHER reasons than "someone else edited the data" depending on how you code your app up.. It's possible to make mistakes in your coding that can cause this error to appear even though noone else updated.. The db doesnt know for sure that someone else edited, all it knows is that a statement it expected to update 1 row actually updated 0 and the assumption is that concurrent edits were made
 
Back
Top