Kaushal Khamar
Kaushal Khamar

Reputation: 2123

How i can prevent concurrency saving of data in Database in .NET

A problem i have faced during testing of my application which is

from two different browser i have open my application and open same page from i am doing some operation.
Then request for update data at same time from both browser. and data from both browser are save in database which is wrong.

I have done some code for prevent this issue like first check that values are exist in database or not. but this kind of validation can't prevent of concurrency saving of data.

Please help me. I need to resolve this issue.
Thanks in adavance

Upvotes: 0

Views: 2021

Answers (1)

Vizard
Vizard

Reputation: 303

You can prevent concurrency error saving data by using Optimistic or Pessimistic locking

Optimistic Locking

  • There are 3 primary ways by which we can implement optimistic locking in .NET:-

    1. Datasets: - Dataset by default implement optimistic locking. They do a check of old values and new values before updating.

    2. Timestamp Data type: - Create a timestamp data type in your table and while updating check if old timestamp is equal to new timestamp.

    3. Check old and new value: - Fetch the values, do the changes and while doing the final updates check if the old value and current values in database are equal. If they are not equal then rollback or else commits the values

Pessimistic Locking

  • We can do pessimistic locking by specifying “IsolationLevel” in SQL Server stored procedures, ADO.NET level or by using transaction scope object.

For more information, check out: http://www.codeproject.com/Articles/114262/ways-of-doing-locking-in-NET-Pessimistic-and-opt

Upvotes: 1

Related Questions