Hibernate version: 3.0.5
I plan to use a table to create a queue of work from which multiple threads can retrieve units, but I only want each thread to retrieve a single unit of work at a time. Also, I'd like to avoid collisions between threads looking for a new unit of work.
So, the plan is to have a "lock" column into which I'll insert a GUID that I've generated previously. This way, other threads can look for the first available row for which the "lock" column is null. Enter the problem.
So the order of opertion is this:
1) Update the first row found that has a lock column equal to null and set the lock column to my GUID.
2) Read the row that I just updated so that I can get the work information.
3) Commit
The reason for doing the update first is so that I get a database lock at the same time that I select a row in the database. Think of it as a database semaphore.
The problem is how to limit the number of rows affected by my update? I only want to affect the very first row I encounter that has a null lock column, so that I don't create problems for other threads looking for units of work.
Is there a way to do this? I haven't found one yet. Still looking.
Thanks,
Dave
|