Optimistic Concurrency

B

bill

Hi all,

I 've just start moving from ADO to ADO.NET and have a
question regarding optimistic concurrency. As
in "Optimistic Concurrency Example" from "NET Framework
Developer's Guide" when an update conflict occurs such as
when the user try to update a row that has already been
updated or deleted by another user. In case of such
conflict the "DBConcurrencyException" that were thrown
when I call DataAdapter's update method only give the
message indicating that the "violation occurs" and "0
records were affected", the "RowError" method of
the "Rows" collection also give a similar answer. My
question is how can I know the exact reason of the update
conflict, whether the row was already deleted or had
already been updated. Is there a method in ADO.NET classes
that give the exact reason of the conflict or do I need to
use some other technique to find out. If possible you
could give some example code or point me to the rigth
location. Thanks you for your time.

Regards,
 
S

Stephany Young

The exact reason for the conflict is that there is an optimistic concurrency
violation.

Once that is thrown, if you want to delve further then you need to do
something like:

'select count(*) from <table> where <column>=<value>'

If you do this via EexcuteScalar then the the result will be 0 if the record
was deleted or 1 if the record was updated.
 
O

One Handed Man [ OHM ]

The most likely cause is that your UpdateCommand is incorrect in as much as
it does not satisfy the requirement to actually change the database ( in
order words , the logic is wrong ).
 
D

David Sceppa

Bill,

Stephanie is right, you do need to query the database to
determine the cause of the concurrency failure. There is no
magic property in the object model to set.

Personally, I believe the easiest way to accomplish this is
to handle the RowUpdated event, look for a concurrency exception,
and issue a parameterized query based on the key column(s) for
the current contents of the corresponding row in the database.
If the query returns no rows, the row was deleted by another
user. If the query returns a row, you can compare its contents
to the original values in the DataRow to determine which
column(s) are out of synch.

I hope this information proves helpful.

David Sceppa
Microsoft
This posting is provided "AS IS" with no warranties,
and confers no rights. You assume all risk for your use.
© 2003 Microsoft Corporation. All rights reserved.
 
O

One Handed Man [ OHM ]

Please correct me if I am wrong ( and I probably am ), but thought that If
the UpdateCommand logic was wrong and that there were no rows aletered
because of it, that it would generate the same exception ( because zero rows
were affected ). I am convinced I have had exactly this scenario in the
past.

I realise that true concurrency exception would raise this also, but is what
I have said incorrect ?

Regards

;-D
 
D

David Sceppa

The DataAdapter throws a DBConcurrencyException if the
database reports that the query to submit a pending change
executed successfully but modified no rows. It is possible to
supply bad updating logic and see this exception as a result.

David Sceppa
Microsoft
This posting is provided "AS IS" with no warranties,
and confers no rights. You assume all risk for your use.
© 2003 Microsoft Corporation. All rights reserved.
 
D

David Sceppa

Bill,
What is the difference between "DataRow.CancelEdit"
and "DataRow.RejectChanges" methods?

CancelEdit cancels the changes you made since calling
BeginEdit. RejectChanges cancels the pending changes stored in
the row. Say that the original value of a column is "A":

//Assume row["MyCol"] is "A" initially

//row["MyCol"]
row.BeginEdit(); // "A"
row["MyCol"] = "B"; // "B"
row.EndEdit(); // "B"

row.BeginEdit(); // "B"
row["MyCol"] = "C"; // "C"
row.CancelEdit(); // "B"

row.RejectChanges(); // "A"


I hope this information proves helpful.

David Sceppa
Microsoft
This posting is provided "AS IS" with no warranties,
and confers no rights. You assume all risk for your use.
© 2003 Microsoft Corporation. All rights reserved.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top