mysql 存储过程 致命问题,求救!~
存储过程生成ID遇到重复数据,数据库已经设置该字段为唯一约束,并发执行后出现错误,产生了相同的ID。
存储过程如下:(在线等)
begin
DECLARE atomicTaskID bigint default 0;
DECLARE max_atomicTaskID bigint default 0;
select concat(taskid,"002001") into atomicTaskID;
select max(SMSTask_ID)into max_atomicTaskID from smstask where SMSTask_TaskID = taskid;
if(max_atomicTaskID is null)then
insert into smstask values( '0',atomicTaskID,'0',taskid,'0','0','0','0','0','0','0','0','0','0','0','0','0','0','0','0','0','0','0','0','0');
return atomicTaskID;
else
if( max_atomicTaskID%1000 < 999 )then
if(max_atomicTaskID >= atomicTaskID)then
select max_atomicTaskID + 1 into atomicTaskID;
end if;
insert into smstask values( '0',atomicTaskID,'0',taskid,'0','0','0','0','0','0','0','0','0','0','0','0','0','0','0','0','0','0','0','0','0');
return atomicTaskID;
else
return -1;
end if;
end if;
end
------解决方案--------------------
select max(SMSTask_ID)into max_atomicTaskID from smstask where SMSTask_TaskID = taskid;
的时候,应该把表锁住,以防止其它并发用户同时也在执行这个语句,
------解决方案--------------------额,为什么主键不用auto_increment