Oracle取顺序流水号(解决了并发的问题)

--取顺序流水号(使用了自治事务及悲观锁)
流水号记录表: Tcount表(X int,item varchar(20)) 主键:x+item做为联合主键

procedure getX(p_item varchar2,cu out number)
  is
  begin
         cu:=0;  
         autonomous_insert(p_item);

         select x+1 into cu from tcount where item=p_item for update;
         update tcount set x=x+1 where item=p_item;
         --commit;
  end;
  
  
  procedure autonomous_insert(p_item varchar2)
  is 
  --使用自治事务
  pragma autonomous_transaction;
  begin
       insert into tcount 
       select 0,p_item  from dual where not exists(select 1 from tcount t where t.item=p_item) ;

       commit;
  exception when others then 
       rollback;
  end;

你可能感兴趣的:(oracle)