DBMS_SCHEDULER as a new alternative for DBMS_JOB
Many people have a dislike of Dbms_job. Setting the right parameters
requires good knowledge of the input parameters and you probably need a
calculator too! In Oracle 10g, a new package might make things easier.
What’s in a name
If you check 10g’s data dictionary, you’ll find a new package by the name dbms_scheduler.
One of the most obvious improvements is the naming convention of the
functions in this package. DBMS_JOB.Submit is now named
DBMS_SCHEDULER.Create_job and DBMS_JOB.Run goes by the name
DBMS_SCHEDULER.Run_job. You may disagree, but I think the new names
make a lot more sense.
In control of intervals
Fortunately,
Oracle made major improvements in the most annoying flaw of DBMS_JOB:
defining the interval. For instance, if you want a job to start every
morning at 06:00 exactly, the parameter "interval" usually contains
something like ‘TRUNC(SYSDATE) + 1 + 21/24′. With DBMS_SCHEDULER, the new parameter "repeat_interval" is set to ‘FREQ=DAILY’. In combination with a start_date = ’20-APR-2006 06:00′ the job starts at 06:00. Everyday at
06:00. And not at 06:10:14 because the previous run lasted 10 minutes
14 seconds and you entered an interval of "SYSDATE + 1".
Scheduler and program objects
Another notable thing is the option to create scheduler and program objects. This is quite interesting too, because now you can define a custom schedule which you can reuse in all jobs.
This example creates a schedule which runs every hour on the half hour.
BEGIN<br /> DBMS_SCHEDULER.create_schedule<br /> ( schedule_name => 'run_every_hour'<br /> , start_date => SYSTIMESTAMP<br /> , repeat_interval => 'freq=hourly; byminute=30'<br /> , end_date => NULL<br /> , comments => 'This schedule starts every hour at the half hour'<br /> ) ;<br />END;<br />/
Next step is creating a program object with parameters:
BEGIN<br /> dbms_scheduler.create_program<br /> ( program_name => 'Exampleprogram'<br /> , program_type => 'STORED_PROCEDURE'<br /> , program_action => 'package.procedure'<br /> , number_of_arguments => 1<br /> , enabled => FALSE<br /> , comments => 'A demo program object.' );<br /> <br /> dbms_scheduler.define_program_argument<br /> ( program_name => 'Exampleprogram'<br /> , argument_position => 1<br /> , argument_name => 'parameter_1'<br /> , argument_type => 'VARCHAR2'<br /> , default_value => 'Scott'<br /> ) ;<br /> dbms_scheduler.enable( name => 'Exampleprogram' );<br />END;<br />/
Creating a job is now easier than ever:
BEGIN <br /> DBMS_SCHEDULER.create_job<br /> ( job_name => 'Examplejob'<br /> , program_name => 'Exampleprogram'<br /> , schedule_name => 'run_every_hour'<br /> , enabled => FALSE<br /> , comments => 'Job that starts according to predefined schedule'<br /> ) ;<br />
dbms_scheduler.set_job_argument_value<br /> ( job_name => 'Examplejob'<br /> , argument_position => 1<br /> , argument_value => 'Tiger' );<br /><br /> dbms_scheduler.enable( 'Examplejob' );<br /><br /> commit;
END;
/
The parameter program_type can be one of the values:
‘PLSQL_BLOCK’, ‘STORED_PROCEDURE’ or ‘EXECUTABLE’. With ‘executable’, dbms_scheduler allows to execute shell scripts and
executables.
Ooops…
Please
note that in Oracle 10g R1 there is a vulnerability in which user with
access to DBMS_SCHEDULER and the create job privilege are able to gain
DBA and remote OS rights. Pete Finnigan found the bug and there is a
patch available as described here.
Recent Comments