在Oracle10g里面,已经不赞成使用DBMS_JOB,推荐使用DBMS_SCHEDULER,它的健壮为调度提供更好的规划和结构。下面我只简单写一个例子。
begin
DBMS_SCHEDULER.CREATE_PROGRAM(
program_name => 'guozhw',
program_type => 'STORED_PROCEDURE',
program_action => 'guozhwTest',
number_of_arguments => 0,
enabled => true,
comments => '定时调度测试'
);
end;
program_name:program的名字
program_type:program的类型。STORED_PROCEDURE存储过程
program_action:要执行的程序。guozhwTest 存储过程的名字
举例如下:
BEGIN
DBMS_SCHEDULER.CREATE_PROGRAM (
program_name => 'PRM_CREATEDUNDATA',
program_action => 'PKG_SCHEDULER.CREATEDUNDATA',
program_type => 'STORED_PROCEDURE',
enabled => FALSE,
number_of_arguments => 5,
comments => '生成催缴数据'
);
DBMS_SCHEDULER.DEFINE_PROGRAM_ARGUMENT(PROGRAM_NAME => 'PRM_CREATEDUNDATA',
ARGUMENT_POSITION => 1,
ARGUMENT_NAME => 'I_BILLINGCYCLEID',
ARGUMENT_TYPE => 'VARCHAR2',
DEFAULT_VALUE => NULL,
OUT_ARGUMENT => FALSE);
DBMS_SCHEDULER.DEFINE_PROGRAM_ARGUMENT(PROGRAM_NAME => 'PRM_CREATEDUNDATA',
ARGUMENT_POSITION => 2,
ARGUMENT_NAME => 'I_BUSINESSTYPE',
ARGUMENT_TYPE => 'VARCHAR2',
DEFAULT_VALUE => NULL,
OUT_ARGUMENT => FALSE);
DBMS_SCHEDULER.DEFINE_PROGRAM_ARGUMENT(PROGRAM_NAME => 'PRM_CREATEDUNDATA',
ARGUMENT_POSITION => 3,
ARGUMENT_NAME => 'I_STATE',
ARGUMENT_TYPE => 'VARCHAR2',
DEFAULT_VALUE => NULL,
OUT_ARGUMENT => FALSE);
DBMS_SCHEDULER.DEFINE_PROGRAM_ARGUMENT(PROGRAM_NAME => 'PRM_CREATEDUNDATA',
ARGUMENT_POSITION => 4,
ARGUMENT_NAME => 'I_BANKCODE',
ARGUMENT_TYPE => 'VARCHAR2',
DEFAULT_VALUE => NULL,
OUT_ARGUMENT => FALSE);
DBMS_SCHEDULER.DEFINE_PROGRAM_ARGUMENT(PROGRAM_NAME => 'PRM_CREATEDUNDATA',
ARGUMENT_POSITION => 5,
ARGUMENT_NAME => 'I_OPERATORCODE',
ARGUMENT_TYPE => 'VARCHAR2',
DEFAULT_VALUE => NULL,
OUT_ARGUMENT => FALSE);
DBMS_SCHEDULER.ENABLE(NAME => 'PRM_CREATEDUNDATA');
COMMIT;
END;
之后就可以在select * from sys.USER_SCHEDULER_PROGRAMS t中看到program了
×××××××××××××××××××××××××××××××××××××××××***********************************************************
最近遇到一个CASE,要做定时任务,想来想去,还是先看看Oracle提供了啥..
Oracle10g引入了一种新的作业调度程序:dbms_scheduler,通过如下命令可以查看它包含一些什么功能,
SQL> desc sys.dbms_scheduler;
...
不过,最好还是用PL/SQL dev或者TOAD来展开包头,这样所有功能和注释都一幕了然了。
在Oracle9i,我们都是job来运行定时任务,例如定时备份一些数据,定时清理一下归档。在Oracle10g里面,已经不
赞成试用DBMS_JOB,而是推荐使用DBMS_SCHEDULER,它更加的复杂和健壮,为调度提供更好的规划和结构。
dbms_scheduler把创建一个作业分成了不同的部分:dbms_scheduler,program和job,下面我们依次来创建一个例子。
首先创建一个测试表:
SQL> desc user118800;
Name Type Nullable Default Comments
------------- ------------ -------- ------- --------
SN NUMBER 序列
AREACODE VARCHAR2(4) 区号
DEVICESNUMBER VARCHAR2(12) 电话号码
READ INTEGER 0 是否读取
BIT INTEGER 业务位
DDATE DATE Y
有如下存储过程:
create or replace procedure pro_test is
begin
UPDATE user118800 SET READ=1,ddate=SYSDATE WHERE READ=0 AND ROWNUM <2;
COMMIT;
--sys.dbms_lock.sleep(120);
end pro_test;
这个测试,就是用过程来更新user118800表里面的一行数据,并记录下更新时间,接着我们就创建作业来定时执行。
创建一个scheduler:
/*************************************************************
* Schedule Administration Procedures
*************************************************************
*/
-- Schedule attributes which can be used with set_attribute/get_attribute are :
--
-- repeat_interval - VARCHAR2
-- an expression using the calendar syntax
-- comments - VARCHAR2
-- an optional comment.
-- end_date - TIMESTAMP WITH TIME ZONE
-- cutoff date after which the schedule will not specify
-- any dates
-- start_date - TIMESTAMP WITH TIME ZONE
-- start or reference date used by the calendar syntax
--
-- Schedules cannot be enabled and disabled.
-- Create a named schedule. This must be a valid schedule.
PROCEDURE create_schedule(
schedule_name IN VARCHAR2,
start_date IN TIMESTAMP WITH TIME ZONE DEFAULT NULL,
repeat_interval IN VARCHAR2,
end_date IN TIMESTAMP WITH TIME ZONE DEFAULT NULL,
comments IN VARCHAR2 DEFAULT NULL);
这是创建scheduler的存储过程,运行如下:
begin
-- Call the procedure
sys.dbms_scheduler.create_schedule(schedule_name => 'your schedule_name',
start_date => '27-8月 -07 12.00.00.000 上午',
repeat_interval => 'FREQ=MINUTELY; INTERVAL=1',
end_date => '28-8月 -07 12.00.00.000 上午',
comments => 'TEST SCHEDULER');
end;
这里需要注意的是,参数start_date和end_date都是TIMESTAMP 类型,在输入的时候要遵循它们的格式,
往往doc里面的格式都是英文环境下的,这个时候你要通过查询字典nls_database_parameters或者
select to_timestamp(sysdate) from dual; 来获得格式。
repeat_interval是调度运行的时间间隔,本例是每分钟运行一次
repeat_interval => 'FREQ=HOURLY; INTERVAL=1'
每小时运行一次
repeat_interval => 'FREQ=DAILY INTERVAL=1'
每天运行一次
更详细的说明,要查询doc。另外它还有一个过程dbms_scheduler.evaluate_calendar_string是估算下一次运行的时间的。
第二步,创建一个program
/*************************************************************
* Program Administration Procedures
*************************************************************
*/
-- Program attributes which can be used with set_attribute/get_attribute are:
--
-- program_action - VARCHAR2
-- This is a string specifying the action. In case of:
-- 'PLSQL_BLOCK': PLSQL code
-- 'STORED_PROCEDURE: name of the database object
-- representing the type (optionally with schema).
-- 'EXECUTABLE': Full pathname including the name of the
-- executable, or shell script.
-- program_type - VARCHAR2
-- type of program. This must be one of the supported
-- program types. Currently these are
-- 'PLSQL_BLOCK', 'STORED_PROCEDURE', 'EXECUTABLE'
-- comments - VARCHAR2
-- an optional comment. This can describe what the
-- program does, or give usage details.
-- number_of_arguments- PLS_INTEGER
-- the number of arguments of the program that can be set
-- by any job using it, these arguments MUST be defined
-- before the program can be enabled
-- enabled - BOOLEAN
-- whether the program is enabled or not. When the program
-- is enabled, checks are made to ensure that the program
-- is valid.
-- Create a new program. The program name can be optionally qualified with a
-- schema. If enabled is set to TRUE, validity checks will be performed and
-- the program will be created in an enabled state if all are passed.
PROCEDURE create_program(
program_name IN VARCHAR2,
program_type IN VARCHAR2,
program_action IN VARCHAR2,
number_of_arguments IN PLS_INTEGER DEFAULT 0,
enabled IN BOOLEAN DEFAULT FALSE,
comments IN VARCHAR2 DEFAULT NULL);
运行如下:
begin
-- Call the procedure
sys.dbms_scheduler.create_program(program_name => 'your program_name',
program_type => 'PLSQL_BLOCK',
program_action => 'BEGIN PRO_TEST; END;',
number_of_arguments => 0,
enabled => TRUE,
comments => 'comments');
end;
这里需要注意的是program_type,如果你要运行PLSQL块,则选择PLSQL_BLOCK,如果是sh脚本,则'EXECUTABLE'。
这里我选择PLSQL块。
第三步创建一个job
-- create a job using a named schedule object and a named program object.
-- If enabled is set TRUE, it will be attempted to enable this job after
-- creating it.
-- Values must be set for each argument of the program that does not have a
-- default_value specified (before enabling the job).
PROCEDURE create_job(
job_name IN VARCHAR2,
program_name IN VARCHAR2,
schedule_name IN VARCHAR2,
job_class IN VARCHAR2 DEFAULT 'DEFAULT_JOB_CLASS',
enabled IN BOOLEAN DEFAULT FALSE,
auto_drop IN BOOLEAN DEFAULT TRUE,
comments IN VARCHAR2 DEFAULT NULL);
实际在10g提供的过程里面,有6中create job的方法,而且也可以单独创建作业,但这里我们选择其中一种。
begin
-- Call the procedure
sys.dbms_scheduler.create_job(job_name => 'my test job',
program_name => 'your program_name',
schedule_name => 'your schedule_name',
job_class => 'DEFAULT_JOB_CLASS',
enabled => true,
auto_drop => true,
comments => 'comments');
end;
这样就完成了一个job的创建,下面就是运行它:
begin
-- Call the procedure
sys.dbms_scheduler.run_job(job_name => 'my test job',
use_current_session => 'false');
end;
use_current_session 这个参数要定义为false,它才能在后台运行。
创建以后的监控:
查看调度
SELECT * FROM User_Scheduler_Schedules;
查看作业
SELECT * FROM User_Scheduler_Jobs;
查看程序
SELECT * FROM User_Scheduler_Programs;
查看执行中的作业,也就是PRO_TEST在运行阶段的作业。前面的过程中,我定义了一个sys.dbms_lock.sleep(120);
实际就是为了查看这个视图。
SELECT * FROM User_Scheduler_Running_Jobs;
查看job的日志
SELECT * FROM User_Scheduler_Job_Log;
如果日志中有FAILeD的状态,那么就要查看altersid.log了。
好了,一个基本的调度例子就完成了。如果想更详细的了解它,请阅读该包头的注释,和查阅相关DOC
Oracle® Database
PL/SQL Packages and Types Reference
10g Release 2 (10.2)
在9i的时候,job有一个烦人的问题,就是延时,一个job的执行耗时是10分钟,inteval为1天,第一次执行为晚上12点,
在以后的执行过程中,该job为逐渐按10分钟递增时间。10g的Scheduler解决了这个问题,但是我还是发现Scheduler同样无法异步的执行,我的inteval是1分钟,但在程序中如果打开
sleep(120)后,实际inteval就变成了120秒...它还是和job一样,必须等待上一次任务的完成,这样也没有达到我的初衷。
最后,我也应用中是否最终会使用Scheduler,还有待测试。除了Scheduler,在中间层,如EJB和Spring....