关于MYSQL 死锁的问题 Deadlock found when trying to get lock; try restarting

定时执行的MYSQL 存储过程报错:
Deadlock found when trying to get lock; try restarting transaction

这个错误是在MYSQL 的错误日志中看到的, 不知道要从哪里开始入手解决这个问题。 暂时没发现这个错误对业务逻辑有什么影响,求高手指点。 贴出过程代码如下:

 


  1. DELIMITER $$

  2. USE `sms`$$

  3. DROP PROCEDURE IF EXISTS `proc_passout`$$

  4. CREATE DEFINER=`root`@`%` PROCEDURE `proc_passout`()

  5. BEGIN

  6. DECLARE flag_lt INT(4);

  7. DECLARE flag_dx INT(4);

  8. DECLARE flag_yd INT(4);

  9. DECLARE flag INT(4) DEFAULT 0;

  10. DECLARE allflag INT(4);

  11. DECLARE cont_flag INT(4);

  12. DECLARE cont_deliverflag INT;

  13. DECLARE i_oid VARCHAR(20) DEFAULT 0;

  14. DECLARE tmpName VARCHAR(20) DEFAULT '' ;

  15. DECLARE selectmoid CURSOR FOR

  16. SELECT oid FROM cont WHERE DELIVERFLAG=5 ORDER BY oid ASC LIMIT 100;

  17. DECLARE CONTINUE HANDLER FOR SQLSTATE '02000' SET tmpName = NULL;

  18. /*=========================*/

  19. SET cont_deliverflag=9;

  20. OPEN selectmoid;

  21. FETCH selectmoid INTO i_oid;

  22. /*处理黑名单*/

  23. SET autocommit=0;

  24. UPDATE mt a,blacklist b SET a.deliverflag=1,a.delivertime=LEFT(NOW()+0,14),a.msgid='blacklist'

  25. WHERE a.phone=b.phone AND a.moid=i_oid;

  26. COMMIT;

  27. /*=========================*/

  28. WHILE ( tmpName IS NOT NULL) DO

  29. /*===========联通========*/

  30. SELECT COUNT(*) INTO flag_lt FROM mt WHERE moid=i_oid

  31. AND SUBSTR(phone,1,3) IN(SELECT subphone FROM telcom WHERE TYPE IN(2));

  32. /*==========电信==========*/

  33. SELECT COUNT(*) INTO flag_dx FROM mt WHERE moid=i_oid

  34. AND SUBSTR(phone,1,3) IN(SELECT subphone FROM telcom WHERE TYPE IN(3));

  35. /*============总数=========*/

  36. SELECT COUNT(*) INTO allflag FROM mt WHERE moid=i_oid;

  37. IF flag_lt=allflag THEN #全是联通号

  38. UPDATE cont SET channelid=1,deliverflag=cont_deliverflag WHERE oid=i_oid;

  39. SET flag =1;

  40. ELSEIF flag_dx=allflag THEN #全是电信号

  41. UPDATE cont SET channelid=10,deliverflag=cont_deliverflag WHERE oid=i_oid ;

  42. SET flag=1;

  43. ELSE

  44. /*处理联通号码*/

  45. IF flag_lt>0 AND flag=0 THEN

  46. SET @moid=seq('cont');

  47. SET autocommit=0;

  48. UPDATE mt SET moid=@moid WHERE moid=i_oid

  49. AND SUBSTR(phone,1,3)

  50. IN(SELECT subphone FROM telcom WHERE TYPE IN(2));

  51. INSERT INTO cont(putintime,Oid, MsgType, MsgCont, ChannelId, ChName, ChPassword, SpNumber,DeliverFlag,PRIORITY)

  52. SELECT PUTINTIME,@moid,MSGTYPE,MSGCONT,1,CHNAME,CHPASSWORD,SPNUMBER,cont_deliverflag,PRIORITY

  53. FROM cont WHERE oid=i_oid AND DELIVERFLAG=5;

  54. COMMIT;

  55. END IF;

  56. /*处理CDMA号码*/

  57. IF flag_dx>0 AND flag=0 THEN

  58. SET @moid=seq('cont');

  59. SET autocommit=0;

  60. UPDATE mt SET moid=@moid WHERE moid=i_oid

  61. AND SUBSTR(phone,1,3)

  62. IN(SELECT subphone FROM telcom WHERE TYPE IN(3));

  63. INSERT INTO cont(putintime,Oid, MsgType, MsgCont, ChannelId, ChName, ChPassword, SpNumber,DeliverFlag,PRIORITY)

  64. SELECT PUTINTIME,@moid,MSGTYPE,MSGCONT,10,CHNAME,CHPASSWORD,SPNUMBER,cont_deliverflag,PRIORITY

  65. FROM cont WHERE oid=i_oid AND DELIVERFLAG=5;

  66. COMMIT;

  67. END IF;

  68. END IF;

  69. SELECT COUNT(*) INTO flag_yd FROM mt WHERE moid=i_oid

  70. AND SUBSTR(phone,1,3) IN(SELECT subphone FROM telcom WHERE TYPE IN(2,3));

  71. /*处理移动号码*/

  72. IF flag_yd=0 THEN

  73. SET autocommit=0;

  74. UPDATE cont SET DELIVERFLAG=cont_deliverflag WHERE cont.OID=i_oid AND DELIVERFLAG=5;

  75. COMMIT;

  76. END IF;

  77. FETCH selectmoid INTO i_oid;

  78. END WHILE;

  79. CLOSE selectmoid;

  80. END$$

  81. DELIMITER ;

上一篇:E - Tokitsukaze and Duel CodeForces - 1190C (博弈 + 窗体移动)


下一篇:try catch finally中return