Skip Navigation LinksHome > Categories > Code from a Category

Optimistic Concurrency Control with rowversion - SQL Server

User Name: codelecturer
Name: Mike Chauhan
Contact Me:
Home Page:
6 years of total IT experience including programming, application development, System Maintenance . 3 years of Experienced and expertise in .Net Framework VB, Visual Basic.Net, ASP.Net 2.0, 3.5 and AD... [More]
Viewed Times: 3177
Add Date: 01/26/2013
In this article we will learn about Optimistic concurrency control with rowversion with SQL Server/
A table can have only one timestamp column. The value in the timestamp column is updated every time a row containing a timestamp column is inserted or updated. Of course this means we should not use it as a primary key, because we can get many orphans quickly if updates are performed on other columns.

As a row is modified in a table, the timestamp is updated with the current database timestamp value obtained from the @@DBTS function.

Rowversion is the synonym for timestamp in SQL Server 2005 and SQL Server 2008.

It is an 8 bytes unique binary key within the database.

Here is how it looks like: 0x0000000000000743. After an update to the row: 0x0000000000000745.

The rowversion(timestamp) starts changing as soon as the transaction begins. If the transaction is rolled back, it returns to the original value.

The main purpose is row versioning in multi user environment, in other words concurrency checking.

Pessimistic concurrency means locking the data at the row, page, or table level and don't allow anyone to modify it until the target user is done modifying and saving it back to the database. Trouble with this method: it may take a few minutes for the target user to update a record during which other users may be prevented from doing their work (locked out from the table). If the target user called away for a meeting for example in the middle of data entry, you need to unlock the table by a timeout mechanism in order to prevent disruption to data access by other users.

Optimistic concurrency means reading a record in a table and displaying it for the target user, but not locking it. Other users can read and modify the record at anytime while the target user is performing the manual update on the computer screen. When the target user releases the record for database update you need to check if someone changed it in between the initial read and the release (like 1-5 minutes). Usually this is not a problem due to the work distribution among staff, nevertheless you have to program for it to avoid conflicting updates and damage to database integrity.

Assume you are a developer and developing a program in Visual Basic to update the name and address table of customers. There will be 100 staff member who can perform this application function. How can you be sure that while target staff A typing in the change, staff X is not changing the same row?

Here is what you do:

1. Read the name and address table including the timestamp. You display the info to the user for update and save the timestamp.

2. Certain amount of time later, like 2 minutes, the user presses the submit button after changes were typed in.

3. You open a transaction with Begin Transaction

4. You read the timestamp of the name and address row

5. You compare the current timestamp to the saved timestamp

. 6. If the timestamps are same, you update the row and commit the transaction

7. If timestamps are different, you roll back the transaction and notify the user about the fact that the data was changed by someone else. You can let the user decide what to do or follow the appropriate company business rule for data entry conflict resolution.

This is pretty common practice in multi user environment. The alternate would be to examine a datetime column, or the entire row which is more processing intensive.

The following example shows timestamp (rowversion in SQL Server 2008) in action:

Happy Programming!!!

Post a Comment

Name: (Optional)
Email: (Optional, you can get an email if somebody replys your comments)*
Email me if somebody respons my comment below:
Enter Text
as Below:
(case insensitive, if hard to read, click the "get a new one" button)
* Your email address will not be shared with any third parties for any reason.
** Maximum 1000 charactors.