mysql rollback 两次_mysql rollback and throw error

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.

My stored procedure looks like this:

delimiter|create procedure multi_inserts(IN var1int(11),...IN string1 text)BEGINdeclareexithandlerforsqlexception rollback;declareexithandlerforsqlwarning rollback;START TRANSACTION;insertintotable1(a,b,c,d)values(var1,var2,var3,var4);insertintotable2(e,f,g)values(var5,var6,string1);COMMIT;ENDdelimiter;

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:

$result=mysql_query($procedure);if(!$result){//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.

Thanks to your advices I fixed this problem. Here's what I did:

Stored Procedure

delimiter|create procedure multi_inserts(IN var1int(11),...IN string1 text)BEGINdeclareexithandlerforsqlexception sqlwarningBEGINrollback;select-1;END;START TRANSACTION;insertintotable1(a,b,c,d)values(var1,var2,var3,var4);insertintotable2(e,f,g)values(var5,var6,string1);COMMIT;ENDdelimiter;

If I use out variable instead of select -1, it gives me this error:

OUT or INOUT argument is not a variable or NEW pseudo-variable in BEFORE trigger

I don't know what did I wrong, but I couldn't fix this problem.

PHP script

$result=mysqli_query($con,$procedure);if(is_object($result)){//rollback happened do something!}

If the SP is successful it throws true.

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 var1int(11),...IN string1 text,OUT p_return_code tinyintunsigned)BEGINDECLAREexithandlerforsqlexceptionBEGIN--ERRORsetp_return_code=1;rollback;END;DECLAREexithandlerforsqlwarningBEGIN--WARNINGsetp_return_code=2;rollback;END;START TRANSACTION;insertintotable1(a,b,c,d)values(var1,var2,var3,var4);insertintotable2(e,f,g)values(var5,var6,string1);COMMIT;--SUCCESSsetp_return_code=0;END$$

delimiter;

answered Nov 2 '10 at 19:04

07e7dadf0ea80d91dcb3663ce62312e9?s=32&d=identicon&r=PG

Thanks for the detailed answer. I rewrote my SP and it worked fine in MySQL console but php's mysql_query couldn't handle the result set and gave me funny error, so I updated PHP to 5.3.3 for mysqli_query. I thought this would solve it. How naive of me. After the update I got another error in Pear Auth.php which I'm using as an authentication and I cant see if mysqli_query really would solve the problem unless I solve the new problem first. The latest problem is that I cant update pear auth for some reason. I guess pear auth is incompatible with 5.3.3. Now what should I do? sorry I'm ranting.. – Kou Nov 2 '10 at 21:53

I don't use PHP. Sorry. – Ike Walker Nov 2 '10 at 22:45

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值