Currently, the popular mainstream database has this feature, the most representative of Microsoft SQL Server 7.0, oracle8i/9i, and so on. However, to make the job work, we need to configure it to be implemented. These configurations all have GUI operations. This article describes the implementation of the job configuration by the command line under oracle9i ...
As we all know, the general operating system provides a way to perform tasks on a regular basis, such as a command Crontab that allows the system to perform tasks on a regular basis on UNIX platforms. However, for some requirements, such as: some of the operation of the database table, the most typical of the stock exchange after the closing of the day, it involves a large number of database table operations, if still using the operating system to carry out the timing, not only need a lot of programming work, but also there will be inconsistent user Even causes the program to not execute.
In fact, for the above requirements, we can take advantage of the function job queue, which is owned by the database itself, to implement it. A job allows a user to schedule and schedule a task in advance, allowing it to be automatically executed one or more times during a specified time or time period, and is highly efficient because the task is executed in the database.
Job allows us to customize the execution time of the task and provide flexible processing, but also through configuration, scheduling tasks in the system user access to a small number of times to execute, greatly improve the efficiency. For example, the long, repetitive work of daily database backups, updates, deletions, and replications, and the timing push in the value-added messaging business, we can use jobs to automate tasks to reduce workload.
Currently, the popular mainstream database has this feature, the most representative of Microsoft SQL Server 7.0, oracle8i/9i, and so on. However, to make the job work, we need to configure it to be implemented. These configurations all have GUI operations. This article describes the implementation of the job configuration through the command line under Oracle9i.
Prerequisite: Written stored procedures to be executed periodically [cannot take parameters].
Defining a job, execution interval is an important thing to be aware of. SYSDATE+1/24 is the interval stored in the Dba_jobs view, which produces a snapshot that is not available once in an hour. You can change this data to a different sampling time, 24*60 = 1440 minutes in a day, and you can use this number to adjust the number of executions. For example: I want to get a snapshot every 10 minutes, and you should use the following command:
Execute Dbms_job.submit (
: Jobno,--Job number
' SP; ',--process of execution
Trunc (sysdate+10/1440, ' MI '),--Next execution time
' Trunc (sysdate+10/1440, ' MI ') ',--time interval
True,--no_parse
: Instno);
1. Create Job
Create a task that is performed every 5 minutes.
Variable V_SN number;
Begin
Dbms_job.submit (: V_SN,
' P_push_send; ',
Trunc (sysdate+5/1440, ' MI '),
' Trunc (sysdate+5/1440, ' MI ') ');
Commit
End
/
To delete a task:
Execute Dbms_job.remove (jobno);
2. Query Task statement
Two tables involved: User_jobs and dba_jobs_running[View Dba_jobs]
SELECT * from User_jobs;
SELECT * from Dba_jobs_running;
SELECT * from Dba_jobs;
3. Necessary parameters [modify Initsid.ora parameters]
job_queue_processes = 4--Number of executable jobs
Job_queue_interval = 10--Default interval delay time 10s
Job_queue_keep_connections=true--job Maintain normal connection
Modify the number of executable jobs to 20:
ALTER SYSTEM SET job_queue_processes = 2;0
To modify the cancellation restriction mode:
ALTER SYSTEM DISABLE restricted session;
4. Several related job operations
Delete Job:dbms_job.remove (jobno);
Modify the action to be performed: Dbms_job.what (Jobno,what);
Modify the next execution time: Dbms_job.next_date (job,next_date);
Modification interval: dbms_job.interval (job,interval);
Stop Job:dbms.broken (job,broken,nextdate);
Start Job:dbms_job.run (jobno);
First, set initialization parameters job_queue_processes
Sql> alter system set job_queue_processes=n; (n>0)
Job_queue_processes Maximum value is 1000
View the job queue background process
Sql>select name,description from V$bgprocess;
Ii. Introduction to the usage of Dbms_job package
Contains the following child procedures:
Broken () process.
The change () procedure.
Interval () process.
Isubmit () process.
Next_date () process.
The Remove () procedure.
The Run () procedure.
Submit () process.
User_export () process.
What () process.
1. The broken () process updates the status of a submitted work, typically used to mark a broken job as not broken.
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 work number, which uniquely identifies the work in the issue.
The broken parameter indicates whether this work will be marked as broken--true indicates that the work will be marked as broken, and Flase indicates that the work will be marked as not broken.
The Next_date parameter indicates when this work will run again. The default value for this parameter is the current date and time.
If the job fails for some reason, Oracle will retry the execution 16 times, and it will be marked as a job with a broken reboot status of broken, as in the next two ways;
A, use Dbms_job.run () to execute the job immediately
Sql>begin
Sql>dbms_job.run (: jobno) The job number returned by the Jobno when submitting the submit process
sql>end;
sql>/
B, use Dbms_job.broken () to mark broken again as false
Sql>begin
Sql>dbms_job.broken (: job,false,next_date)
sql>end;
sql>/
2. The change () process is used to alter the settings of the specified work.
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 the job.
The What parameter is a block of Pl/sql code that runs from this work.
The Next_date parameter indicates when this work will be performed.
The interval parameter indicates how often a work is executed.
3. The Interval () procedure is used to explicitly set the number of time intervals between the redo of a work. 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 work is executed.
4. The Isubmit () process is used to submit a job with a specific work number. This process 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 process and the submit () procedure is that the job parameter is passed as an in parameter and includes a work number provided by the developer. If the supplied work number is already in use, an error is generated.
5. The next_date () process is used to explicitly set the execution time of a job. This process receives two parameters: Job and Next_date.
PROCEDURE next_date (Job in Binary_ineger,
Next_date in date)
Job identifies a work that already exists. The Next_date parameter indicates the date and time this work should be performed.
6. Remove () process to delete a scheduled operation. This procedure receives an argument:
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 that is returned by calling the Submit () procedure for this work. The work that is already running cannot be deleted by the calling program.
7. The Run () process is used to perform a specified task immediately. This procedure receives only one parameter:
PROCEDURE Run (Job in Binary_ineger)
The job parameter identifies the work that will be executed immediately.
8, the use of the submit () process, the work is normally planned well.
This process 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 PL/SQL code block that will be executed.
The Next_date parameter indicates when this work will be run.
Interval parameter when this work will be executed again.
The No_parse parameter indicates whether this work should be parsed at commit time or at execution--true indicates that the Pl/sql code should parse when it first executes, and False indicates that the PL/SQL code should be parsed immediately.
9. The User_export () procedure returns a command that is used to schedule an existing work so that the work can be resubmitted.
This program has two parameters: Job and My_call.
PROCEDURE user_export (Job in Binary_ineger,
My_call in Out VARCHAR2)
The job parameter identifies an arranged work. The My_call parameter contains the body text required to resubmit this work in its current state.
10. The What () process promises to reset this running command when work is performed. This process receives two parameters: job and what.
PROCEDURE What (Job in Binary_ineger,
What in Out VARCHAR2)
The job parameter identifies a work that exists. The What parameter indicates the new PL/SQL code that will be executed.
Third, view related job information
1. Related view
Dba_jobs
All_jobs
User_jobs
Dba_jobs_running contains information about running job
2, view the relevant information
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 Y
3 Rows selected.
Running job-related information
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
----- ---------- ------------- --------- --------
14144 HR 24-oct-94 17:21:24
8536 QS 24-oct-94 16:45:12
2 rows selected.
JOB QUEUE Lock Related information
SELECT SID, TYPE, ID1, ID2
From V$lock
WHERE TYPE = ' JQ ';
SID TY ID1 ID2
--------- -- --------- ---------
JQ 0 14144
1 row selected.
Iv. Simple Examples
A simple example:
To create a test table
Sql> CREATE TABLE TEST (a date);
Table has been created.
Create a custom procedure
sql> Create or replace procedure MYPROC as
2 begin
3 INSERT into TEST values (sysdate);
4 End;
5/
Procedure has been created.
Create Job
sql> variable JOB1 number;
Sql>
Sql> begin
2 Dbms_job.submit (: Job1, ' MYPROC; ', sysdate, ' sysdate+1/1440 '); --1440 minutes a day, that is, a minute to run the test process once
3 END;
4/
The PL/SQL process has completed successfully.
Run Job
Sql> begin
2 Dbms_job.run (: JOB1);
3 END;
4/
The PL/SQL process has completed successfully.
Sql> Select To_char (A, ' yyyy/mm/dd hh24:mi:ss ') time from TEST;
Time
-------------------
2001/01/07 23:51:21
2001/01/07 23:52:22
2001/01/07 23:53:24
Delete Job
Sql> begin
2 Dbms_job.remove (: JOB1);
3 END;
4/
The PL/SQL process has completed successfully.