I want to :
I have parallel processes doing this same work together, and i want to ensure that all get to work on unique rows. How do i ensure that?
I assume you are running on SQL Server (because of the tag), if not then my answer is not applicable. Locking alone is not enough. If you use database record locking SqL server will block other processes trying to access the locked row and in effect you will handle only one row at a time. The solution for you is to combine row locking with READPAST hint so the rows locked by someone else will be skipped. Here's what each process should do:
select top 1 id, ... from TheTable with (updlock, readpast) where flag = 0
//do the work now
update TheTable set flag = 1 where id=<previously retrieved id>
The nice thing here that the operation of selecting the next unlocked row and locking it is atomic so it guarantees that no one else will be able to select the same row.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With