I have the following procedure
procedure MyProc is
n number;
begin
-- Query A
select count(*) into n from SomeTable where Column1 = 0;
if n = 0 then
insert into SomeTable (Column1, Column2) values (0, 'some data');
else
update SomeTable set Column2 = 'some other data' where Column1 = 0;
end if;
commit;
end;
This procedure is run by several jobs in several threads :
for i in 1..10
loop
Jobname := dbms_scheduler.generate_job_name('JobName');
JobAction := 'begin MyProc; end;';
dbms_scheduler.create_job(job_name => Jobname, job_type => 'PLSQL_BLOCK', job_action => JobAction, enabled => true);
end loop;
The goal is to create only one row in the table SomeTable and all the other jobs will update the same row...
When all the jobs are finished, I notice that sometimes several rows are created instead of only one.
I understood that whenever the Query A is executed, because of row locks, it will see only the rows of the table that were committed before the query started... hence some other jobs don't see the change...
Is there anyway to solve that please ?
In .Net there is a concept of Monitor.Enter & Monitor.Exit that makes all the other threads wait until a resource is released...
Can anyone help please ?
Thanks