Lost Update Anomaly in Sql Server Update Command

Posted by Javed on Stack Overflow See other posts from Stack Overflow or by Javed
Published on 2010-01-27T13:13:39Z Indexed on 2010/03/31 20:03 UTC
Read the original article Hit count: 458

Hi, I am very much confused.

I have a transaction in ReadCommitted Isolation level. Among other things I am also updating a counter value in it, something similar to below:

Update tblCount set counter = counter + 1

My application is a desktop application and this transaction happens to occur quite frequently and concurrently. We recently noticed an error that sometimes the counter value doesn't get updated or is missed. We also insert one record on each counter update so we are sure that records have been inserted but somehow counter fails to update. This happens once in 2000 simulaneous transactions.

I seriously doubt it is a lost update anomaly I am facing but if you look at the command above, it's just update the counter from its own value: if I have started a transaction and the transaction has reached this statement, it should have locked the row. This should not cause lost update, but it's happening somehow.

Is the thing that this update command works in two parts? Like first it reads the counter value (during which it doesn't get the exclusive lock) and then writes the new calculated value (when it does get an exclusive lock)?

Please help, I have got really confused.

© Stack Overflow or respective owner

Related posts about sql-server

Related posts about transactions