dongxuxian6930 2015-01-16 14:48
浏览 152
已采纳

如何确保2个远程操作成功完成,如果其中一个远程操作失败,则回滚所有操作?

I plan to use PHP to connect to 2 remote servers. One is a MySQL database. The other one is a Job Queue server.

For every Job that I push to the Job Queue server, I want to ensure that (exactly) 1 record is inserted into the MySQL DB. I want to ensure that the MySQL DB records and the Job Queue tasks execution are 100% in sync.

Since both are remote servers, I am worried that sometimes there may be connection problem to one of the servers (or both).

In my PHP application, how can I ensure that 2 remote operations complete successfully, and rollback all operations if 1 of the remote operations fails? Is there any programming best practices that I should follow for this kind of scenario?

  • 写回答

1条回答 默认 最新

  • doulian7305 2015-01-16 15:01
    关注

    This sounds like a pretty basic AND use case. Since you're in PHP, you're not worried about asyncronous calls or timeout waits. To pseudocode it:

    Start Transaction1
        ... do stuff... //transaction 1 still open, no commit
    Start Transaction2
        ... do more stuff...
    If (Transaction1 success && Transaction2 success) {
        commit Transaction1
        commit Transaction2
    } else {
        rollback Transaction1
        rollback Transaction2
    }
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 孟德尔随机化结果不一致
  • ¥15 深度学习残差模块模型
  • ¥50 怎么判断同步时序逻辑电路和异步时序逻辑电路
  • ¥15 差动电流二次谐波的含量Matlab计算
  • ¥15 Can/caned 总线错误问题,错误显示控制器要发1,结果总线检测到0
  • ¥15 C#如何调用串口数据
  • ¥15 MATLAB与单片机串口通信
  • ¥15 L76k模块的GPS的使用
  • ¥15 请帮我看一看数电项目如何设计
  • ¥23 (标签-bug|关键词-密码错误加密)