Example:
Converting DB jobs to Scheduler
Let's say we have a job in DBA_JOBS that we want to convert to the
Scheduler.
select job, log_user, next_date, next_sec, interval, what
from dba_jobs
where job=152152;
JOB LOG_USER NEXT_DATE NEXT_SEC INTERVAL WHAT
---------- ---------- ---------- -------- ------------------------- --------------------
152152 SCOTT 05/01/2006 01:00:00 trunc(sysdate+1) + 1/24 SCOTTS_PROC;
This job belongs to SCOTT, and executes SCOTTS_PROC every day at 1
am. Let's look at how to create and run a similar job in the
Scheduler. The rough equivalents to DBMS_JOB.SUBMIT and
DBMS_JOB.RUN are:
| DBMS_JOB |
DBMS_SCHEDULER |
| dbms_job.run |
dbms_scheduler.run_job |
| dbms_job.submit |
dbms_scheduler.create_job |
DBMS_SCHEDULER.CREATE_JOB is an overloaded procedure; in this
example, we'll look at just one of many ways to call it. Here's
the call:
begin
dbms_scheduler.create_job
(job_name => 'SCOTT.RUN_SCOTTS_PROC',
job_type => 'STORED_PROCEDURE',
job_action=> 'SCOTT.SCOTTS_PROC',
start_date=> trunc(sysdate+1)+1/24,
repeat_interval=> 'FREQ=DAILY; BYDAY=MON,TUE,WED,THU,FRI,SAT; BYHOUR=1;',
enabled=>true,
auto_drop=>false,
comments=>'Converted from job 152152');
end;
There are a lot of things to notice here; let's go through the
call line by line.
- Instead of a number, the job's identified by the name you
pass as job_name. Job names
follow the standard Oracle naming conventions. SYS can create
a job for anyone. So, job_name => 'SCOTT.RUN_SCOTTS_PROC'
creates a job in SCOTT's schema named RUN_SCOTTS_PROC .
- You need to specify the job type. As in the old
job scheduling mechanism, you can schedule anonymous blocks
(job_type: 'PLSQL_BLOCK') or stored procedures (job_type:
'STORED_PROCEDURE'). You can also schedule programs that run
outside the database (job_type: 'EXECUTABLE') or schedule "job
chains" (job_type: 'CHAIN'). A job chain is a sequence of
jobs.
- job_action is the equivalent
of "what" in dbms_job.submit. Note that if you're scheduling a
stored procedure, you don't put a semicolon after the
procedure name, as you did in dbms_job.submit . Note also that
if you want to pass arguments to a stored procedure, like
UPDATE_INDEXES('SCOTT'), you have to either wrap the stored
procedure in an anonymous pl/sql block, or set the arguments
with a separate call to SET_JOB_ARGUMENT_VALUE - a topic we
won't cover here.
- start_date is the first date
the job will execute. Note that if you specify the
repeat_interval using calendaring syntax, which we'll cover
shortly, start-date is used as a
reference only - the job won't actually execute until the next
date that matches your repeat_interval. For example, if you
set the repeat interval to every Thursday, and pass a start
date that's on a Tuesday, then the job will execute on the
first Thursday following the start date.
- This call passes repeat_interval in calendaring syntax,
which we'll discuss below. You could also pass it just as you
had it in DBA_JOBS, as 'trunc(sysdate+1) + 1/24'.
- In DBMS_SCHEDULER, unlike DBMS_JOB, the default is that new
jobs are disabled. If you want your job to be enabled as soon
as you create it, as it would have been with DBMS_JOB, then
passenabled=>true.
- You can control whether or not a one-time job is dropped
after it's run by setting auto_drop to FALSE.
- You can pass in comments, which are displayed alongside the
job in the static data dictionary view that shows Scheduler
jobs.
After you run this call to create_job, there's one more thing
to notice: you don't have to COMMIT. The results are immediately
visible to all users in the DBA_SCHEDULER_JOBS table:
OWNER JOB_NAME JOB_ACTION START_DATE REPEAT_INTERVAL STATE COMMENTS
----- --------------- ----------------- ---------- --------------- --------- -------------------------
SCOTT RUN_SCOTTS_PROC SCOTT.SCOTTS_PROC 05/08/2006 FREQ=DAILY; BYD SCHEDULED Converted from job 152152
What if you want to run this job right away? Use
dbms_scheduler.run_job:
connect scott/tiger
begin
DBMS_SCHEDULER.RUN_JOB (
job_name => 'RUN_SCOTTS_PROC',
use_current_session => false);
end;
/
Note that the default for use_current_session is TRUE -- that is,
if you don't pass this parameter, RUN_SCOTTS_PROC will run synchronously, ie. in your
current session, instead of runningasynchronously, ie. being picked up and run
by a job slave.
Job Run Log
While a job is running, it won't show up in DBA_JOBS_RUNNING;
instead, it's in DBA_SCHEDULER_RUNNING_JOBS. Once the job has
executed, log rows are written to DBA_SCHEDULER_JOB_LOG and
DBA_SCHEDULER_JOB_RUN_DETAILS. These tables record the exit status
(success, failure) of each job run, the run dates and duration,
and provide any additional details such as error messages, who
stopped a stopped job, etc.
More Job Tools
If you're going to move jobs to the Scheduler, you'll need to know
how to change their next run dates (or other attributes), how to
disable and reenable them, and how to kill a running job.
Fortunately, these are all easy tasks in DBMS_SCHEDULER. Here are
the rough equivalents to the DBMS_JOB commands:
| DBMS_JOB |
Purpose |
DBMS_SCHEDULER |
| dbms_job.next_date |
Change the next time a job will run |
dbms_scheduler.set_atrribute - change the
repeat_interval or start_date |
| dbms_job.what |
Change the program a job runs |
dbms_scheduler.set_atrribute - change job_action |
| dbms_job.interval |
Change how often a job runs |
dbms_scheduler.set_atrribute - change repeat_interval |
| dbms_job.change |
Change job attributes |
dbms_scheduler.set_atrribute - change the appropriate
attribute |
| dbms_job.broken |
Mark a job as BROKEN, or unmark it |
dbms_scheduler.disable / dbms_scheduler.enable |
| alter system kill session... |
Stop a running job |
dbms_scheduler.stop_job |
Special
Notes
- To review the execution times of a job:
SELECT JOB_NAME, STATUS,
ERROR# FROM user_SCHEDULER_JOB_RUN_DETAILS;
- To review the errors of a Job:
select status, error#,
substr(additional_info,1,500)
from
user_scheduler_job_run_details ;
- By default, XE on Windows sets up the scheduler service to
disabled. Enable and start it
- If you are trying to setup a Windows .bat job, you may need to
perform the following:
job_action=>'c:\windows\system32\cmd.exe /c
C:\AA\Gen_Del_Images.bat > nul',