PL/SQL Packages and Types Reference 10g Release 1 (10.1) Part Number B10802-01 |
|
|
View PDF |
The DBMS_JOB
package schedules and manages jobs in the job queue.
Note: The For more information, see "Moving from |
This chapter contains the following topics:
No specific system privileges are required to use DBMS_JOB
. No system privileges are available to manage DBMS_JOB
. Jobs cannot be altered or deleted other than jobs owned by the user. This is true for all users including those users granted DBA privileges.
You can execute procedures that are owned by the user or for which the user is explicitly granted EXECUTE
. However, procedures for which the user is granted the execute privilege through roles cannot be executed.
Note that, once a job is started and running, there is no easy way to stop the job.
DBMS_JOB
supports multi-instance execution of jobs. By default jobs can be executed on any instance, but only one single instance will execute the job. In addition, you can force instance binding by binding the job to a particular instance. You implement instance binding by specifying an instance number to the instance affinity parameter. Note, however, that in 10i instance binding is not recommended. Service affinity is preferred. This concept is implemented in the DBMS_SCHEDULER package.
The following procedures can be used to create, alter or run jobs with instance affinity. Note that not specifying affinity means any instance can run the job.
To submit a job to the job queue, use the following syntax:
DBMS_JOB.SUBMIT( JOB OUT BINARY_INTEGER, WHAT IN VARCHAR2, NEXT_DATE IN DATE DEFAULTSYSDATE, INTERVAL IN VARCHAR2 DEFAULT 'NULL', NO_PARSE IN BOOLEAN DEFAULT FALSE, INSTANCE IN BINARY_INTEGER DEFAULT ANY_INSTANCE, FORCE IN BOOLEAN DEFAULT FALSE);
Use the parameters INSTANCE
and FORCE
to control job and instance affinity. The default value of INSTANCE
is 0 (zero) to indicate that any instance can execute the job. To run the job on a certain instance, specify the INSTANCE
value. Oracle displays error ORA-23319
if the INSTANCE
value is a negative number or NULL.
The FORCE
parameter defaults to FALSE.
If force is TRUE,
any positive integer is acceptable as the job instance. If FORCE
is FALSE,
the specified instance must be running, or Oracle displays error number ORA-23428.
To assign a particular instance to execute a job, use the following syntax:
DBMS_JOB.INSTANCE( JOB IN BINARY_INTEGER, INSTANCE IN BINARY_INTEGER, FORCE IN BOOLEAN DEFAULT FALSE);
The FORCE
parameter in this example defaults to FALSE.
If the instance value is 0 (zero), job affinity is altered and any available instance can execute the job despite the value of force. If the INSTANCE
value is positive and the FORCE
parameter is FALSE,
job affinity is altered only if the specified instance is running, or Oracle displays error ORA-23428.
If the FORCE parameter is TRUE, any positive integer is acceptable as the job instance and the job affinity is altered. Oracle displays error ORA-23319 if the INSTANCE value is negative or NULL.
To alter user-definable parameters associated with a job, use the following syntax:
DBMS_JOB.CHANGE( JOB IN BINARY_INTEGER, WHAT IN VARCHAR2 DEFAULT NULL, NEXT_DATE IN DATE DEFAULT NULL, INTERVAL IN VARCHAR2 DEFAULT NULL, INSTANCE IN BINARY_INTEGER DEFAULT NULL, FORCE IN BOOLEAN DEFAULT FALSE );
Two parameters, INSTANCE
and FORCE,
appear in this example. The default value of INSTANCE
is NULL
indicating that job affinity will not change.
The default value of FORCE
is FALSE.
Oracle displays error ORA-23428
if the specified instance is not running and error ORA-23319
if the INSTANCE
number is negative.
The FORCE
parameter for DBMS_JOB.RUN
defaults to FALSE.
If force is TRUE,
instance affinity is irrelevant for running jobs in the foreground process. If force is FALSE
, the job can run in the foreground only in the specified instance. Oracle displays error ORA-23428
if force is FALSE
and the connected instance is the incorrect instance.
DBMS_JOB.RUN( JOB IN BINARY_INTEGER, FORCE IN BOOLEAN DEFAULT FALSE);
Note that, once a job is started and running, there is no easy way to stop the job.
This procedure sets the broken flag. Broken jobs are never run.
DBMS_JOB.BROKEN ( job IN BINARY_INTEGER, broken IN BOOLEAN, next_date IN DATE DEFAULT SYSDATE);
Parameter | Description |
---|---|
|
Number of the job being run. |
|
Job broken: |
|
Date of the next refresh. |
Note: If you set job as broken while it is running, Oracle resets the job's status to normal after the job completes. Therefore, only execute this procedure for jobs that are not running. |
You must issue a COMMIT
statement immediately after the statement.
This procedure changes any of the fields a user can set in a job.
DBMS_JOB.CHANGE ( job IN BINARY_INTEGER, what IN VARCHAR2, next_date IN DATE, interval IN VARCHAR2, instance IN BINARY_INTEGER DEFAULT NULL, force IN BOOLEAN DEFAULT FALSE);
COMMIT
statement immediately after the statement.instance
and force
are added for job queue affinity. Job queue affinity gives users the ability to indicate whether a particular instance or any instance can run a submitted job.what
, next_date
, or interval
are NULL
, then leave that value as it is.BEGIN DBMS_JOB.CHANGE(14144, null, null, 'sysdate+3'); COMMIT; END;
This procedure changes job instance affinity.
DBMS_JOB.INSTANCE ( job IN BINARY_INTEGER, instance IN BINARY_INTEGER, force IN BOOLEAN DEFAULT FALSE);
You must issue a COMMIT
statement immediately after the statement.
This procedure changes how often a job runs.
DBMS_JOB.INTERVAL ( job IN BINARY_INTEGER, interval IN VARCHAR2);
Parameter | Description |
---|---|
|
Number of the job being run. |
|
Date function, evaluated immediately before the job starts running. |
next_date
. interval
is evaluated by plugging it into the statement select interval
into next_date
from dual;interval
parameter must evaluate to a time in the future. Legal intervals include:
Interval | Description |
---|---|
|
Run once a week. |
|
Run once every Tuesday. |
|
Run only once. |
interval
evaluates to NULL
and if a job completes successfully, then the job is automatically deleted from the queue.COMMIT
statement immediately after the statement.This procedure changes when an existing job next runs.
DBMS_JOB.NEXT_DATE ( job IN BINARY_INTEGER, next_date IN DATE);
Parameter | Description |
---|---|
|
Number of the job being run. |
|
Date of the next refresh: it is when the job will be automatically run, assuming there are background processes attempting to run it. |
You must issue a COMMIT
statement immediately after the statement.
This procedure removes an existing job from the job queue. This currently does not stop a running job.
DBMS_JOB.REMOVE ( job IN BINARY_INTEGER );
Parameter | Description |
---|---|
|
Number of the job being run. |
You must issue a COMMIT
statement immediately after the statement.
BEGIN DBMS_JOB.REMOVE(14144); COMMIT; END;
This procedure runs job JOB
now. It runs it even if it is broken.
Running the job recomputes next_date
. See view user_jobs
.
DBMS_JOB.RUN ( job IN BINARY_INTEGER, force IN BOOLEAN DEFAULT FALSE);
EXECUTE DBMS_JOB.RUN(14144);
An exception is raised if force
is FALSE
, and if the connected instance is the wrong one.
This procedure submits a new job. It chooses the job from the sequence sys
.jobseq
.
DBMS_JOB.SUBMIT ( job OUT BINARY_INTEGER, what IN VARCHAR2, next_date IN DATE DEFAULT sysdate, interval IN VARCHAR2 DEFAULT 'null', no_parse IN BOOLEAN DEFAULT FALSE, instance IN BINARY_INTEGER DEFAULT any_instance, force IN BOOLEAN DEFAULT FALSE);
COMMIT
statement immediately after the statement.instance
and force
are added for job queue affinity. Job queue affinity gives users the ability to indicate whether a particular instance or any instance can run a submitted job.This submits a new job to the job queue. The job calls the procedure DBMS_DDL
.ANALYZE_OBJECT
to generate optimizer statistics for the table DQUON
.ACCOUNTS
. The statistics are based on a sample of half the rows of the ACCOUNTS
table. The job is run every 24 hours:
VARIABLE jobno number; BEGIN DBMS_JOB.SUBMIT(:jobno, 'dbms_ddl.analyze_object(''TABLE'', ''DQUON'', ''ACCOUNTS'', ''ESTIMATE'', NULL, 50);' SYSDATE, 'SYSDATE + 1'); COMMIT; END; / Statement processed. print jobno JOBNO ---------- 14144
There are two overloaded procedures. The first produces the text of a call to re-create the given job. The second alters instance affinity (8i and after) and preserves the compatibility.
DBMS_JOB.USER_EXPORT ( job IN BINARY_INTEGER, mycall IN OUT VARCHAR2); DBMS_JOB.USER_EXPORT ( job IN BINARY_INTEGER, mycall IN OUT VARCHAR2, myinst IN OUT VARCHAR2);
Parameter | Description |
---|---|
|
Number of the job being run. |
|
Text of a call to re-create the given job. |
|
Text of a call to alter instance affinity. |
This procedure changes what an existing job does, and replaces its environment.
DBMS_JOB.WHAT ( job IN BINARY_INTEGER, what IN VARCHAR2);
Parameter | Description |
---|---|
|
Number of the job being run. |
|
PL/SQL procedure to run. |