有时候需要一个存储过程操作是互斥的 怎么搞?
将数据库表类型转换成 InnoDB ,然后存储过程的前面加上SET AUTOCOMMIT=0;
start transaction;
表示开始事务
存储过程的最后加上 COMMIT 提交事务
另外第一个查询语句一定要加上 for update 来锁定数据库
一个存储过程的例子:
BEGIN
declare v_phone char(32);
declare v_date datetime;
SET AUTOCOMMIT=0;
start transaction;
select * into v_phone,v_date from task_run limit 0,1 for update;
if length(v_phone) is null then
select '' as 'phone';
else
select v_phone as 'phone';
delete from task_run where phone = v_phone;
end if;
commit;
END