views:

119

answers:

2

In an optimistic concurrency scenario fo a web-app, I am considering to give each table the timestamp column (sqlserver), comparable to a guid. Linq to entities will then generate sql update queries like WHERE id = @p0 AND timestamp = @p1 when one decorates the timestamp column with a certain attribute in Entity Framework. When the number of updated records returned is 0 we have detected a concurrency exception.

In a lot of posts I am reading about Self Tracking Entities which may be an alternative or better solution. But I didn't see any advantage over the "simple" timestamp method described above. Apart from the scenario where the database is immutable and doesn't offer the timestamp column.

Which solution is better and why?

EDIT

Yury Tarabanko correctly states that STE is another concept. However zeeshanhirani's answer demonstrates that concurrency check is one main motive to track changes.

Lets rephrase the question: why would anybody use the STE concept for concurrency check where the 'timestamp column' method looks so much easier.

+1  A: 

You are mixing two concepts here. STE is not about concurrency at all.

Self tracking entities just know how to do their change tracking regardles of how those changes were made. So you always know what is the current state of entities object graph. And you don't need to invoke additional change detecting.

What is STE.

EDIT:

"concurrency check is one main motive to track changes"

AFAIK STEs and POCOs share the same approach to concurrency check which simply results in additional where condition(s) in update statement sent to DB. Equivalent to this:

UPDATE [schema].[table] 
     SET [prop1] = value1, ... 
     WHERE [key] = key_value 
          AND [concurrency_prop_1] = concurrency_prop1_old_value 
          AND [concurrency_prop_2] = concurrency_prop2_old_value

So 'the main motive' to track changes is, well, to track changes in N-tier app.

Yury Tarabanko
Fair enough. But how about the question: why would anybody want to do this for concurrency check where the 'timestamp' method looks far more natural and easier.
Gerard
I think nobody would choose STEs for the sake of concurrency check. STEs are not about concurrency. As @zeeshanhinari pointed out 'STEs works with' concurrency check concept. They do send original values for concurrency tokens over the wire. If they hadn't you would not be able to check those values on another tier of multi-tier application. "The idea is to create smart entity objects that keep track of their own changes and changes to related entities."
Yury Tarabanko
A: 

Self Tracking Entity actually works with the concept you described. STE basically tracks changes to the object when the context is not around. However when it sends its changes back to the server using WCF service, it sends the current values of the property, the new state of the entity and also the original values of the the primary key column, independent association value and original value for any columns that are marked as Concurrency=Fixed in entity data model.

zeeshanhirani
By the way, w.r.t. "original values of the the primary key column": never change a primary key - change the design (e.g. delete and add)!
Gerard