sql-server

11 Post

oracle

15 Post

postgresql

14 Post

my-sql

2 Post

common-sql

3 Post

News

5 News

Oracle DBMS_SCHEDULER — Job Chain

An Oracle Job Chain is a feature of DBMS_SCHEDULER that organizes multiple executable steps into a single workflow.

A chain controls the execution order, dependencies, parallel processing, synchronization, and failure handling of its steps.

Unlike a regular Scheduler job, a Job Chain can execute several programs sequentially or concurrently and start subsequent operations based on the results of previous steps.

1. Steps to Create a Job Chain

1.1. Create the chain — CREATE_CHAIN

Creates the chain structure.

BEGIN
    DBMS_SCHEDULER.CREATE_CHAIN(
        chain_name => 'DWH_CHAIN',
        comments   => 'DWH processing chain'
    );
END;
/

1.2. Create programs — CREATE_PROGRAM

Defines the operations executed by chain steps.

BEGIN
    DBMS_SCHEDULER.CREATE_PROGRAM(
        program_name   => 'PRG_JOB_1',
        program_type   => 'STORED_PROCEDURE',
        program_action => 'PRC_JOB_1',
        enabled        => TRUE
    );
END;
/

The stored procedure PRC_JOB_1 must already exist.

1.3. Define chain steps — DEFINE_CHAIN_STEP

Associates Scheduler programs with chain steps.

BEGIN
    DBMS_SCHEDULER.DEFINE_CHAIN_STEP(
        chain_name   => 'DWH_CHAIN',
        step_name    => 'JOB_1',
        program_name => 'PRG_JOB_1'
    );
END;
/

1.4. Define chain rules — DEFINE_CHAIN_RULE

Defines when a step executes.

BEGIN
    DBMS_SCHEDULER.DEFINE_CHAIN_RULE(
        chain_name => 'DWH_CHAIN',
        condition  => 'TRUE',
        action     => 'START JOB_1',
        rule_name  => 'RULE_1'
    );
END;
/

1.5. Enable the chain — ENABLE

Activates the chain for execution.

BEGIN
    DBMS_SCHEDULER.ENABLE(
        name => 'DWH_CHAIN'
    );
END;
/

1.6. Create the chain job — CREATE_JOB

Creates a Scheduler job associated with the chain.

BEGIN
    DBMS_SCHEDULER.CREATE_JOB(
        job_name   => 'JOB_DWH_CHAIN',
        job_type   => 'CHAIN',
        job_action => 'DWH_CHAIN',
        enabled    => TRUE,
        auto_drop  => FALSE
    );
END;
/

1.7. Execute the chain — RUN_JOB

Starts the chain.

BEGIN
    DBMS_SCHEDULER.RUN_JOB(
        job_name            => 'JOB_DWH_CHAIN',
        use_current_session => FALSE
    );
END;
/

1.8. Monitor execution

Displays currently running chain steps.

SELECT owner,
       job_name,
       job_subname,
       state
FROM   dba_scheduler_running_chains
WHERE  job_name = 'JOB_DWH_CHAIN';

 

2. Execution Types

2.1. Sequential execution

START
  |
  +--> JOB_1 --> SUCCEEDED
  |
  +--> JOB_2 --> SUCCEEDED
  |
  +--> JOB_3 --> RUNNING

Steps execute one after another, depending on the preceding step's result.

JOB_1

JOB_1 SUCCEEDED

JOB_2

JOB_2 SUCCEEDED

JOB_3

END

condition => 'TRUE',
action    => 'START JOB_1'

condition => 'JOB_1 SUCCEEDED',
action    => 'START JOB_2'

condition => 'JOB_2 SUCCEEDED',
action    => 'START JOB_3'

condition => 'JOB_3 SUCCEEDED',
action    => 'END'

Each condition/action pair represents a separate chain rule.

2.2. Parallel execution

START
  |
  +--> JOB_1 --> SUCCEEDED --+
  |                          |
  +--> JOB_2 --> SUCCEEDED --+--> END
  |                          |
  +--> JOB_3 --> SUCCEEDED --+

Multiple steps start independently and can execute concurrently.

START CHAIN

JOB_1

JOB_2

JOB_3

END CHAIN

condition => 'TRUE',
action    => 'START JOB_1'

