I am trying to figure out a way to detect rollback in a MySQL stored procedure so that I can handle the situation due to a PHP script, but so far I have not been able to find any solution.
My stored procedure is as follows:
delimiter |
create procedure multi_inserts(
IN var1 int(11),
.
.
.
IN string1 text
)
BEGIN
declare exit handler for sqlexception rollback;
declare exit handler for sqlwarning rollback;
START TRANSACTION;
insert into table1(a,b,c,d) values(var1,var2,var3,var4);
insert into table2(e,f,g) values(var5,var6,string1);
COMMIT;
END
delimiter ;
I checked the rollback test using this procedure and it rolled back, but I did not receive any false data. I want my stored procedure to give some kind of error message if the transaction failed, so I could handle it like this:
$result = mysql_query($procedure);
if(!$result)
{
//rollback occured do something
}
Is there a way to detect rollback in MySQL? Am I missing something? Any answer would be appreciated. Thanks for reading.
Thanks to your advice, I fixed this problem. Here is what I did:
Stored procedure
delimiter |
create procedure multi_inserts(
IN var1 int(11),
.
.
.
IN string1 text
)
BEGIN
declare exit handler for sqlexception sqlwarning
BEGIN
rollback;
select -1;
END;
START TRANSACTION;
insert into table1(a,b,c,d) values(var1,var2,var3,var4);
insert into table2(e,f,g) values(var5,var6,string1);
COMMIT;
END
delimiter ;
If I use a variable instead of select -1, it gives me this error:
ArgumentOUT INOUT NEW-
, , .
PHP
$result=mysqli_query($con,$procedure);
if(is_object($result))
{
//rollback happened do something!
}
SP , true.