I realise that this is probably more a question for an Oracle SQL forum but I will ask here anyway.
Currently I have a datastage job which is to select a number of records for processing. To do this it selects messages with an indicator field of null from the table and updates this indicator indicating that it is ready for processing. Easy enough so far. The problems begin to arise as the datastage job which calls this update is run as multi-instance. Further complicating matters is that when selecting the records, checks must be made that no related records are being processed in the other instances.
At first we thought we'd cracked it with a simple update select statement, as follows:
Code: Select all
UPDATE table SET indicator = 'process_0'
where record_id exists in
(SELECT record id WHERE indicator is null
AND NOT EXISTS
(SELECT parent_record_id where indicator is not null))
This process works if the update job is running in a single instance. However when its running multiple instance its possible that 2 or more instances of the process can call the job at the same time and as a result the first one will be updating and prior to committing those records the second instance can select those same records and overwrite the indicator which its own data. (This indicator determines what process that record will follow).
Anyway so we tried updating the SQL to a stored procedure that would lock the records once selected so that a second instance could not select those records for update, as follows:
Code: Select all
CURSOR IS
(SELECT record id WHERE indicator is null
AND NOT EXISTS
(SELECT parent_record_id where indicator is not null))
FOR UPDATE SKIP LOCKED;
BEGIN
FOR cursor
LOOP
UPDATE table SET indicator = 'process_0'
WHERE CURRENT OF cursor
END LOOP
COMMIT;
So I was wondering if anybody out there who has better SQL skills can see a problem with the logic I am trying to implement. I think its an interesting problem and would really like to know if its possible in the one procedure (I am sure it must be).
Just so you're aware we do have a work around where we have built a delay into the process and are running the original update select statement essentially as a single instance within the controlling sequence, so as stated above this is more a question of interest rather than necessity.
Looking forward to any feedback if any. Thanks in advance.