oracle定时器在项目中的应用

现在业务人员提出了一个需求:

在项目中的工作流,都要有一个流程编号,此编号有一定的规则:

前四五位是流程的字母缩写,中间是8位的日期,后面五位是流水码,要求流水码每天从00001开始。即:QJLC2018060800001

 

没有想到更好的方式,暂时考虑到了使用oracle的定时器来每天定时的将流水码重置为1。

创建任务编码表:

/*==============================================================*/

/* Table: t_flow_taskcode_conf                                  */

/*==============================================================*/

create table t_flow_taskcode_conf  (

   flowflag              varchar2(8),

   flowab                varchar2(10),

   flowcode             NUMBER(5)

);

comment on table t_flow_taskcode_conf is \’流程生成任务编号表\’;

comment on column t_flow_taskcode_conf.flowflag is \’流程标识\’;

comment on column t_flow_taskcode_conf.flowab is \’流程四位缩写\’;

comment on column t_flow_taskcode_conf.flowcode is \’流水码\’;

 

insert into t_flow_taskcode_conf (FLOWFLAG, FLOWAB, FLOWCODE) values (\’QJLC\’, \’QJLC\’, 1);

insert into t_flow_taskcode_conf (FLOWFLAG, FLOWAB, FLOWCODE) values (\’BGYP\’, \’BGYP\’, 1);

insert into t_flow_taskcode_conf (FLOWFLAG, FLOWAB, FLOWCODE) values (\’DJBX\’, \’DJBX\’, 1);

commit;

 创建oracle内部的定时器:

create or replace procedure taskcode_procedure is

begin

  update t_flow_taskcode_conf fc set fc.flowcode = 1;

  commit;

end;

 

–定义taskcode每天自动初始化的job任务

declare taskcodejob number;

begin

  dbms_job.submit(

        taskcodejob,  –定时器ID,系统自动获得

        \’taskcode_procedure;\’, –what 执行的存储过程名

        sysdate,  –定时器开始执行的时间,这样写表示立即执行 –next_date,可以不填

        \’TRUNC(sysdate)+1\’–\’Interval时间字符串\’ –关键设置,此处表示每天的0点执行

  );

commit;

end;

  

#########下面是一些oracle中的job表和内置定时器函数的介绍:

—  select * from user_jobs;  –查看调度任务

—  select * from dba_jobs_running;–查看正在执行的调度任务

—  select * from dba_jobs;–查看执行完的调度任务

 

—-更新一个job的sql代码

declare

taskcodejob number;

begin

      dbms_job.run(3);        –运行jobid为3的定时器

      –dbms_job.remove(10);   –9是从user_jobs这个表中查询到然后手动赋值到这里的

      –dbms_job.broken(8);    –停止一个job

      –dbms_job.interval(84,\’TRUNC(sysdate)+15/1440\’);–更改定时器的运行频率

commit;

end; 

 

Java代码:

  1. /**
  2.  * 传入流程的标志,返回流程的任务编码
  3.  * @param taskCodeEnum
  4.  * @return
  5.  */
  6. public String getAndSetTaskCode(FlowTaskCodeEnum taskCodeEnum){
  7.    String flowflag = taskCodeEnum.toString();  //flowflag是”BGYP”,”QJLC”等字符串
  8.    //先获取流程的编码
      
    String taskcode = publicCollectDao.getTaskCodeByFlow(flowflag);
  9.    Map<String,Object> map = new HashMap<>();
  10.    map.put("flowflag",flowflag);
  11.    //设置流程的编码加1
  12.    publicCollectDao.updateFlowCode(map);
  13.    return taskcode;
  14. }

Mybatis的xml文件:

<select id=”getTaskCodeByFlow” parameterType=”string” resultType=”string”>

  select fc.flowab||to_char(sysdate,\’yyyyMMdd\’)||lpad(fc.flowcode,5,\’0\’) taskcode

  from t_flow_taskcode_conf fc 

  where fc.flowflag = #{flowflag}

</select>

<update id=”updateFlowCode” parameterType=”map”>

  update t_flow_taskcode_conf set flowcode = flowcode+1 where flowflag=#{flowflag}

</update>

 

上面的java代码,要保证getAndSetTaskCode()方法在使用时开启了事务。

 

参考:

https://www.cnblogs.com/mingforyou/archive/2012/06/06/2538063.html

https://blog.csdn.net/anrry258/article/details/26555693

注意区分是普通的sql窗口还是commond窗口。

 

版权声明:本文为yjk295722366原创文章,遵循 CC 4.0 BY-SA 版权协议,转载请附上原文出处链接和本声明。
本文链接:https://www.cnblogs.com/yjk295722366/p/9155112.html