You should enable row level locking
on the table with:
CREATE TABLE mytable (...) LOCK DATAROWS
Then you:
- Begin the transaction
- Select your row with
FOR UPDATE
option (which will lock it)
- Do whatever you want.
No other process can do anything to this row until the transaction ends.
P. S. Some mention overhead problems that can result from using LOCK DATAROWS
.
Yes, there is overhead, though i'd hardly call it a problem for a table like this.
But if you switch to DATAPAGES
then you may lock only one row per PAGE
(2k
by default), and processes whose rows reside in one page will not be able to run concurrently.
If we are talking of table with dozen of rows being locked at once, there hardly will be any noticeable performance drop.
Process concurrency is of much more importance for design like that.