DBMS SCHEDULER

From Oracle FAQ
Jump to: navigation, search

DBMS_SCHEDULER is a more sophisticated job scheduler introduced in Oracle 10g. The older job scheduler, DBMS_JOB, is still available, is easier to use in simple cases and fit some needs that DBMS_SCHEDULER does not satisfy.

Create a job[edit]

BEGIN
  DBMS_SCHEDULER.CREATE_JOB (
     job_name           => 'my_java_job',
     job_type           => 'EXECUTABLE',
     job_action         => '/usr/bin/java myClass',
     repeat_interval    => 'FREQ=MINUTELY',
     enabled            => TRUE
  );
END;
/

Unlike DBMS_JOB you do not need to commit the job creation for it to be taken into account. As a corollary, if you want to cancel it, you have to remove or disable it (see below).

Remove a job[edit]

EXEC DBMS_SCHEDULER.DROP_JOB('my_java_job');

Run a job now[edit]

To force immediate job execution:

EXEC dbms_scheduler.run_job('myjob');

Change job attributes[edit]

Examples:

EXEC DBMS_SCHEDULER.SET_ATTRIBUTE('WEEKNIGHT_WINDOW', 'duration', '+000 06:00:00');
BEGIN 
  DBMS_SCHEDULER.SET_ATTRIBUTE
     ('WEEKNIGHT_WINDOW', 'repeat_interval', 
      'freq=daily;byday=MON, TUE, WED, THU, FRI;byhour=0;byminute=0;bysecond=0');
END;

Enable / Disable a job[edit]

BEGIN 
  DBMS_SCHEDULER.ENABLE('myjob');
END;
BEGIN 
  DBMS_SCHEDULER.DISABLE('myjob');
END;

Monitoring jobs[edit]

SELECT * FROM dba_scheduler_jobs WHERE job_name = 'MY_JAVA_JOB';
SELECT * FROM dba_scheduler_job_log WHERE job_name = 'MY_JAVA_JOB';

or checking from JOB owner schema

 SELECT * FROM user_scheduler_jobs WHERE job_name = 'MY_JAVA_JOB';
 SELECT * FROM user_scheduler_job_log WHERE job_name = 'MY_JAVA_JOB';

Use user_scheduler_jobs and user_scheduler_job_log to only see jobs that belong to your user (current schema).