The solution that worked for me: double update
. For example:
UPDATE mytesttable
SET TimeOfLastUpdate = TimeOfLastUpdate
WHERE TimeOfLastUpdate < sysdate-1/24
UPDATE mytesttable
SET Status = 'Red',
TimeOfLastUpdate = sysdate
WHERE TimeOfLastUpdate < sysdate-1/24
COMMIT;
(similar code for 'Green')
The first update
doesn't change anything, but it grabs a lock on the row, which won't be released until commit
is called.
The second update updates the row with a sysdate
that is guaranteed to be greater than or equal to the time that the lock was acquired, thus preventing race conditions. (Unless sysdate
were to go backwards in time.)