Detailed explanation of the use of job in Oracle

Reprinted from: http://blog.csdn.net/dogwoods/article/details/2010571

 

In project development, we often have some complex business logic. Using oracle 's stored procedures can greatly reduce the workload of java program code writing, and the stored procedures are executed on the database, so that the good performance support of oracle can be used , and the program execution efficiency and stability can be greatly improved. To execute stored procedures regularly, use job .

The following is a description of commonly used parameters:

Field (column)           type Description JOB NUMBER The unique identification number of the task LOG_USER VARCHAR2(30) The user who submits the task PRIV_USER VARCHAR2(30) The user who grants the task privilege SCHEMA_USER VARCHAR2(30) The user schema for parsing the task LAST_DATE DATE The last success The time when the task was run LAST_SEC VARCHAR2(8) The hour, minute and second of the last_date date in HH24:MM:SS format THIS_DATE DATE The start time of the running task, or null if no task is running THIS_SEC VARCHAR2(8) Such as HH24:MM :hour, minute and second of this_date date in SS format NEXT_DATE DATE time of the next scheduled task NEXT_SEC VARCHAR2(8) HH24:MM:SS format of next_date date in hour, minute and second TOTAL_TIME NUMBER required for the task to run Total time, in seconds BROKEN VARCHAR2(1) Flag parameter, Y indicates that the task is interrupted and will not run in the future 
 
 
 
 
 
 
 
 
 
 
 
 
INTERVAL VARCHAR2(200) The expression used to calculate the next running time FAILURES NUMBER The number of times the task has run without success WHAT VARCHAR2(2000) The PL/SQL block that executes the task CURRENT_SESSION_LABEL RAW MLSLABEL The task's trusted Oracle session CLEARANCE_HI RAW MLSLABEL The Task trusted Oracle maximum clearance CLEARANCE_LO RAW MLSLABEL This task trusted Oracle minimum clearance NLS_ENV VARCHAR2(2000) NLS session settings for task running MISC_ENV RAW(32) Some other session parameters for task running 
 
 
 
 
 
 


Description INTERVAL parameter value every day at 12 midnight 'TRUNC(SYSDATE + 1)' every day at 8:30 am 'TRUNC(SYSDATE + 1) + (8*60+30)/(24*60)' every Tuesday at 12 noon' NEXT_DAY(TRUNC(SYSDATE ), ''TUESDAY'' ) + 12/24' 12:00 midnight on the first day of each month 'TRUNC(LAST_DAY(SYSDATE ) + 1)' 11:00 pm on the last day of each quarter' TRUNC (ADD_MONTHS(SYSDATE + 2/24, 3 ), 'Q' ) -1/24'every Saturday and Sunday at 6:10 am 'TRUNC(LEAST(NEXT_DAY(SYSDATE, ''SATURDAY"), NEXT_DAY(SYSDATE, " SUNDAY"))) + (6×60+10)/(24×60)' 
 
 
 
 
 

 

1. Set the initialization parameter  job_queue_processes
  sql> alter system set job_queue_processes=n; (n>0)
  The maximum value of job_queue_processes is 1000.
  
  View the job queue background process
  sql>select name, description from v$bgprocess;
  
  2. The usage introduction of dbms_job package
  includes the following subsections Procedure:   Broken() procedure.   change() process.   Interval() procedure.   The Isubmit() process.   Next_Date() procedure.   Remove() process.   Run() process.   Submit() process.   User_Export() procedure.   What() procedure.   1. The Broken() procedure updates the status of a submitted job, typically used to mark a broken job as unbroken.   This process has three parameters: job, broken and next_date.   PROCEDURE Broken (job IN binary_integer,            Broken IN boolean,            next_date IN date :=SYSDATE) 
  










  


  



  
  The job parameter is the job number, which uniquely identifies the job in the question.
  The broken parameter indicates whether the job will be marked broken - TRUE indicates that the job will be marked broken, while FLASE indicates that the job will be marked unbroken. The bitsCN.Com  next_date parameter indicates when this job will run again. This parameter defaults to the current date and time.   If the job fails for some reason, oracle will retry 16 times and still fail to execute successfully, it will be marked as broken to restart the job whose status is broken. There are two ways:   a. Use dbms_job. run() executes the job immediately     sql>begin     sql>dbms_job.run(:jobno) The jobno is the job number returned when the submit process submits     sql>end;     sql>/   b. Use dbms_job.broken() to mark the broken again as false     sql>begin     sql>dbms_job.broken (:job,false,next_date)     sql>end;     sql>/   2. The Change() process is used to change the settings of the specified job.   This process has four parameters: job, what, next_date and interval.   PROCEDURE Change (job IN binary_integer,            What IN varchar2,  














  


           next_date IN date,
           interval IN varchar2)
  
  This job parameter is an integer value that uniquely identifies this job.
  The What parameter is a block of PL/SQL code that is run by this job.
  The next_date parameter indicates when this job will be executed.
  The interval parameter indicates how often a job should be re-executed. 3. The Interval   () procedure is used to explicitly set the number of time intervals between re-executing a job. This process has two parameters: job and interval.   PROCEDURE Interval (job IN binary_integer,             Interval IN varchar2)   The job parameter identifies a specific job. The interval parameter indicates how often a job should be re-executed.   4. The ISubmit() procedure is used to submit a job with a specific job number. This procedure has five parameters: job, what, next_date, interval and no_parse.   PROCEDURE ISubmit (job IN binary_ineger,             What IN varchar2,             next_date IN date,             interval IN varchar2,             no_parse IN booean:=FALSE) 
  

  


  

  

  





  
  The only difference between this procedure and the Submit() procedure is that the job parameter is passed as an IN parameter and includes a job number provided by the developer. An error will be generated if the provided job number is already in use.
  
  5. The Next_Date() procedure is used to explicitly set the execution time of a job. This procedure accepts two parameters: job and next_date.
  
  PROCEDURE Next_Date(job IN binary_ineger,
            next_date IN date) bitscn.com   job identifies an existing job. The next_date parameter indicates the date and time that this job should be performed.   6. Remove() procedure to remove a job that has been scheduled to run. This procedure accepts one parameter:   PROCEDURE Remove(job IN binary_ineger); The   job parameter uniquely identifies a job. The value of this parameter is the value of the job parameter returned by calling the Submit() procedure for this job. A job that is already running cannot be deleted by the calling program.   7. The Run() procedure is used to execute a specified job immediately. This procedure accepts only one parameter:   PROCEDURE Run(job IN binary_ineger)   The job parameter identifies the job to be executed immediately.   8. Using the Submit() procedure, the work is planned as normal.   This procedure has five parameters: job, what, next_date, interval and no_parse.   PROCEDURE Submit ( job OUT binary_ineger,             What IN varchar2, 

  

  

  

  

  
 
  

  


  


            next_date IN date,
            interval IN varchar2,
            no_parse IN booean:=FALSE)
  
  The job parameter is the binary_ineger returned by the Submit() procedure. This value is used to uniquely identify a job.
  The what parameter is the block of PL/SQL code to be executed.

