2

我有一个从并发事务调用的过程:

//Some actions here

INSERT INTO table1
                (table1_id, table1_val1, table1_val2, table1_val3)
              VALUES
                (gettablenewid('TABLE1'), val1, val2, val3);

INSERT INTO table1
                (table1_id, table1_val1, table1_val2, table1_val3)
              VALUES
                (gettablenewid('TABLE1'), val1, val2, val3);
INSERT INTO table1
                (table1_id, table1_val1, table1_val2, table1_val3)
              VALUES
                (gettablenewid('TABLE1'), val1, val2, val3);
//some other actions

函数gettablenewid代码(id_table存储每个表的PK):

create or replace
    function        GetTableNewId(tablename in varchar2)
      return number is
      PRAGMA AUTONOMOUS_TRANSACTION;
      Result number;
    cursor c1 is SELECT ig.id_value+1 id_new
                              FROM id_table ig
                              WHERE ig.table_identifier = tablename
                              FOR UPDATE of  ig.id_value;
    begin
    for c1_rec in c1 loop  
      UPDATE id_table ig
         SET ig.id_value = c1_rec.id_new
       WHERE current of c1 ;
       Result:=c1_rec.id_new;
    end loop;
    commit;
      return(Result);
    end GetTableNewId;

有时插入语句会因 table1_id 的 ORA-00001 而失败,我无法理解为什么会发生这种情况。

4

1 回答 1

0

因此,正如 Scott Lamb 所说,代码中没有错误。我发现另一个进程同时为 table_identifier = 'TABLE1' 修改了 id_table。感谢大家的帮助。

于 2013-03-14T06:03:13.503 回答