I have a situation where I need to:
- Read the value of a row.
- If the value of some column is 'X', perform action A. Otherwise, perform action B.
- If we performed action A, update the column with the result of the action.
Action A is not a database operation, and may take a while to run, and it is not reversible. Action B is not a database operation, but is very fast to run. The sequence is performed on multiple threads, across multiple servers.
Currently we have no locking, and so occasionally we see action A being executed multiple times, when it should only happen once. I think my only solution here is to somehow wrap the sequence above with an acquire lock step and a release lock step, and I'm not sure how to do that.
I have seen a similar question, where the answer was to add 'locked' and 'acquiry time' columns to the row. However, in that situation, the OP wasn't worried about frequently re-acquiring the lock. If I had to spin-wait for the previous lock to expire every time I wanted to execute the sequence, my server's performance would probably go out the window.
Is there something built in to SQL that I can use here?