假设我们有以下代码:
try {
$message = new CreateSomething($data);
$this->messageBus->dispatch($message);
} catch (RuntimeException $exception) {
$failNotification = new CreateFailedNotification($data);
$this->eventBus->dispatch($failNotification);
throw $exception;
}
两辆公共汽车都设置了 DoctrineTransactionMiddleware。让我们假设在处理过程中messageBus
我们坚持一些实体但发生异常 -DoctrineTransactionMiddleware
在这种情况下提供回滚事务,所以稍后我们进入上述代码中的 catch 块。一切都处理得很好,因此在eventBus
中创建的事务已eventBus
成功提交,但 entityManager 也有在此期间持久存在的实体messageBus
- 所以最后这些实体也被保存到数据库中,这是不需要的行为。
我在学说文档中读过
当使用显式事务划分并发生异常时,应立即回滚事务并通过调用 EntityManager#close() 关闭 EntityManager
DoctrineTransactionMiddleware 没有这样做——我必须在执行调度close()
之前在 catch 块中显式执行方法——然后我从. 也许我理解错了这个想法,它就像那个 OR DoctrineMiddleware 错过了执行 close() 方法。IMO DoctrineTransactionMiddleware 应该在没有开发人员干预的情况下提供这样的执行。eventBus
messageBus