測試環境:Oracle9i,PL/SQL Developer 8.0,PL/SQL Developer 7.0
一同事通過PL/SQL Developer 8.0 建立 Oracle Jobs後,在Jobs檔案夾下面看不到了,而在DBMS_Jobs檔案夾下面的。而通過PL/SQL Developer 7.0 建立 Oracle Jobs後,是可以在Jobs檔案夾下面看到的。對此很迷惑。
1:通過PL/SQL Developer 7.0 串連到Oralce後,在Jobs檔案夾看到DBMS_Jobs(PL/SQL Developer 8.0內建,PL/SQL Developer 7.0沒有)檔案夾下面的自訂Jobs。所以通過PL/SQL Developer 8.0 建立 Oracle Jobs在DBMS_Jobs檔案夾下面和通過PL/SQL Developer 8.0 建立 Oracle Jobs 顯示在Jobs檔案夾下面是一樣的。
2:通過PL/SQL Developer 8.0 建立 Oracle Jobs 為什麼不顯示在Jobs檔案夾下面呢?
在PL/SQL Developer 8.0 To_Do_List 的Jobs 右鍵 New 出現的建立Jobs視窗和PL/SQL Developer 7.0差別很大。貌似這PL/SQL Developer 8.0 的Jobs 不支援Oralce 9i。因為一些選項不支援Oracle 9i
報錯:文字。表或視圖不存在。select owner,program_name from all_scheduler_programs.一些類似的錯誤。說明Oralce9i不支援在PL/SQL Developer 8.0 Jobs 右鍵 New 出現的建立Jobs的方法。
一下文字來源於網路資訊:
PL/SQL Developer 8.0 只支援使用 Oracle 10 dbms_scheduler 包建立的新型job(增強性計劃任務)
Oracle 10g DBMS_SCHEDULER的中度解析
針對DBMS_SCHEDULER的使用方法進行了詳盡的介紹,更多內容請參考下文:
DBMS_SCHEDULER是Oracle 10G中新增的一個包,與老版本的dbms_job包相比,dbms_scheduler有很多新特性,我將通過一系列的文章來介紹一下如何使用這個包.
1. 建立job
job是什麼呢? 簡單的說就是計劃(schedule)加上任務說明. 另外還有一些必須的參數.
這裡提到的"任務"可以是資料庫內部的預存程序,匿名的PL/SQL塊,也可以是作業系統層級的指令碼.
可以有兩種方式來定義"計劃":
1) 使用DBMS_SCHDULER.CREATE_SCHEDULE 定義一個計劃;
2) 調用DBMS_SCHDULER.CREATE_JOBE過程直接指定 (下面會詳細說明)
在建立一個計劃時,你至少需要指定下面的屬性,它們是job運行所必須的:
開始時間 (start_time);
重複頻率 (repeat_interval);
結束時間 (end_time)
另外,對於一個job而言,還有很多的附加參數:
job_class
job_priority
auto_drop
restartable
max_runs
max_failures
schedule_limit
logging_level
下面,我以問答的形式來具體解釋.
Q1:怎麼從資料庫中查詢job的屬性 ?
A1: 有兩種方法:
1) 查詢(DBA|ALL|USER)_SCHEDULER_JOBS 視圖
(提示: 根據使用者權限的不同,選擇性的查詢 DBA|ALL|USER視圖)
2) 調用DBMS_SCHEDULER包中的GET_ATTRIBUTE 過程
Q2: 怎麼設定這些屬性呢?
A2: 也是有兩種方法
1) 在建立job時直接指定
2) 調用DBMS_SCHEDULER包中的SET_ATTRIBUTE 過程
Q3: "我需要什麼許可權才能建立job" ?
它可以建立屬主為任何使用者(SYS使用者除外)的job.
預設情況下,job會被建立在當前的schema下,並且是沒有啟用的; 如果要使job一建立
就自動啟用,需要顯式的設定enabled 屬性為true, 來看一個例子:
begin
dbms_scheduler.create_job
(
job_name => 'ARC_MOVE',
schedule_name => 'EVERY_60_MINS',
job_type => 'EXECUTABLE',
job_action => '/home/dbtools/move_arcs.sh',
enabled => true,
comments => 'Move Archived Logs to a Different Directory'
);
end;
/
Q4: 能不能詳細地講述一下上面這個過程用到的各個參數?
A4:
job_name: 顧名思義,每個job都必須有一個的名稱
schedule_name: 如果定義了計劃,在這裡指定計劃的名稱
job_type: 目前支援三種類型:
PL/SQL塊: PLSQL_BLOCK,
預存程序: STORED_PROCEDURE
外部程式: EXECUTABLE (外部程式可以是一個shell指令碼,也可以是作業系統層級的指令).
job_action: 根據job_type的不同,job_action有不同的含義.
如果job_type指定的是預存程序,就需要指定預存程序的名字;
如果job_type指定的是PL/SQL塊,就需要輸入完整的PL/SQL代碼;
如果job_type指定的外部程式,就需要輸入script的名稱或者作業系統的指令名
enabled: 上面已經說過了,指定job建立完畢是否自動啟用
comments: 對於job的簡單說明
2. 指定job的執行頻率
如果我們建立了一個job,並且希望它按照我們指定的日期和時間來運行,就需要定義job的重複頻度了. 例如每天運行,每周日的22:00運行, 每周一,三,五運行,每年的最後一個星期天運行等等.
(說明:10G以前的版本,與作業系統的互動方面,實現的不是很好。例如要實現一個週期性rman備份任務,就需要結合OS的命令來實現,在UNIX下可以用crontab實現,在windows下用AT命令來實現)
10G 在這方面有了很大的增強,因為建立job時可以直接指定作業系統的命令或者指令碼,再合理的定義job的執行頻率,可以很輕鬆地完成複雜的調度任務.
10G 支援兩種模式的repeat_interval,一種是PL/SQL運算式,這也是dbms_job包中所使用的,例如SYSDATE+1, SYSDATE + 30/24*60; 另一種就是日曆運算式。
例如MON表示星期一,SUN表示星期天,DAY表示每天,WEEK表示每周等等. 下面來看幾個使用日曆運算式的例子:
repeat_interval => 'FREQ=HOURLY; INTERVAL=2'
每隔2小時運行一次job
repeat_interval => 'FREQ=DAILY'
每天運行一次job
repeat_interval => 'FREQ=WEEKLY; BYDAY=MON,WED,FRI"
每周的1,3,5運行job
repeat_interval => 'FREQ=YEARLY; BYMONTH=MAR,JUN,SEP,DEC; BYMONTHDAY=30'
每年的3,6,9,12月的30號運行job
用過crontab的人應該都有種似曾相識的感覺吧,呵呵
下面再說說使用日曆運算式的規則:
日曆運算式基本分為三部分: 第一部分是頻率,也就是"FREQ"這個關鍵字,它是必須指定的; 第二部分是時間間隔,也就是"INTERVAL"這個關鍵字,取值範圍是1-999. 它是可選的參數; 最後一部分是附加的參數,可用於精確地指定日期和時間,它也是可選的參數,例如下面這些值都是合法的:
BYMONTH,BYWEEKNO,BYYEARDAY,BYMONTHDAY,BYDAY
BYHOUR,BYMINUTE,BYSECOND
詳細的參數說明請參考 dbms_scheduler的使用說明.
既然說到了repeat_interval,你可能要問:"有沒有一種簡便的方法來得出,或者說是評估出job的每次已耗用時間,以及下一次的已耗用時間呢?"
dbms_scheduler包提供了一個過程evaluate_calendar_string,可以很方便地完成這個需求. 來看下面的例子:
SQL> set serveroutput on size 999999
SQL> declare
L_start_date TIMESTAMP;
l_next_date TIMESTAMP;
l_return_date TIMESTAMP;
begin
l_start_date := trunc(SYSTIMESTAMP);
l_return_date := l_start_date;
for ctr in 1..10 loop
dbms_scheduler.evaluate_calendar_string(
'FREQ=DAILY; BYDAY=MON,TUE,WED,THU,FRI; BYHOUR=7,15',
l_start_date, l_return_date, l_next_date
);
dbms_output.put_line('Next Run on: ' ||
to_char(l_next_date,'mm/dd/yyyy hh24:mi:ss')
);
l_return_date := l_next_date;
end loop;
end;
/
輸出結果如下:
Next Run on: 03/22/2004 07:00:00
Next Run on: 03/22/2004 15:00:00
Next Run on: 03/23/2004 07:00:00
Next Run on: 03/23/2004 15:00:00
Next Run on: 03/24/2004 07:00:00
Next Run on: 03/24/2004 15:00:00
Next Run on: 03/25/2004 07:00:00
Next Run on: 03/25/2004 15:00:00
Next Run on: 03/26/2004 07:00:00
Next Run on: 03/26/2004 15:00:00
如果想要支援的話,只能使用SQL命令列管理傳統job了
Job scheduling from Oracle 10g with dbms_scheduler
In Oracle 10g the DBMS_JOB package is replaced by the DBMS_SCHEDULER package. The DBMS_JOB package is now depricated and in Oracle 10g it's only provided for backward compatibility. From Oracle 10g the DBMS_JOB package should not be used any more, because is could not exist in a future version of Oracle.
With DBMS_SCHEDULER Oracle procedures and functions can be executed. Also binary and shell-scripts can be scheduled.
Rights
If you have DBA rights you can do all the scheduling. For administering job scheduling you need the priviliges belonging to the SCHEDULER_ADMIN role. To create and run jobs in your own schedule you need the 'CREATE JOB' privilege.
With DBMS_JOB you needed to set an initialization parameter to start a job coordinator background process. With Oracle 10g DBMS_SCHEDULER this is not needed any more.
If you want to user resource plans and/or consumer groups you need to set a system parameter:
ALTER SYSTEM SET RESOURCE_LIMIT = TRUE;
Getting started quickly
To quickly get a job running, you can use code like this:
begin
dbms_scheduler.create_job(
job_name => 'DEMO_JOB_SCHEDULE'
,job_type => 'PLSQL_BLOCK'
,job_action => 'begin package.procedure(''param_value''); end; '
,start_date => '01/01/2006 02:00 AM'
,repeat_interval => 'FREQ=DAILY'
,enabled => TRUE
,comments => 'Demo for job schedule.');
end;
/
This schedules a pl/sql block to be executed daily starting 1/1/2006 02:00 AM.
You can schedule things like this, but DBMS_SCHEDULER can reuse components.
You can build a schedule using components like program, schedule, job, job class and window. We will now discuss these components in detail.
Program
The program component represents program-code that can be executed. This program code can have parameters. Code example
begin
dbms_scheduler.create_program (
program_name => 'DEMO_JOB_SCHEDULE'
,program_type => 'STORED_PROCEDURE'
,program_action => 'package.procedure'
,number_of_arguments => 1
,enabled => FALSE
,comments => 'Demo for job schedule.');
dbms_scheduler.define_program_argument (
program_name => 'DEMO_JOB_SCHEDULE'
,argument_position => 1
,argument_name => 'kol1'
,argument_type => 'VARCHAR2'
,default_value => 'default'
);
dbms_scheduler.enable(name => 'DEMO_JOB_SCHEDULE');
end;
/
The parameter program_type can have one of the following values: 'PLSQL_BLOCK', 'STORED_PROCEDURE','EXECUTABLE'.
dbms_scheduler also allows to execute shell scripts (Windows: *.bat files) and executables.
Schedule
A schedule defines the frequence and date/time specifics of the start-time for the job.
example code
begin
dbms_scheduler.create_schedule(
schedule_name => 'DEMO_SCHEDULE'
, start_date => '01/01/2006 22:00:00'
, repeat_interval => 'FREQ=WEEKLY'
, comments => 'Weekly at 22:00');
END;
/
To drop the schedule:
begin
dbms_scheduler.drop_schedule(
schedule_name => 'DEMO_SCHEDULE'
, force => TRUE );
end;
/
Calendar expresions can have one of these values: 'Yearly','Monthly','Weekly','Daily','Hourly','Minutely','Secondely'
Job
A job defines when a specific task will be started. This can be done by assigning a program to one or more schedules (or to a specific date/time). A job can belong to only 1 job class. Code example
begin
dbms_scheduler.create_job(
job_name => 'DEMO_JOB1'
, program_name =>'DEMO_JOB_SCHEDULE'
, schedule_name =>'DEMO_SCHEDULE'
, enabled => FALSE
, comments => 'Run demo program every week at 22:00');
dbms_scheduler.set_job_argument_value(
job_name => 'DEMO_JOB1'
, argument_position => 1
, argument_value => 'param1');
dbms_scheduler.enable('DEMO_JOB1');
commit;
end;
/
Or start shell script
begin
dbms_scheduler.create_job
(
job_name => 'RUN_SHELL1',
schedule_name => 'DEMO_SCHEDULE',
job_type => 'EXECUTABLE',
job_action => '/home/test/run_script.sh',
enabled => true,
comments => 'Run shell-script'
);
end;
/
Monitoring job-scheduling
Jobs can be monitored using Oracle Enterprise Manager 10g. It's also possible to use a number of views that have been created in Oracle 10g. We will discuss some of these views here.
To show details on job run:
select log_date
, job_name
, status
, req_start_date
, actual_start_date
, run_duration
from dba_scheduler_job_run_details
To show running jobs:
select job_name
, session_id
, running_instance
, elapsed_time
, cpu_used
from dba_scheduler_running_jobs;
To show job history:
select log_date
, job_name
, status
from dba_scheduler_job_log;
show all schedules:
select schedule_name, schedule_type, start_date, repeat_interval
from dba_scheduler_schedules;
show all jobs and their attributes:
select *
from dba_scheduler_jobs
show all program-objects and their attributes
select *
from dba_scheduler_programs;
show all program-arguments:
select *
from dba_scheduler_program_args;