场景:
利用oracle的存储过程实现在sys_sequence表中的多个的自动增长ID的多线程同步解决方案
利用oracle的存储过程实现sys_sequence表的自动增长ID的多线程同步解决方案
sys_sequence表结构
sys_sequence表中的记录
DECLARE
no NUMBER(10);
key VARCHAR(50);
begin
key := ?;
begin select lastid into no from sys_sequence where code = key for update;
//首先从sys_sequence找到相应表的lastid值,并使用for update后缀来锁住该记录。
exception when no_data_found then no := 1;
//截获no_data_found错误,当sys_sequence表中无记录时,lastid默认为1。
end;
if no>1 then
update sys_sequence set lastid=no+1 where code=key;
//当lastid大于1即表中存在记录时,使用update语