Is there a possible race condition in this UPDATE statement?

I am writing a synchronizer that will accept all changes in one database and synchronize them with another database. For this purpose, I added Ttwo columns to the table :

alter table T add LastUpdate rowversion, LastSync binary(8) not null default 0

Now I can easily select all the lines that have changed since the last synchronization:

select * from T where LastUpdate > LastSync

However, after synchronization is complete, I have to make these two fields equal. But updating the line also updates the timestamp, so I have to do this:

update T set LastSync=@@DBTS+1 where ID=@syncedId

But I wonder - will it always work? What if I read the value @@DBTSand then another user manages to insert / update the row somewhere before my row is committed? Is this risky code? And if so, how can this be improved?

+5
2

"LastSync" , , , . , rowversion. , , " ".

:

  • @LastSync
  • " @ThisSync = max (LastUpdate) T, LastUpdate > @LastSync"
  • " * T, LastUpdate > @LastSync LastUpdate <= @ThisSync"
  • @ThisSync "LastSync" .

, , rowversion, max(). .

+4

Serializable, / .

RepeateableRead ...

-1

All Articles