From 009d870d520f30a213a1db0e410f8edf674eda5a Mon Sep 17 00:00:00 2001 From: Indra Gunawan Date: Wed, 24 Jul 2024 19:24:44 +0800 Subject: [PATCH] [DoctrineBridge] fix messenger bus dispatch inside an active transaction --- Messenger/DoctrineOpenTransactionLoggerMiddleware.php | 3 ++- .../DoctrineOpenTransactionLoggerMiddlewareTest.php | 6 +++--- 2 files changed, 5 insertions(+), 4 deletions(-) diff --git a/Messenger/DoctrineOpenTransactionLoggerMiddleware.php b/Messenger/DoctrineOpenTransactionLoggerMiddleware.php index 2ef3bbbb..1efbdd47 100644 --- a/Messenger/DoctrineOpenTransactionLoggerMiddleware.php +++ b/Messenger/DoctrineOpenTransactionLoggerMiddleware.php @@ -43,11 +43,12 @@ protected function handleForManager(EntityManagerInterface $entityManager, Envel } $this->isHandling = true; + $initialTransactionLevel = $entityManager->getConnection()->getTransactionNestingLevel(); try { return $stack->next()->handle($envelope, $stack); } finally { - if ($entityManager->getConnection()->isTransactionActive()) { + if ($entityManager->getConnection()->getTransactionNestingLevel() > $initialTransactionLevel) { $this->logger->error('A handler opened a transaction but did not close it.', [ 'message' => $envelope->getMessage(), ]); diff --git a/Tests/Messenger/DoctrineOpenTransactionLoggerMiddlewareTest.php b/Tests/Messenger/DoctrineOpenTransactionLoggerMiddlewareTest.php index 3682ad00..a1d4118d 100644 --- a/Tests/Messenger/DoctrineOpenTransactionLoggerMiddlewareTest.php +++ b/Tests/Messenger/DoctrineOpenTransactionLoggerMiddlewareTest.php @@ -50,9 +50,9 @@ public function log($level, $message, $context = []): void public function testMiddlewareWrapsInTransactionAndFlushes() { - $this->connection->expects($this->exactly(1)) - ->method('isTransactionActive') - ->willReturn(true, true, false) + $this->connection->expects($this->exactly(2)) + ->method('getTransactionNestingLevel') + ->willReturn(0, 1) ; $this->middleware->handle(new Envelope(new \stdClass()), $this->getStackMock());