How to detect a rollback in MySQL stored procedure?

I'm trying to figure out a way to detect an occurrence of rollback in a MySQL stored procedure so I could handle the situation accordingly from a PHP script, but so far I can not find any solution.mysql

My stored procedure looks like this:web

    delimiter |     create procedure multi_inserts(     IN var1 int(11),          .          .          .     IN string1 textsql

      )BEGINapp

      declare exithandler forsqlexception rollback;     declare exithandler forsqlwarning rollback;ide

      START TRANSACTION;     insert intotable1(a,b,c,d)values(var1,var2,var3,var4);     insert intotable2(e,f,g)values(var5,var6,string1);     COMMIT;this

END     delimiter ;spa

I did a rollback test on this procedure and it did rollback but I got no false. I want my stored procedure to throw some kind of error message if the transaction failed, so I could handle it like this:code

   $result =mysql_query($procedure);orm

if(!$result)ip

    {//rollback occured do something      }

Is there a way to detect rollback in MySQL? Am I missing something? Any reply will be appreciated. Thanks for reading.

 

You can add an output param and then set it to the value you want in your exit handlers.

Here's an example using your proc:

delimiter $$

  create procedure multi_inserts( IN var1 int(11),      .      .      . IN string1 text, OUT p_return_code tinyint unsigned )BEGIN

  DECLARE exithandler forsqlexception

BEGIN   --ERROR

setp_return_code =1;   rollback;END;

  DECLARE exithandler forsqlwarning

BEGIN   --WARNING

setp_return_code =2;   rollback;END;

  START TRANSACTION; insert intotable1(a,b,c,d)values(var1,var2,var3,var4); insert intotable2(e,f,g)values(var5,var6,string1); COMMIT;

  --SUCCESS

setp_return_code =0;

END$$

  delimiter ;

相關文章
相關標籤/搜索