使用dbms_job包来实现数据库后台进程
2006-06-19 10:29
405 查看
1建立实现任务的过程
在schemamanager或SQLPLUS里建立如下过程
CREATEORREPLACEPROCEDURE"CUSTOMER"."T_JOBTEST"as
begin
updateempsetactive=0
whereactive=1
anddate_published<sysdate-active_days;
end;
2向任务队列中加入任务
在SQLPLUS中执行下列script
VARIABLEjobnonumber;
begin
DBMS_JOB.SUBMIT(:jobno,'t_jobtest();',SYSDATE,'SYSDATE+1');
commit;
end;
该任务立即执行(SYSDATE),并且每隔一天执行一次('SYSDATE+1')。
3查询此任务是否加入任务队列
在SQLPLUS中执行下列script
SELECTjob,next_date,next_sec,failures,broken
FROMuser_jobs;
------------------
DBMS_JOB包介绍
调度任务队列里的任务要使用DBMS_JOB包中的过程。使用任务队列不需要特别的数据库特权。任何可以使用这些过程的用户都可以使用任务队列。
Table8-2DBMS_JOB包中的过程
SubmittingaJobtotheJobQueue向任务队列提交一个任务
Tosubmitanewjobtothejobqueue,usetheSUBMITprocedureintheDBMS_JOBpackage:
DBMS_JOB.SUBMIT(jobOUTBINARY_INTEGER,whatINVARCHAR2,next_dateINDATEDEFAULTSYSDATE,intervalINVARCHAR2DEFAULT'null',no_parseINBOOLEANDEFAULTFALSE)
TheSUBMITprocedurereturnsthenumberofthejobyousubmitted.Table8-3describestheprocedure'sparameters.
Table8-3DBMS_JOB.SUBMIT的参数
Asanexample,let'ssubmitanewjobtothejobqueue.ThejobcallstheprocedureDBMS_DDL.ANALYZE_OBJECTtogenerateoptimizerstatisticsforthetableDQUON.ACCOUNTS.ThestatisticsarebasedonasampleofhalftherowsoftheACCOUNTStable.Thejobisrunevery24hours:
VARIABLEjobnonumber;begin2>DBMS_JOB.SUBMIT(:jobno,3>'dbms_ddl.analyze_object(''TABLE'',4>''DQUON'',''ACCOUNTS'',5>''ESTIMATE'',NULL,50);'6>SYSDATE,'SYSDATE+1');7>commit;8>end;9>/Statementprocessed.printjobnoJOBNO----------14144
任务定义就是SUBMIT过程中WHAT参数中指定的PL/SQL代码。
通常任务定义(内容)是一个过程的一个调用。这个过程能有任意数量的参数。
Jobscanbeexportedandimported.Thus,ifyoudefineajobinonedatabase,youcantransferittoanotherdatabase.Whenexportingandimportingjobs,thejob'snumber,environment,anddefinitionremainunchanged.
任务是可以被卸出(exported)卸入(imported)的。
TheINTERVALdatefunctionisevaluatedimmediatelybeforeajobisexecuted.Ifthejobcompletessuccessfully,thedatecalculatedfromINTERVALbecomesthenewNEXT_DATE.IftheINTERVALdatefunctionevaluatestoNULLandthejobcompletessuccessfully,thejobisdeletedfromthequeue.
Ifajobshouldbeexecutedperiodicallyatasetinterval,useadateexpressionsimilarto'SYSDATE+7'intheINTERVAL
Ifyoualwayswanttoautomaticallyexecuteajobataspecifictime,regardlessofthelastexecution(forexample,everyMonday),theINTERVALandNEXT_DATEparametersshouldspecifyadateexpressionsimilarto'NEXT_DAY(TRUNC(SYSDATE),"MONDAY")'.
Table8-5listssomecommondateexpressionsusedforjobexecutionintervals.
DBMS_JOB.REMOVE(jobINBINARY_INTEGER)
Thefollowingstatementremovesjobnumber14144fromthejobqueue:
DBMS_JOB.REMOVE(14144);
Table8-3describestheprocedure'sparameters.
Table8-3describestheprocedure'sparameters.
Table8-3describestheprocedure'sparameters.
可以通过下表中的数据字典视图来查询任务队列中的任务的信息。
[align=center]Table8-6任务队列信息的视图[/align]
Forexample,youcandisplayinformationaboutajob'sstatusandfailedexecutions.Thefollowingsamplequerycreatesalistingofthejobnumber,nextexecutiontime,failures,andbrokenstatusforeachjobyouhavesubmitted:
显示任务的状态和失败执行的次数。
Youcanalsodisplayinformationaboutjobscurrentlyrunning.Thefollowingsamplequeryliststhesessionidentifier,jobnumber,userwhosubmittedthejob,andthestarttimesforallcurrentlyrunningjobs:
显示当前运行的任务的信息。
parameter.Forexample,ifyousettheexecutionintervalto'SYSDATE+7'onMonday,butforsomereason(suchasanetworkfailure)thejobisnot
executeduntilThursday,'SYSDATE+7'thenexecuteseveryThursday,notMonday.
在schemamanager或SQLPLUS里建立如下过程
CREATEORREPLACEPROCEDURE"CUSTOMER"."T_JOBTEST"as
begin
updateempsetactive=0
whereactive=1
anddate_published<sysdate-active_days;
end;
2向任务队列中加入任务
在SQLPLUS中执行下列script
VARIABLEjobnonumber;
begin
DBMS_JOB.SUBMIT(:jobno,'t_jobtest();',SYSDATE,'SYSDATE+1');
commit;
end;
该任务立即执行(SYSDATE),并且每隔一天执行一次('SYSDATE+1')。
3查询此任务是否加入任务队列
在SQLPLUS中执行下列script
SELECTjob,next_date,next_sec,failures,broken
FROMuser_jobs;
------------------
DBMS_JOB包介绍
调度任务队列里的任务要使用DBMS_JOB包中的过程。使用任务队列不需要特别的数据库特权。任何可以使用这些过程的用户都可以使用任务队列。
Table8-2DBMS_JOB包中的过程
Procedure | Description | Described |
---|---|---|
SUBMIT | Submitsajobtothejobqueue.向任务队列提交一个任务 | |
REMOVE | Removesaspecifiedjobfromthejobqueue.从任务队列中删除指定的任务 | |
CHANGE | Altersaspecifiedjob.Youcanalterthejobdescription,thetimeatwhichthejobwillberun,ortheintervalbetweenexecutionsofthejob.改变任务 | |
WHAT | Altersthejobdescriptionforaspecifiedjob.改变指定任务的任务内容 | |
NEXT_DATE | Altersthenextexecutiontimeforaspecifiedjob.改变指定任务的下一次执行时间 | |
INTERVAL | Alterstheintervalbetweenexecutionsforaspecifiedjob.改变指定任务的执行时间间隔。 | |
BROKEN | Disablesjobexecution.Ifajobismarkedasbroken,Oracledoesnotattempttoexecuteit.禁止指定任务的执行 | |
RUN | Forcesaspecifiedjobtorun.强制执行指定的任务 |
Tosubmitanewjobtothejobqueue,usetheSUBMITprocedureintheDBMS_JOBpackage:
DBMS_JOB.SUBMIT(jobOUTBINARY_INTEGER,whatINVARCHAR2,next_dateINDATEDEFAULTSYSDATE,intervalINVARCHAR2DEFAULT'null',no_parseINBOOLEANDEFAULTFALSE)
TheSUBMITprocedurereturnsthenumberofthejobyousubmitted.
Table8-3DBMS_JOB.SUBMIT的参数
Parameter | Description |
---|---|
job | Thisistheidentifierassignedtothejobyoucreated.Youmustusethejobnumberwheneveryouwanttoalterorremovethejob.Formoreinformationaboutjobnumbers,see |
what | ThisisthePL/SQLcodeyouwanttohaveexecuted.这里是你想执行的PL/SQL代码 Formoreinformationaboutdefiningajob,see |
next_date | Thisisthenextdatewhenthejobwillberun.ThedefaultvalueisSYSDATE. |
interval | Thisisthedatefunctionthatcalculatesthenexttimetoexecutethejob.ThedefaultvalueisNULL.INTERVALmustevaluatetoafuturepointintimeorNULL. Formoreinformationonhowtospecifyanexecutioninterval,see |
no_parse | Thisisaflag.ThedefaultvalueisFALSE. IfNO_PARSEissettoFALSE(thedefault),Oracleparsestheprocedureassociatedwiththejob.IfNO_PARSEissettoTRUE,Oracleparsestheprocedureassociatedwiththejobthefirsttimethatthejobisexecuted.If,forexample,youwanttosubmitajobbeforeyouhavecreatedthetablesassociatedwiththejob,setNO_PARSEtoTRUE. |
VARIABLEjobnonumber;begin2>DBMS_JOB.SUBMIT(:jobno,3>'dbms_ddl.analyze_object(''TABLE'',4>''DQUON'',''ACCOUNTS'',5>''ESTIMATE'',NULL,50);'6>SYSDATE,'SYSDATE+1');7>commit;8>end;9>/Statementprocessed.printjobnoJOBNO----------14144
JobDefinition任务定义
任务定义就是SUBMIT过程中WHAT参数中指定的PL/SQL代码。
通常任务定义(内容)是一个过程的一个调用。这个过程能有任意数量的参数。
Note: Inthejobdefinition,usetwosinglequotationmarksaroundstrings.Alwaysincludeasemicolonattheendofthejobdefinition.在任务定义中,用2个单引号包围字符串。任务定义的末尾总是带一个分号。 |
JobsandImport/Export
Jobscanbeexportedandimported.Thus,ifyoudefineajobinonedatabase,youcantransferittoanotherdatabase.Whenexportingandimportingjobs,thejob'snumber,environment,anddefinitionremainunchanged.
任务是可以被卸出(exported)卸入(imported)的。
JobExecutionInterval任务的执行间隔
TheINTERVALdatefunctionisevaluatedimmediatelybeforeajobisexecuted.Ifthejobcompletessuccessfully,thedatecalculatedfromINTERVALbecomesthenewNEXT_DATE.IftheINTERVALdatefunctionevaluatestoNULLandthejobcompletessuccessfully,thejobisdeletedfromthequeue.
Ifajobshouldbeexecutedperiodicallyatasetinterval,useadateexpressionsimilarto'SYSDATE+7'intheINTERVAL
Ifyoualwayswanttoautomaticallyexecuteajobataspecifictime,regardlessofthelastexecution(forexample,everyMonday),theINTERVALandNEXT_DATEparametersshouldspecifyadateexpressionsimilarto'NEXT_DAY(TRUNC(SYSDATE),"MONDAY")'.
[b]Table8-5CommonJobExecutionIntervals[/b]
DateExpression | Evaluation |
---|---|
'SYSDATE+7' | exactlysevendaysfromthelastexecution 最后一次执行的7天之后执行 |
'SYSDATE+1/48' | everyhalfhour 每半个小时执行一次 |
'NEXT_DAY(TRUNC(SYSDATE), | everyMondayat3PM 每个礼拜一的下午3点执行 |
'NEXT_DAY(ADD_MONTHS(TRUNC(SYSDATE,''Q''),3), | firstThursdayofeachquarter每个季度的第一个星期四 |
Note: WhenspecifyingNEXT_DATEorINTERVAL,rememberthatdateliteralsandstringsmustbeenclosedinsinglequotationmarks.Also,thevalueofINTERVALmustbeenclosedinsinglequotationmarks. |
RemovingaJobfromtheJobQueue删除任务队列中的任务
Toremoveajobfromthejobqueue,usetheREMOVEprocedureintheDBMS_JOBpackage:DBMS_JOB.REMOVE(jobINBINARY_INTEGER)
Thefollowingstatementremovesjobnumber14144fromthejobqueue:
DBMS_JOB.REMOVE(14144);
SyntaxforWHAT
YoucanalterthedefinitionofajobbycallingtheDBMS_JOB.WHATprocedure.DBMS_JOB.WHAT(jobINBINARY_INTEGER,
whatINVARCHAR2)
Note: WhenyouexecuteprocedureWHAT,Oraclerecordsyourcurrentenvironment.Thisbecomesthenewenvironmentforthejob. |
SyntaxforNEXT_DATE
YoucanalterthenextdatethatOracleexecutesajobbycallingtheDBMS_JOB.NEXT_DATEprocedure.DBMS_JOB.NEXT_DATE(jobINBINARY_INTEGER,
next_dateINDATE)
SyntaxforINTERVAL
YoucanaltertheexecutionintervalofajobbycallingtheDBMS_JOB.INTERVALprocedure.DBMS_JOB.INTERVAL(jobINBINARY_INTEGER,
intervalINVARCHAR2)
ViewingJobQueueInformation查询任务队列的信息
可以通过下表中的数据字典视图来查询任务队列中的任务的信息。
[align=center]Table8-6任务队列信息的视图[/align]
View | Description |
---|---|
DBA_JOBS | Listsallthejobsinthedatabase.列出数据库中的任务 |
USER_JOBS | Listsalljobsownedbytheuser.列出该用户拥有的任务 |
DBA_JOBS_RUNNING | Listsalljobsinthedatabasethatarecurrentlyrunning.ThisviewjoinsV$LOCKandJOB$.列出当前运行的任务。 |
Forexample,youcandisplayinformationaboutajob'sstatusandfailedexecutions.Thefollowingsamplequerycreatesalistingofthejobnumber,nextexecutiontime,failures,andbrokenstatusforeachjobyouhavesubmitted:
显示任务的状态和失败执行的次数。
SELECTjob,next_date,next_sec,failures,broken
FROMuser_jobs;
JOBNEXT_DATENEXT_SECFAILURESB
--------------------------------------
912501-NOV-9400:00:004N
1414424-OCT-9416:35:350N
4176201-JAN-0000:00:0016Y
3rowsselected.
Youcanalsodisplayinformationaboutjobscurrentlyrunning.Thefollowingsamplequeryliststhesessionidentifier,jobnumber,userwhosubmittedthejob,andthestarttimesforallcurrentlyrunningjobs:
显示当前运行的任务的信息。
SELECTsid,r.job,log_user,r.this_date,r.this_sec
FROMdba_jobs_runningr,dba_jobsj
WHEREr.job=j.job;
SIDJOBLOG_USERTHIS_DATETHIS_SEC
---------------------------------------------------------
1214144JFEE24-OCT-9417:21:24
258536SCOTT24-OCT-9416:45:12
2rowsselected.
转载者新增:
实际开发中可能会遇到要一个月的某个时间定期执行脚本的需求,可以使用
Add_months(to_date(sysdate,''yyyymmdd''),1)来实现,这个脚本就
会一个月执行一次。
例如:
VARIABLEjobnonumber;beginDBMS_JOB.SUBMIT(:jobno,'p_procedure;',to_date(sysdate,'yyyymmdd'),'Add_months(to_date('sysdate',''yyyymmdd''),1)');commit;end;printjobno;
parameter.Forexample,ifyousettheexecutionintervalto'SYSDATE+7'onMonday,butforsomereason(suchasanetworkfailure)thejobisnot
executeduntilThursday,'SYSDATE+7'thenexecuteseveryThursday,notMonday.
相关文章推荐
- 用PHP实现守护进程任务后台运行与多线程(php-resque使用说明)
- 用PHP实现守护进程任务后台运行与多线程(php-resque使用说明)
- 用PHP实现守护进程任务后台运行与多线程(php-resque使用说明)
- 用PHP实现守护进程任务后台运行与多线程(php-resque使用说明)
- android如何使用BroadcastReceiver后台实现来电通话记录的监听并存取到sqllite数据库通过Contentprovilder实现接口
- 用PHP实现守护进程任务后台运行与多线程(php-resque使用说明)
- 使用 NodeJs 实现一个本地接口数据系统,无需数据库,让前端独立于后台开发
- 用PHP实现守护进程任务后台运行与多线程(php-resque使用说明)
- 用PHP实现守护进程任务后台运行与多线程(php-resque使用说明)
- 用PHP实现守护进程任务后台运行与多线程(php-resque使用说明)
- 用PHP实现守护进程任务后台运行与多线程(php-resque使用说明)
- linux下使用系统调用实现进程后台运行
- python2.0_day19_充分使用Django_form实现前端操作后台数据库
- 使用基于ThinkPHP3.2.3的ThinkAdmin创建手机电脑通用的表白墙(九)后台实现:整体概述
- 使用共享内存实现一个进程写文件,两个进程读文件
- 使用imp/impdb和管道实现数据库的快速迁移
- 数据库(三)为Python shell添加新的功能和使用Flask-Migrate实现数据库迁移
- 无限分级Repeater递归实现:读取一次数据库,使用LINQ2SQL技术,支持排序&显示隐藏
- 不使用Django ORM对数据库进行修改 保证一个方法实现原子性,一致性
- 使用spring的动态路由实现数据库负载均衡