8.4. Managing Named Programs
I've explained two basic scheduling concepts: the job and the schedule. A third important concept is the
program
: the actual code that will be executed as scheduled. Now that you've learned how to call different types of programs, you might be tempted (quite justifiably) to place all scheduled processes whether in stored procedures, PL/SQL anonymous blocks, or operating system executablesunder Scheduler control. There is, however, one complication that you will need to be aware of. According to the syntax you've seen so far, you need to pass the full path of your executable (or your PL/SQL block or stored procedure) to the parameter job_action in the CREATE_JOB procedure. If the path is long (as it may often be), it may be inconvenient to specify the entire
Fortunately, DBMS_SCHEDULER allows you to define
named
programs, which make it extremely easy to reference the program to be executed. Named programs are
8.4.1. Creating a Program
In this
BEGIN
DBMS_SCHEDULER.create_program
(program_name => 'KICK_RMAN_INC',
program_type => 'EXECUTABLE',
program_action => '/u01/app/oracle/admin/tools/kick_rman_inc.sh',
enabled => TRUE,
comments => 'Take RMAN Inc Backup'
);
END;
After the program is created, my original RMAN incremental backup job will not need to specify the full path of the program or what type of job it is. The new definition looks like this:
BEGIN
DBMS_SCHEDULER.create_job (job_name => 'RMAN_INC',
program_name => 'KICK_RMAN_INC',
schedule_name => 'EVERY_DAY',
comments => 'RMAN Inc Backup',
enabled => TRUE
);
END;
Note that I've specified only the schedule name and the program name. Because the program (with its whole path) has already been defined as an executable, there is no need to specify what type of program it is (PL/SQL block, stored procedure or executable), so the program_type parameter is no longer needed in the job description. Furthermore, now that this program has been defined, you can also use it in another job without specifying any details. You can even change the job entirelyfor example, make it a PL/SQL block or a stored procedure instead of an OS executableand you will not have to change any of the code that creates the job. 8.4.2. Running Other Users' Programs
As I described when covering schedules, programs can be owned by users other than their
SQL>
CREATE USER interest_admin IDENTIFIED BY interest_admin;
User created.
SQL>
GRANT CREATE SESSION, CREATE JOB, CREATE PROCEDURE to interest_admin;
Grant succeeded.
SQL>
CONN interest_admin/interest_admin
Connected.
SQL>
CREATE PROCEDURE calc_int
2
AS
3
BEGIN
4
NULL;
5
END;
6
/
Procedure created.
I can now use this procedure in a named program called CAL_INTEREST as the user INTEREST_ADMIN:
BEGIN
DBMS_SCHEDULER.create_program (program_name => 'CALC_INTEREST',
program_type => 'STORED_PROCEDURE',
program_action => 'calc_int',
enabled => TRUE,
comments => 'Calculate Interest'
);
END;
Once the program has been created, I can create a schedule owned by the user SCHED_MANAGER. The schedule is named EVERY_DAY.
SQL>
CONN sched_manager/sched_manager
Connected.
SQL> BEGIN
2 DBMS_SCHEDULER.create_schedule (schedule_name => 'every_day',
3 start_date => SYSTIMESTAMP,
4 repeat_interval => 'FREQ=DAILY; BYHOUR=3',
5 comments => 'Schedule Run for Int Calc'
6 );
7 END;
8 /
PL/SQL procedure successfully completed.
Before user ACC_MANAGER can use this program, she must be granted the EXECUTE privilege on the program or she must have the EXECUTE ANY PROGRAM system privilege .
SQL>
CONN interest_admin/interest_admin
Connected.
SQL>
GRANT EXECUTE ON calc_interest TO acc_manager;
Grant succeeded.
Now as user ACC_MANAGER, I can create the job.
BEGIN
DBMS_SCHEDULER.create_job (job_name => 'Calculate_Daily_Interest',
program_name => 'INTEREST_ADMIN.CALC_INTEREST',
schedule_name => 'SCHED_MANAGER.EVERY_DAY',
comments => 'Daily Interest Calculation',
enabled => TRUE
);
END;
This will create a job called Calculate_Daily_Interest, which executes the program referenced by the named program CALC_INTEREST owned by the user INTEREST_ADMIN as per the schedule defined in the named schedule EVERY_DAY owned by the user SCHED_ADMIN. Whew!
Using named programs can significantly reduce administrative overhead. When you make a change in the executable, the path, or the name of the stored procedure, you will not have to change your job definitions. |