I have a program written in Java that creates 5 threads which select data from Oracle. The select is like this:
select * from queue_requests where request_status = 0 and date_requested <= sysdate and rownum <= ? for update skip locked
I set the parameter to 1024. I often get the following result (using System.out.println):
THREAD 0 UPDATE 1024
THREAD 4 UPDATE 0
THREAD 1 UPDATE 0
THREAD 2 UPDATE 0
THREAD 3 UPDATE 0
So, only one thread finds rows to update. From what i found in different articles, i think oracle firstly applies the rownum clause and after that for update skip locked, so all the threads try to update the first 1024 rows. I could probably use something like:
select * from (select * from queue_requests where request_status = 0 and date_requested <= sysdate for update skip locked) where rownum <= ?
But this will lock all the rows and after that will return a part of them. I don't want to lock all the rows.
I found a solution which created stored procedures, but i can't alter anything in the database. I also found something about Advanced Queueing, but i'm not sure i need something so complex (i didn't find any good example either).
Is there any solution for this problem?