How do you handle data concurrency in .NET ?

Answer Posted / sureshsamatham

ADO.NET and Visual Studio use optimistic concurrency,
because the data architecture is based on disconnected data.
Therefore, you need to add business logic to resolve issues
with optimistic concurrency.
(1.optimistic 2.pessimistic 3.Last in wins)
If you choose to use optimistic concurrency, there are two
general ways to determine if changes have occurred: the
version approach (true version numbers or date-time stamps)
and the saving-all-values approach.
The Version Number Approach
In the version number approach, the record to be updated
must have a column that contains a date-time stamp or
version number. The date-time stamp or a version number is
saved on the client when the record is read. This value is
then made part of the update.
One way to handle concurrency is to update only if value in
the WHERE clause matches the value on the record. The SQL
representation of this approach is:
Copy Code
UPDATE Table1 SET Column1 = @newvalue1, Column2 = @newvalue2
WHERE DateTimeStamp = @origDateTimeStamp
Alternatively, the comparison can be made using the version
number:
Copy Code
UPDATE Table1 SET Column1 = @newvalue1, Column2 = @newvalue2
WHERE RowVersion = @origRowVersionValue
If the date-time stamps or version numbers match, the record
in the data store has not changed and can be safely updated
with the new values from the dataset. An error is returned
if they don't match. You can write code to implement this
form of concurrency checking in Visual Studio. You will also
have to write code to respond to any update conflicts. To
keep the date-time stamp or version number accurate, you
need to set up a trigger on the table to update it when a
change to a row occurs.
The Saving-All-Values Approach
An alternative to using a date-time stamp or version number
is to get copies of all the fields when the record is read.
The DataSet object in ADO.NET maintains two versions of each
modified record: an original version (that was originally
read from the data source) and a modified version,
representing the user updates. When attempting to write the
record back to the data source, the original values in the
data row are compared against the record in the data source.
If they match, it means that the database record has not
changed since it was read. In that case, the changed values
from the dataset are successfully written to the database.
Each data adapter command has a parameters collection for
each of its four commands (DELETE, INSERT, SELECT, and
UPDATE). Each command has parameters for both the original
values, as well as the current (or modified) values.
Note: use time stamps to avoid concurrency violations

Is This Answer Correct ?    6 Yes 0 No



Post New Answer       View All Answers


Please Help Members By Posting Answers For Below Questions

What does executenonquery () method return?

494


Explain how can we load multiple tables in to dataset?

493


Explain the difference between ado and ado.net?

496


What is ole data type?

499


Explain how to pass multiple tables in datasets simultaneously?

490






How to store data in memory?

503


What does ado stand for?

514


What you mean by filtering of data?

528


What is ado.net and its features?

493


What is method to get XML and schema from Dataset? getXML() and get Schema ()

535


What provider ado.net use by default? Explain the role of data provider in ado.net?

517


Some important instruction regarding ADO.NET connection string ?

534


What are the core objects of ADO.NET?

576


Is it possible to load multiple tables in a Dataset?

519


Which is the best method to get two values from the database?

514