Introduction
In this article, we will touch base on 3 ways of doing optimistic locking i.e. Using the ADO.NET dataset, SQL Server Timestamp checks, and old/new value checks. So we will start this article with a small introduction to concurrency, discuss the 5 concurrency problems, and then get down to the actual meat to understand how to implement optimistic locking using the 3 methodologies.
This is a small E-book for all my .NET friends that covers topics like WCF, WPF, WWF, Ajax, Core .NET, SQL, etc you can download the same from here or else you can catch me on my daily free training from here.
Why do we need locking?
In a multi-user environment, it's possible that multiple users can update the same record at the same time causing confusion between users. This issue is termed concurrency.
How can we solve concurrency problems?
Concurrency problems can be solved by implementing a proper "Locking strategy". Locks prevent action on a resource from being performed when some other resource is already performing some action on it.
What kind of confusion is caused because of concurrency?
There are 4 kinds of major problems caused because of concurrency, the below table shows the details.
Problems |
Short description |
Explanation |
Dirty reads |
"Dirty Read" occurs when one transaction is reading a record, which is part of a half-finished work of another transaction. |
- User A and user B see seeing value as "5".
- User B changes the value "5" to "2".
- User A is still seeing the value as "5" A dirty read has happened.
|
Unrepeatable read |
In every data read if you get different values then it's an "Unrepeatable Read" problem. |
- User A sees value as "5".
- User B changes the value"5" to "2".
- User, A refreshes to see value "5", he is surprised....unrepeatable read has happened.
|
Phantom rows |
If "UPDATE" and "DELETE" SQL statements do not affect the data then it can be a "Phantom Rows" problem. |
- User A updates all values "5' to "2".
- User B inserts a new record with the value "2".
- User A selects all records with value "2' if all the values have changed, he is surprised to still find value "2" records.....Phantom rows have been inserted.
|
Lost updates |
"Lost Updates" are scenarios where one updates which are successfully written to the database are overwritten with other updates of other transactions. |
- User A updates all values from "5" to "2".
- User B comes and updates all "2" values to "5".
- User A has lost all his updates.
|
So how can we solve the above problems?
By using optimistic or pessimistic locking, the further coming article discusses the same.
What is Optimistic locking?
As the name suggests "optimistic" assumes that multiple transactions work without affecting each other. In other words, no locks are enforced while doing optimistic locking. The transaction just verifies that no other transaction has modified the data. In case of modification, the transaction is rolled back.
How does optimistic lock work?
You can implement optimistic locking in numerous ways but the fundamentals to implement optimistic locking remain the same. It's a 5 step process as shown below:-
- Record the current timestamp.
- Start changing the values.
- Before updating check whether anyone else has changed the values by checking the old time stamp and new timestamp.
- If it's not equal rollback or else commit.
What are the different solutions by which we can implement optimistic locking?
There are 3 primary ways by which we can implement optimistic locking-in. NET.
- Datasets: Datasets by default implement optimistic locking. They do a check of old values and new values before updating
- Timestamp Data type: Create a timestamp data type in your table and while updating check if the old timestamp is equal to the new timestamp.
- Check old and new values: Fetch the values, do the changes and while doing the final updates check if the old value and current values in the database are equal. If they are not equal then rollback or else commit the values.
Solution number 1. Datasets
As said in the previous section dataset handles optimistic concurrency by itself. Below is a simple snapshot where we held the debug point on the Adapter's update function and then changed the value from the SQL Server. When we ran the "update" function by removing the breakpoint it threw a "Concurrency" exception error as shown below.
If you run the profiler at the back end you can see it fires the update statement checking the current values and the old values are the same.
EXEC sp_executesql N'
UPDATE [tbl_items]
SET [AuthorName] = @p1
WHERE (
[Id] = @p2
AND ((@p3 = 1 AND [ItemName] IS NULL) OR [ItemName] = @p4)
AND ((@p5 = 1 AND [Type] IS NULL) OR [Type] = @p6)
AND ((@p7 = 1 AND [AuthorName] IS NULL) OR [AuthorName] = @p8)
AND ((@p9 = 1 AND [Vendor] IS NULL) OR [Vendor] = @p10)
)',
N'
@p1 nvarchar(11), @p2 int, @p3 int, @p4 nvarchar(4), @p5 int, @p6 int, @p7 int, @p8 nvarchar(18), @p9 int, @p10 nvarchar(2)
',
@p1 = N'this is new', @p2 = 2, @p3 = 0, @p4 = N'1001', @p5 = 0, @p6 = 3, @p7 = 0, @p8 = N'This is Old Author', @p9 = 0, @p10 = N'kk'
In this scenario we were trying to change the field value "AuthorName" to "This is new" but while updating it makes a check with the old value "This is old author".
Below is the downsized code snippet of the above SQL which shows the comparison with the old value.
,@p8 = N'This is Old Author'
Solution number 2. Use timestamp data type
The other way of doing optimistic locking is by using the 'TimeStamp' data type of SQL Server. Timestamp automatically generates a unique binary number every time you update the SQL Server data. Timestamp data types are for versioning your record updates.
To implement optimistic locking we first fetch the old 'TimeStamp' value and when we are trying to update we check if the old timestamp is equal to the current timestamp as shown in the below code snippet.
UPDATE tbl_items
SET itemname = @itemname
WHERE CurrentTimestamp = @OldTimeStamp;
We then check if any updates have happened, in case updates have not happened then we raise a serious error '16' using SQL Server 'raise error' as shown in the below code snippet.
IF (@@ROWCOUNT = 0)
BEGIN
RAISEERROR('Hello some else changed the value', 16, 10)
END
If any concurrency violation takes place you should see the error propagated when you call 'ExecuteNonQuery' to the client-side as shown in the below figure.
Solution number 3. Check old values and new values
Many times we would like to check concurrency on only certain fields and omit fields like identity etc. For those kinds of scenarios, we can check the old value and the new value of the updated fields as shown in the below code snippet.
UPDATE tbl_items
SET itemname = @itemname
WHERE itemname = @OldItemNameValue
Source code
Download the source code from the top of this article.