dongtuo7364 2017-03-25 21:00
浏览 91

Laravel 5.3 - Laravel数据库事务和通知在失败的事务上发送电子邮件?

Using the below transaction, I fire an event, which runs the two listeners listed below the event. The issue is when the first event AddQuestionToQuestionsTable fails for any reason, the DB data gets rolled back correctly because of DB::rollback, but the laravel notification email I have setup and fired in the second listener QuestionAddedNotificationSend gets sent out regardless of the error, so whether there was an error or not.

If there was an error in the transaction, we would not want to send the email. Note: I may add additional listeners that also insert into the DB, so I would need to know how to only fire off the emails when the transaction is successful.

DB::beginTransaction();

try {
    event(new LaravelQuestionPosted($question, $user));
    // Listeners: AddQuestionToQuestionsTable
    // Listeners: QuestionAddedNotificationSend
} catch (\Exception $e) {
    DB::rollback();
    // something went wrong
}

DB::commit();

Anyone know how to make it work as intended?

  • 写回答

1条回答 默认 最新

  • du42561 2017-03-25 22:39
    关注

    You can pass true as the third parameter when dispatching the event with event() to have the listen handling halted when any of the handlers returns a non-null value or throws an exceptions.

    评论

报告相同问题?

悬赏问题

  • ¥20 有偿 写代码 要用特定的软件anaconda 里的jvpyter 用python3写
  • ¥20 cad图纸,chx-3六轴码垛机器人
  • ¥15 移动摄像头专网需要解vlan
  • ¥20 access多表提取相同字段数据并合并
  • ¥20 基于MSP430f5529的MPU6050驱动,求出欧拉角
  • ¥20 Java-Oj-桌布的计算
  • ¥15 powerbuilder中的datawindow数据整合到新的DataWindow
  • ¥20 有人知道这种图怎么画吗?
  • ¥15 pyqt6如何引用qrc文件加载里面的的资源
  • ¥15 安卓JNI项目使用lua上的问题