Oracle9i Database Administrator's Guide
Release 1 (9.0.1)

Part Number A90117-01
Go To Documentation Library
Go To Product List
Book List
Go To Table Of Contents
Go To Index

Master Index


Go to previous page Go to next page

Managing Job Queues

This chapter describes how to use job queues to schedule the periodic execution of user jobs, and contains the following topics:

Enabling Processes Used for Executing Jobs

You can schedule routines (jobs) to be run periodically using the job queue. To schedule a job you submit it to the job queue, using the Oracle supplied DBMS_JOBS package, and specify the frequency at which the job is to be run. Additional functionality enables you to alter, disable, or delete a job that you previously submitted.

Job queue (Jnnn) processes execute jobs in the job queue. For each instance, these job queue processes are dynamically spawned by a coordinator job queue (CJQ0) background process. The coordinator periodically selects jobs that are ready to run from the jobs shown in the DBA_JOBS view. It orders them by time, and then spawns Jnnn processes to run the selected jobs. Each Jnnn process executes one of the selected jobs.

The JOB_QUEUE_PROCESSES initialization parameter controls whether a coordinator job queue process is started by an instance. If this parameter is set to 0, no coordinator job queue process is started at database startup, and consequently no job queue jobs are executed. The JOB_QUEUE_PROCESSES initialization parameter also specifies the maximum number of Jnnn processes that can concurrently run on an instance. The maximum number of processes that can be specified is 1000.

The following initialization parameter setting causes the coordinator job queue process to start at database startup, and allows the spawning of a maximum of 60 concurrent Jnnn processes.


In any given period that the coordinator job queue process scans the jobs shown in the DBA_JOBS view, it spawns at most only the number of Jnnn processes required to execute the jobs it has selected. While the above example allows for 60 concurrent Jnnn processes, if only 20 jobs are selected for execution, then the coordinator spawns, or reuses, only the number of Jnnn processes necessary to execute the 20 jobs (at least, 20). Any idle existing Jnnn processes are considered available for reuse.

When a Jnnn process finishes execution of a job, it polls for another job to execute. If there are no jobs selected for execution, it enters an idle state, but wakes up periodically to poll again. If, after a predetermined number of tries, it still finds no jobs to execute, it terminates.

The JOB_QUEUE_PROCESSES initialization parameter is dynamic and it can be modified by an ALTER SYSTEM statement. For example, the following statement sets the maximum number of concurrent Jnnn processes allowed to 20.


If the new value is lower than the previous setting and less than the number of currently executing Jnnn processes, the excess processes are allowed to complete before they are terminated.

Jnnn processes will not execute jobs if the instance is running in restricted mode.

See also:

"Restricting Access to an Open Database" for information about enabling and disabling restricted mode 

Managing Job Queues

This section describes the various aspects of managing job queues and contains the following topics:

The DBMS_JOB Package

To schedule and manage jobs in the job queue, use the procedures in the DBMS_JOB package. There are no database privileges associated with using job queues. Any user who can execute the job queue procedures can use the job queue.

The following are procedures of the DBMS_JOB package. They are described in this section as noted.

Procedure  Description 


Submits a job to the job queue. See "Submitting a Job to the Job Queue"


Removes a specified job from the job queue. See "Removing a Job from the Job Queue"


Alters a specified job that has already been submitted to the job queue. You can alter the job description, the time at which the job will be run, or the interval between executions of the job. See "Altering a Job"


Alters the job description for a specified job. See "Altering a Job"


Alters the next execution time for a specified job. See "Altering a Job"


Alters the interval between executions for a specified job. See "Altering a Job".  


Sets or resets the job broken flag. If a job is marked as broken, Oracle does not attempt to execute it. See "Broken Jobs"


Forces a specified job to run. See "Forcing a Job to Execute"

See Also:


Submitting a Job to the Job Queue

To submit a new job to the job queue, use the SUBMIT procedure in the DBMS_JOB package. You specify the following parameters with the SUBMIT procedure:

Parameter  Description 


An output parameter. This is the identifier assigned to the job you are creating. You must use this job number whenever you want to alter or remove the job. See "Job Number"


This is the PL/SQL code you want to have executed. See "Job Definition"


This is the next date when the job will be run. The default value is SYSDATE


This is the date function that calculates the next time to execute the job. The default value is NULL. INTERVAL must evaluate to a future point in time or NULL. See "Job Execution Interval"


This is a flag. If NO_PARSE is set to FALSE (the default), Oracle parses the procedure associated with the job. If NO_PARSE is set to TRUE, Oracle parses the procedure associated with the job the first time that the job is executed. If, for example, you want to submit a job before you have created the tables associated with the job, set NO_PARSE to TRUE

For example, consider the following statements that 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.

      ''dquon'', ''accounts'', 
      ''ESTIMATE'', NULL, 50);', 
      SYSDATE, 'SYSDATE + 1');
