PL/SQL Developer 8.0 创建 Oracle Jobs 的疑惑
通过PL/SQL Developer 8.0 创建 Oracle Jobs 的疑惑[转]测试环境: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;