一个包中有多个存储过程,每个存储过程有多个DML语句,存储过程之间有逻辑关系,
请教一下,什么时候执行commit操作最好?是每个DML语句后执行一次,还是每个存储过程执行一次,还是整个包执行一次?
          另:如果每个DML语句后执行一次,那么如果后面的DML语句出现错误,如何恢复数据?
              如果一个存储过程或一个包执行一次commit,这样占用系统资源太多请高手帮忙分析一下,谢谢

解决方案 »

  1.   


    --当你执行update table或者insert into table后,就立即commit;
    --在过程里面设置exception,也就是异常处理,如果操作失败,就rollback;
      

  2.   

    我用的是单次提交,但是这样出过问题,就是前面的操作已经提交了,但是在执行后面的操作时报错了,导致数据无法通过rollback恢复,但是如果批量提交的话,占用系统资源太多,所以想请教一下,分享各位在开发中的宝贵经验
      

  3.   


    如果你执行update table或者insert into table后,就立即commit ,如果后面再执行update table或者insert into table时报错,rollback是恢复不了前面已经commit的数据
      

  4.   


    --看看你写的procedure,应该要有严谨的异常处理
    create table goods(
           goods_id varchar2(10),
           goods_name varchar2(20),
           goods_num number(10),
           date_in date)
    --
    SQL> alter table goods
      2  add constraint pk_goods primary key(goods_id);
      --
    insert into goods
    select '10010','佳洁士',100,to_date('2011-01-01','yyyy-mm-dd') from dual union all
    select '10012','safeguard',500,to_date('2011-02-23','yyyy-mm-dd') from dual union all
    select '10015','美国大杏仁',5000,to_date('2011-04-12','yyyy-mm-dd') from dual;
    --
    create or replace procedure pro_update(
           g_id_in goods.goods_id%type,
           g_name_in goods.goods_name%type,
           g_num_in goods.goods_num%type,
           g_dt_in goods.date_in%type)
    as
           v_sign number;
    begin
         select count(*) into v_sign
         from goods
         where goods_id=g_id_in;
         if v_sign=0 then
            insert into goods(goods_id,goods_name,goods_num,date_in)
            values(g_id_in,g_name_in,g_num_in,g_dt_in);
            commit;
            dbms_output.put_line('insert success');
         end if;
         exception
         when others then
              dbms_output.put_line('some errors');
              rollback; 
    end pro_update;
    --
    SQL> exec pro_update('10013','wahaha',500,to_date('2011-02-25','yyyy-mm-dd'));insert successPL/SQL procedure successfully completed
    SQL> select * from goods;GOODS_ID   GOODS_NAME             GOODS_NUM DATE_IN
    ---------- -------------------- ----------- -----------
    10010      佳洁士                       100 2011-1-1
    10012      safeguard                    500 2011-2-23
    10015      美国大杏仁                  5000 2011-4-12
    10013      wahaha                       500 2011-2-25