Suppose, I am about to start a project using ASP.NET and SQL Server 2005. I have to design the concurrency requirement for this application. I am planning to add a TimeStamp column in each table. While updating the tables I will check that the TimeStamp column is same, as it was selected.
Will this approach be suffice? Or is there any shortcomings for this approach under any circumstances?
Please advice.
Thanks
Lijo
First of all the way which you describe in your question is in my opinion the best way for ASP.NET application with MS SQL as a database. There is no locking in the database. It is perfect with permanently disconnected clients like web clients.
How one can read from some answers, there is a misunderstanding in the terminology. We all mean using Microsoft SQL Server 2008 or higher to hold the database. If you open in the MS SQL Server 2008 documentation the topic “rowversion (Transact-SQL)” you will find following:
So timestamp data type is the synonym for the rowversion data type for MS SQL. It holds 64-bit the counter which exists internally in every database and can be seen as @@DBTS. After a modification of one row in one table of the database, the counter will be incremented.
As I read your question I read “TimeStamp” as a column name of the type rowversion data. I personally prefer the name RowUpdateTimeStamp. In AzManDB (see Microsoft Authorization Manager with the Store as DB) I could see such name. Sometimes were used also ChildUpdateTimeStamp to trace hierarchical RowUpdateTimeStamp structures (with respect of triggers).
I implemented this approach in my last project and be very happy. Generally you do following:
Or make a data casting like following
to hold RowUpdateTimeStamp as bigint, which corresponds ulong data type of C#. If you makes OUTER JOINTs or JOINTs from many tables, the construct
MAX(RowUpdateTimeStamp)from all tables will be seen a little more complex. Because MS SQL don’t support function like MAX(a,b,c,d,e) the corresponding construct could looks like following:spSoftwareUpdatestored procedure could look likeCode of
dbo.spSoftwareDeletestored procedure look like the same. If you don’t switch onNOCOUNT, you can produce DBConcurrencyException automatically generated in a lot on scenarios. Visual Studio gives you possibilities to use optimistic concurrency like “Use optimistic concurrency” checkbox in Advanced Options of theTableAdapterorDataAdapter.If you look at
dbo.spSoftwareUpdatestored procedure carful you will find, that I useRowUpdateTimeStamp <= @originalRowUpdateTimeStampin WHERE instead ofRowUpdateTimeStamp = @originalRowUpdateTimeStamp. I do so because, the value of@originalRowUpdateTimeStampwhich has the client typically are constructed as aMAX(RowUpdateTimeStamp)from more as one tables. So it can be thatRowUpdateTimeStamp < @originalRowUpdateTimeStamp. Either you should use strict equality = and reproduce here the same complex JOIN statement as you used in SELECT statement or use <= construct like me and stay exact the same safe as before.By the way, one can construct very good value for ETag based on RowUpdateTimeStamp which can sent in HTTP header to the client together with data. With the ETag you can implement intelligent data caching on the client side.
I can’t write whole code here, but you can find a lot of examples in Internet. I want only repeat one more time that in my opinion usage optimistic concurrency based on rowversion is the best way for the most of ASP.NET scenarios.