Question

I have a job scheduled in the back end (for sending e-mails) using DBMS_Scheduler. However, a requirement was to allow users to schedule e-mails as well as to allow them to send e-mails immediately on the click of a button.

To accomplish this, I made the following arrangement: When the user clicks on a button, a procedure is called. The procedure executes the job I mentioned above (using run_job). This allowed me to use the same job to send e-mails immediately as well as to send them later at a scheduled time.

The problem I'm now facing is this: Suppose user A clicks on the button. And at the same time, user B clicks on the button on his system. Then user B gets the following error message: The job is already running.

How do I execute the same job multiple times (in parallel) in Oracle?

Was it helpful?

Solution

Set the DBMS_SCHEDULER.RUN parameter USE_CURRENT_SESSION to TRUE.

--Create a test job.
begin
    dbms_scheduler.create_job(
        job_name => 'TEST_JOB',
        job_type => 'PLSQL_BLOCK',
        job_action => 'BEGIN dbms_lock.sleep(3); END;');
end;
/

--This fails if run concurrently. 
begin
    dbms_scheduler.run_job(job_name => 'TEST_JOB', use_current_session => FALSE);
end;
/

--This can run concurrently.
begin
    dbms_scheduler.run_job(job_name => 'TEST_JOB', use_current_session => TRUE);
end;
/

OTHER TIPS

If you can convert your job to an event based job, you could use lightweight jobs that respond on the event that triggers them. For event based jobs you can set parallel_instances to true, allowing multiple instances of the same job to run in parallel.

This is the way to do what you want.

Lightweight parallel jobs are intended to be short living jobs with simple tasks.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top