condition => 'TRUE',
action    => 'START JOB_2'

condition => 'TRUE',
action    => 'START JOB_3'

condition => 'JOB_1 SUCCEEDED AND
              JOB_2 SUCCEEDED AND
              JOB_3 SUCCEEDED ',
action    => 'END'

2.3. Parallel execution with final calculation

START
  |
  +--> JOB_1 --> SUCCEEDED --+
  |                          |
  +--> JOB_2 --> SUCCEEDED --+--> FINAL_CALCULATION --> END
  |                          |
  +--> JOB_3 --> SUCCEEDED --+

Three steps execute concurrently. A final step starts after all three complete.

START CHAIN

JOB_1

JOB_2

JOB_3

ALL THREE COMPLETED

FINAL_CALCULATION

END

condition => 'JOB_1 SUCCEEDED AND
              JOB_2 SUCCEEDED AND
              JOB_3 SUCCEEDED ',
action    => 'START FINAL_CALCULATION'
condition => 'FINAL_CALCULATION SUCCEEDED',
action    => 'END'

2.4. Conditional execution and failure handling

 

START CHAIN
    |
    v
  JOB_1
    |
    +-- SUCCEEDED --> JOB_2
    |                   |
    |                   +-- SUCCEEDED --> JOB_3
    |                   |                   |
    |                   |                   +-- SUCCEEDED --> END SUCCESS
    |                   |                   |
    |                   |                   +-- FAILED ----> END ERROR 20003
    |                   |
    |                   +-- FAILED -----------------------> END ERROR 20002
    |
    +-- FAILED ----> ERROR_HANDLER
                         |
                         +-- SUCCEEDED --> END ERROR 20001
                         |
                         +-- FAILED ----> END ERROR 20004

Execution follows different paths depending on the step's result.

JOB_1

SUCCEEDED

JOB_2

END

FAILED

ERROR HANDLING

END ERROR

condition => 'JOB_1 SUCCEEDED',
action    => 'START JOB_2'
condition => 'JOB_1 FAILED',
action    => 'END 20001'

3. DEFINE_CHAIN_RULE Parameters

Parameter

Description

chain_name

Name of the chain

condition

Logical expression controlling execution

action

Operation performed when the condition is true

rule_name

Unique rule name

comments

Optional description

4. Condition Values

Condition

Description

TRUE

Unconditionally satisfied

step_name SUCCEEDED

Step completed successfully

step_name FAILED

Step failed

step_name COMPLETED

Step completed

step_name STOPPED

Step was stopped

step_name RUNNING

Step is executing

step_name NOT_STARTED

Step has not started

step_name ERROR_CODE

Numeric error code

Logical operators include AND, OR, NOT, and parentheses.

5. Action Values

Action

Description

START step_name

Starts a step

STOP step_name

Stops a running step

END

Ends the chain successfully

END error_code

Ends the chain with an error

6. Main Scheduler Methods

Method

Description

CREATE_CHAIN

Creates a chain

CREATE_PROGRAM

Creates an executable program

DEFINE_CHAIN_STEP

Adds a chain step

DEFINE_CHAIN_RULE

Defines execution rules

ALTER_CHAIN

Modifies chain or step attributes

ENABLE

Enables Scheduler objects

DISABLE

Disables Scheduler objects

CREATE_JOB

Creates a Scheduler job

RUN_JOB

Executes a job

STOP_JOB

Stops a running job

DROP_CHAIN_RULE

Removes a rule

DROP_CHAIN_STEP

Removes a step

DROP_CHAIN

Removes a chain

7. Monitoring Views

View

Description

DBA_SCHEDULER_CHAINS

Chain definitions

DBA_SCHEDULER_CHAIN_STEPS

Chain steps

DBA_SCHEDULER_CHAIN_RULES

Execution rules

DBA_SCHEDULER_RUNNING_CHAINS

Currently running chain steps

DBA_SCHEDULER_RUNNING_JOBS

Active Scheduler jobs

DBA_SCHEDULER_JOB_RUN_DETAILS

Execution history

DBA_SCHEDULER_JOB_LOG

Job activity

Important: Chain steps execute according to rule conditions, not the order in which the rules were created. Parallel execution is achieved through independent start rules, without a dedicated parallelism parameter.