`
gelu2
  • 浏览: 7267 次
  • 性别: Icon_minigender_1
  • 来自: 上海
最近访客 更多访客>>
社区版块
存档分类
最新评论

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;

分享到:
评论

相关推荐

Global site tag (gtag.js) - Google Analytics