China . Network Management Alliance


  next_date参数指识何时将运行这个工作。
  interval参数何时这个工作将被重执行。
  no_parse参数指示此工作在提交时或执行时是否应进行语法分析——TRUE指示此PL/SQL代码在它第一次执行时应进行语法分析,而FALSE指示本PL/SQL代码应立即进行语法分析。
  
  9、User_Export()过程返回一个命令,此命令用来安排一个存在的工作以便此工作能重新提交。
  此程序有两个参数:job与my_call。
  
  PROCEDURE User_Export(job    IN binary_ineger,
             my_call  IN OUT varchar2)
  
  job参数标识一个安排了的工作。my_call参数包含在它的当前状态重新提交此工作所需要的正文。
  
  10、What()过程应许在工作执行时重新设置此正在运行的命令。这个过程接收两个参数:job与what。
  
  PROCEDURE What (job IN binary_ineger,
          What IN OUT varchar2)
  job参数标识一个存在的工作。what参数指示将被执行的新的PL/SQL代码。
  
  三、查看相关job信息
  1、相关视图
  dba_jobs
  all_jobs
  user_jobs
  dba_jobs_running 包含正在运行job相关信息

www_bitscn_com


  
  2、查看相关信息
  
  SQL>SELECT JOB, NEXT_DATE, NEXT_SEC, FAILURES, BROKEN
  SQL>FROM DBA_JOBS;
  
  JOB NEXT_DATE NEXT_SEC FAILURES B
  ------- --------- -------- -------- -
  9125 01-JUN-01 00:00:00 4 N
  14144 24-OCT-01 16:35:35 0 N
  9127 01-JUN-01 00:00:00 16 Y
  3 rows selected.
  
  正在运行的JOB相关信息
  
  SELECT SID, r.JOB, LOG_USER, r.THIS_DATE, r.THIS_SEC
  FROM DBA_JOBS_RUNNING r, DBA_JOBS j
  WHERE r.JOB = j.JOB;
  
  SID JOB LOG_USER THIS_DATE THIS_SEC
  ----- ---------- ------------- --------- --------
  12 14144 HR 24-OCT-94 17:21:24
  25 8536 QS 24-OCT-94 16:45:12
  2 rows selected.
   
  JOB QUEUE LOCK相关信息
  
  SELECT SID, TYPE, ID1, ID2
  FROM V$LOCK
  WHERE TYPE = 'JQ';
  
  SID TY ID1 ID2
  --------- -- --------- ---------
  12 JQ 0 14144
  1 row selected.
  
  四、简单例子
  一个简单例子: 中国网管联盟 
  
  创建测试表
  SQL> create table TEST(a date);
  
  表已创建。
  
  创建一个自定义过程
  SQL> create or replace procedure MYPROC as
   2 begin
   3 insert into TEST values(sysdate);
   4 end;
   5 /
  
  过程已创建。
  
  创建JOB
  SQL> variable job1 number;
  SQL> 
  SQL> begin
   2 dbms_job.submit(:job1,'MYPROC;',sysdate,'sysdate+1/1440');  --每天1440分钟,即一分钟运行test过程一次
   3 end;
   4 /
  
  PL/SQL 过程已成功完成。
  
  运行JOB
  SQL> begin
   2 dbms_job.run(:job1);
   3 end;
   4 /
  
  PL/SQL 过程已成功完成。
  
  SQL> select to_char(a,'yyyy/mm/dd hh24:mi:ss') 时间 from TEST;
  
  时间
  -------------------
  2001/01/07 23:51:21
  2001/01/07 23:52:22
  2001/01/07 23:53:24
  
  删除JOB
  SQL> begin
   2 dbms_job.remove(:job1);
   3 end;
   4 / bitsCN_com 
  
  PL/SQL 过程已成功完成。

Guess you like

Origin http://10.200.1.11:23101/article/api/json?id=327037653&siteId=291194637