处理“交易”;以编程方式
我有一个进程创建两个数据库表条目,并向authorize.net api 发送两个请求。
现在,如果在该过程中的任何时候程序因任何原因出错,我想“回滚”。在我们的系统中创建的任何条目都应被删除,并且应通知用户出现错误,并应给予重试的选项。
流程的简化版本如下所示:
Create Account Entry ->创建用户条目 ->向 Authorize.net 发送一次性付款请求 ->保存来自 Authorize.net 的一次性付款请求的结果 ->向 Authorize.net 发送定期付款请求 ->保存来自 Authorize.net 的定期付款请求的结果
处理这种情况的最佳方法是什么?
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?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
您只需要使用 InnoDB 表并然后您可以使用标准事务处理语句。 (即:您将使用
begin
,尝试执行必要的处理,然后相应地发出commit
或rollback
。)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 acommit
or arollback
accordingly.)我会使用常规 InnoDB 事务来处理数据库部分。不过,回滚 Authorize.NET 调用会比较棘手,并且只需要一些非常仔细的异常处理。
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.
您可以将查询包装在事务中,请注意要回滚的表需要将其引擎设置为 innodb。下面是代码的示例:
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: