• 工作笔记系列-关于存储过程


    create or replace procedure p_add_alarm_supervisor_history  //这是创建语句
    (superId in varchar2 ,operatorId in varchar2,superServerId in varchar2,res in varchar2)//参数
    is
    tablecount number;//声明的变量,   内部如果要用都要在这里声明
    begin
      select 1 into tablecount from dual for update nowait; //通过一种取巧的方式锁住该存储过程
      tablecount:=0;
      select count(*) into tablecount from gps_alarm_supervisor where supervison_id=superId;
       if(tablecount>0) then
           insert into gps_alarm_supervisor_history(
           warn_src,supervison_level,supervison_id,supervison_endtime,
           supervisor_tel,supervisor_email,supervison_time,supervisor,
           process_flag,process_user,process_time,process_result,vehicle_no,
           warn_type,platform_id,warn_time,vehicle_color,access_code
           )
            select
           warn_src,supervison_level,superId,supervison_endtime,supervisor_tel,
           supervisor_email,supervison_time,operatorId,process_flag,process_user,
           process_time,res,vehicle_no,warn_type,superServerId,warn_time,
           vehicle_color,access_code  from gps_alarm_supervisor
            where supervison_id=superId;
            delete from gps_alarm_supervisor where supervison_id=superId;
            Dbms_output.put_line('正确执行提交事务');
            commit;
       else
         Dbms_Output.put_line('没有信息 ');
         end if;
         exception when others
           then
             dbms_output.put_line('出现错误回滚事务');
             rollback;
    end;

    上面是写的存储过程,流程很简单:

      传入四个参数,首先这四个参数是在java那边进行过判断和控制,所以不可能为空。在这里就不用判断了。

      首先是使用select 1 from dual for update nowait进行锁住存储过程,这样就不会出现多线程访问该存储过程了。这里使用到了into一个值,是因为存储过程中无法单纯的使用select 语句,单纯的select没有意义  oracle禁止。  但它不知道我们通过取巧的方式...

      接下来是一个判断,如果表里存在该ID的数据  那么执行   不存在则走else 结束存储过程

      然后是一长段语句:查询数据并且插入另一张表。 成功执行 那么commit

      如果报错的话  最后的异常捕获会捕获到异常  那么就rollback 

      

      在这里有一些问题,我在这里锁住了dual表 ,存储过程结束后会自动释放吗? 

      如果有DBA大神看见是否能帮忙指点一下迷津?

  • 相关阅读:
    mybatis的分页插件pagehelper的使用
    SSM框架中添加事务
    junit测试,spring中使用
    maven的聚合和继承,maven的依赖
    shiro的角色和权限
    tomcat配置虚拟文件路径
    通过HttpServletRequest的request获取各种路径
    shiro学习一
    JDBC
    GUI
  • 原文地址:https://www.cnblogs.com/dingzb/p/3681091.html
Copyright © 2020-2023  润新知