Statement processed.
PRINT jobno

Job Environment

When you submit a job to the job queue or alter a job's definition, Oracle records the following environment characteristics:

Oracle also records the following NLS parameters:

Oracle restores all of these environment characteristics every time a job is executed. NLS_LANGUAGE and NLS_TERRITORY parameters determine the defaults for unspecified NLS parameters.

You can change a job's environment by using the DBMS_SQL package and the ALTER SESSION statement.

See Also:


Jobs and Import/Export

Jobs can be exported and imported. Thus, if you define a job in one database, you can transfer it to another database. When exporting and importing jobs, the job's number, environment, and definition remain unchanged.


If the job number of a job you want to import matches the number of a job already existing in the database, you will not be allowed to import that job. Submit the job as a new job in the database. 

Job Owner

When you submit a job to the job queue, Oracle identifies you as the owner of the job. Only a job's owner can alter the job, force the job to run, or remove the job from the queue.

Job Number

A queued job is identified by its job number. When you submit a job, its job number is automatically generated from the sequence SYS.JOBSEQ. Once a job is assigned a job number, that number does not change. Even if the job is exported and imported, its job number remains the same.

Job Definition

The job definition is the PL/SQL code specified in the WHAT parameter of the SUBMIT procedure. Normally, the job definition is a single call to a procedure. The procedure call can have any number of parameters.


In the job definition, use two single quotation marks around strings. Always include a semicolon at the end of the job definition. 

The following are examples of valid job definitions:

Job Execution Interval

If a job should be executed periodically at a set interval, use a date expression similar to 'SYSDATE + 7' in the INTERVAL parameter. Below are shown some common date expressions used for job execution intervals.

Date Expression  Evaluation 

Exactly seven days from the last execution 

'SYSDATE + 1/48'

Every half hour 


Every Monday at 3PM 


First Thursday of each quarter 


When specifying NEXT_DATE or INTERVAL, remember that date literals and strings must be enclosed in single quotation marks. Also, the value of INTERVAL must be enclosed in single quotation marks.  

The INTERVAL date function is evaluated immediately before a job is executed. When the job completes successfully, the date calculated from INTERVAL becomes the new NEXT_DATE. For example, if you set the execution interval to 'SYSDATE + 7' on Monday, but for some reason (such as a network failure) the job is not executed until Thursday, 'SYSDATE + 7' then executes every Thursday, not Monday. If the INTERVAL date function evaluates to NULL and the job completes successfully, the job is deleted from the queue.

If you always want to automatically execute a job at a specific time, regardless of the last execution (for example, every Monday), the INTERVAL and NEXT_DATE parameters should specify a date expression similar to 'NEXT_DAY(TRUNC(SYSDATE), ''MONDAY'')'.

Database Links and Jobs

If you submit a job that uses a database link, the link must include a username and password. Anonymous database links will not succeed.

How Jobs Execute

Jnnn processes execute jobs. To execute a job, the process creates a session to run the job. When a Jnnn process runs a job, the job is run in the same environment in which it was submitted and with the owner's default privileges. The owner must be explicitly granted the necessary object privileges for all objects referenced within the job definition.

When you force a job to run using the procedure DBMS_JOB.RUN, the job is run by your user process and with your default privileges only. Privileges granted to you through roles are unavailable. You must be explicitly granted the necessary object privileges for all objects referenced within the job definition.

Job Queue Locks

Oracle uses job queue locks to ensure that a job is executed in only one session at a time. When a job is being run, its session acquires a job queue (JQ) lock for that job. You can use the locking views in the data dictionary to examine information about locks currently held by sessions.

The following query lists the session identifier, lock type, and lock identifiers for all sessions holding JQ locks:


      SID TY       ID1       ID2
--------- -- --------- ---------
       12 JQ         0     14144
1 row selected.

In the query above, the identifier for the session holding the lock is 12. The ID1 column is always 0 for JQ locks. The ID2 column is the job number of the job the session is running. This view can be joined with the DBA_JOBS_RUNNING view to obtain more information about the job.

See Also:


Job Execution Errors

When a job fails, information about the failure is recorded in a trace file and the alert log. Oracle writes message number ORA-12012 and includes the job number of the failed job.

The following can prevent the successful execution of queued jobs:

If a job returns an error while Oracle is attempting to execute it, Oracle tries to execute it again. The first attempt is made after one minute, the second attempt after two minutes, the third after four minutes, and so on, with the interval doubling between each attempt. If the job fails 16 times, Oracle automatically marks the job as broken and no longer tries to execute it. However, between attempts, you have the opportunity to correct the problem that is preventing the job from running. This will not disturb the retry cycle, and Oracle will eventually attempt to run the job again.

Removing a Job from the Job Queue

To remove a job from the job queue, use the REMOVE procedure in the DBMS_JOB package.

The following statement removes job number 14144 from the job queue:


