thread-safe access to unique database rows

I have an Oracle table (db2 soon also) which is being used as a work-queue.  An external process inserts rows to this table with a status field set to 'Pending'.

My consumer process :
1. Locks a 'Pending' row, and retrieves it for work.
2. Processes the work item represented by this row
3. Updates the row status to 'Processed'.

Step 2 is a time intensive process (a human interface in some cases).
I want to be able to implement mulitple consumers, so the problem is to ensure that the row is locked and no other consumer will retrieve this row.
I've implemented several solutions that each looks like a hack to me.
I'm looking for a "best practice" solution, or at least some validation of my solutions

Solution 1.
Use sys_guid() to lock row before access. All clients retrieve work via this SP.
PROCEDURE SecureNextFile(WORKSTEPfilter IN INTEGER, workitem OUT SYS_REFCURSOR) IS
 myguid VARCHAR2(32);
BEGIN
     myguid:=sys_guid();

     UPDATE INBOUND_FILE SET PROC_GUID=MYGUID, WORKSTEP=WORKSTEPfilter+1 WHERE WORKSTEP = WORKSTEPfilter AND ROWNUM=1 AND PROC_GUID IS NULL;
     COMMIT;
     OPEN WORKITEM FOR SELECT * FROM INBOUND_FILE INBF
     WHERE
            PROC_GUID=MYGUID;
END SecureNextFile;


Solution 2
Use Select For Update

FUNCTION Secure_NextTempadj RETURN INTEGER is
cursor c_f is
    SELECT TEMPADJID  
    FROM TEMP_ADJUSTMENTS WHERE CBSTATUS='Selected' AND ROWNUM=1
    ORDER BY CASE ORIGINFLAG WHEN 'FI' THEN 1 ELSE 2 END
    FOR UPDATE;

    v_a TEMP_ADJUSTMENTS.tempadjid%type;

begin
    open c_f;
    fetch c_f into v_a;
    if c_f%notfound then
        rollback;
        return -1;
    end if;
    update TEMP_ADJUSTMENTS set cbstatus='Locked' where current of c_f;
    commit;
    close c_f;
    RETURN v_a;
END;
mcrose1968Asked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

momi_sabagCommented:
so you need a solution for oracle or db2?
0
mcrose1968Author Commented:
Yes, I need a solution for db2.

Also I'm asking, "Is this how everyone else does this?"
It's a common problem, so I'm hoping for a common solution - something that won't leave my sucussor wondering what I was thinking.

0
mcrose1968Author Commented:
I believe the solutions I presented work in Oracle (there could be a hole).
Most importantly, I want the RIGHT solution.
0
momi_sabagCommented:
your solution seems ok
you just update the row you are working on to indicate it is worked on
the purpose here is to hold the lock for the minimum time and i think your solution does that
i would make that the select statements use the most efficient access plan possible
0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Oracle Database

From novice to tech pro — start learning today.

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.