ITeye博客

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

gelu2 · 2012-12-13 12:54 · 数据库 · Markdown

--取顺序流水号(使用了自治事务及悲观锁) 流水号记录表: 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;

评论(0)

暂无评论。

评论功能维护中,当前仅展示历史评论。