Altering a Job

To alter a job that has been submitted to the job queue, use the procedures CHANGE, WHAT, NEXT_DATE, or INTERVAL in the DBMS_JOB package.



You can alter any of the user-definable parameters associated with a job by calling the DBMS_JOB.CHANGE procedure.

In this example, the job identified as 14144 altered to execute every three days:


If you specify NULL for WHAT, NEXT_DATE, or INTERVAL when you call the procedure DBMS_JOB.CHANGE, the current value remains unchanged.


When you change a job's definition using the WHAT parameter in the procedure DBMS_JOB.CHANGE, Oracle records your current environment. This becomes the new environment for the job. 


You can alter the definition of a job by calling the DBMS_JOB.WHAT procedure.

The following example changes the definition of the job identified as 14144:

DBMS_JOB.WHAT(14144, 'scott.emppackage.give_raise(''RBAYLIS'', 6000.00);'


When you execute the procedure DBMS_JOB.WHAT, Oracle records your current environment. This becomes the new environment for the job. 


You can alter the next execution time for a job by calling the DBMS_JOB.NEXT_DATE procedure, as shown in the following example:



The following example illustrates changing the execution interval for a job by calling the DBMS_JOB.INTERVAL procedure:


In this case, the job will not run again after it successfully executes.

Broken Jobs

A job is labeled as either broken or not broken. Oracle does not attempt to run broken jobs. However, you can force a broken job to run by calling the procedure DBMS_JOB.RUN.

How a Job Becomes Broken

When you submit a job it is considered not broken.

There are two ways a job can break:

Once a job has been marked as broken, Oracle will not attempt to execute the job until you either mark the job as not broken, or force the job to be executed by calling the procedure DBMS_JOB.RUN.

The following example marks job 14144 as not broken and sets its next execution date to the following Monday:


Running Broken Jobs

If a problem has caused a job to fail 16 times, Oracle marks the job as broken. Once you have fixed this problem, you can run the job by either:

If you force the job to run by calling the procedure DBMS_JOB.RUN, Oracle runs the job immediately. If the job succeeds, then Oracle labels the job as not broken and resets its count of the number of failed executions for the job to zero.

Once you reset a job's broken flag (by calling either RUN or BROKEN), job execution resumes according to the scheduled execution intervals set for the job.

Forcing a Job to Execute

There may be times when you would like to manually execute a job. For example, if you have fixed a broken job, you may want to test the job immediately by forcing it to execute. To force a job to execute immediately, use the procedure RUN in the DBMS_JOB package.

When you run a job using DBMS_JOB.RUN, Oracle recomputes the next execution date. For example, if you create a job on a Monday with a NEXT_DATE value of 'SYSDATE' and an INTERVAL value of 'SYSDATE + 7', the job is run every 7 days starting on Monday. However, if you execute RUN on Wednesday, the next execution date will be set to the next Wednesday.

The following statement runs job 14144 in your session and recomputes the next execution date:



When you force a job to run, the job is executed in your current session. Running the job reinitializes your session's packages. 


Terminating a Job

You can terminate a running job by marking the job as broken, identifying the session running the job, and disconnecting that session. You should mark the job as broken, so that Oracle does not attempt to run the job again.

After you have identified the session running the job (using V$SESSION or V$LOCK, as shown earlier), you can disconnect the session using the SQL statement ALTER SYSTEM. For examples of viewing information about jobs and sessions, see the next section, "Viewing Job Queue Information".

See Also:


Viewing Job Queue Information

You can view information about jobs in the job queue using the data dictionary views listed below:

View  Description 




DBA view describes all the jobs in the database. ALL view describes all jobs that are accessible to the current user. USER view describes all jobs owned by the current user. 


Lists all jobs in the database that are currently running. This view can be joined with V$LOCK to identify jobs that have locks.  

Displaying Information About a Job

The following query creates a listing of the job number, next execution time, failure count, and broken status for each job you have submitted:


-------  ---------  --------  --------  -
   9125  01-JUN-01  00:00:00         4  N
  14144  24-OCT-01  16:35:35         0  N
  41762  01-JUN-01  00:00:00        16  Y
3 rows selected.

Displaying Information About Running Jobs

You can also display information about only the jobs currently running. The following query lists the session identifier, job number, user who submitted the job, and the start times for all currently running jobs:

  WHERE r.JOB = j.JOB;

-----  ----------   -------------   ---------  --------
   12       14144   JFEE            24-OCT-94  17:21:24
   25        8536   SCOTT           24-OCT-94  16:45:12
2 rows selected.

See Also:

Oracle9i Database Reference for more information on data dictionary views 

Go to previous page Go to next page
Copyright © 1996-2001, Oracle Corporation.

All Rights Reserved.
Go To Documentation Library
Go To Product List
Book List
Go To Table Of Contents
Go To Index

Master Index