并发插入中违反了唯一约束(ORA-00001)

时间:2013-03-13 05:19:36

标签: concurrency oracle11g primary-key unique-constraint

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

//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插入语句失败,我无法理解为什么会发生这种情况。

1 个答案:

答案 0 :(得分:0)

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