Section 8.4. Managing Named Programs


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 path. And if you supply it more than once, you are again introducing opportunities for errors to enter your scheduling process.

Fortunately, DBMS_SCHEDULER allows you to define named programs, which make it extremely easy to reference the program to be executed. Named programs are essentially synonyms for the actual code being executed. Instead of calling the actual code segment, you simply reference the name you have given the program. Then, if you ever need to change your job to run a different program, you will not need to change the job definition. All you have to do is swap the new executable into the program, and your job will call the right one.

8.4.1. Creating a Program

In this next example, I'll use DBMS_SCHEDULER's CREATE_PROGRAM procedure to give a frequently called executable program the name KICK_RMAN_INC.

     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 creators. By default, a program defined in the CREATE_JOB procedure is assumed to be owned by the user creating the job. However, if you want, you can use a program owned by another user. For example, suppose that a user known as INTEREST_ADMIN owns the program to apply interest as follows:

     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!

The ability to execute named programs can be controlled by the EXECUTE privilege, but no privileges exist to reference any named schedule owned by a different user.


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.




Oracle PL(s)SQL For DBAs
Oracle PL(s)SQL For DBAs
ISBN: N/A
EAN: N/A
Year: 2005
Pages: 122

flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net