开发者

Handling a 'transaction' programmatically

I have a process that creates two database table entries, and sends two requests to the authorize.net api.

Now, if at any point in the process the program errors out for any reason, I'd like to 'rollback'. Any entry created in our system should be removed and the user should be notified that there was an error and should be given the开发者_如何转开发 option to retry.

A watered down version of the flow looks like this:

Create Account Entry -> Create User Entry -> Send one time payment request to Authorize.net -> Save result of one time payment request from Authorize.net -> Send recurring payment request to Authorize.net -> Save result of recurring payment request from Authorize.net

What's the best way to handle a situation like this?


You simply need to use an InnoDB table and you can then use the standard transaction processing statements. (i.e.: You'd use a begin, attempt to carry out the necessary processing and then either issue a commit or a rollback accordingly.)


I would use regular InnoDB transactions to handle the database part. Rolling back the Authorize.NET calls would be trickier though and would just require some very careful exception handling.


You can wrap you queries in transaction please note the tables you want to rollback need to have their engine set to innodb. Heres an example of how the code might look:

//Wrap your queries in a transaction

    mysql_query("START TRANSACTION"); 

        try{ 
            //your stuff - if your stuff is successful commit, in the event of an error rollback

            if($success){
                mysql_query("COMMIT"); 
            } 
            else{ 
                mysql_query("ROLLBACK"); 
            } 
        }catch(Exception $e){ 
            //if something goes seriously wrong
            mysql_query("ROLLBACK"); 
        }
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