diff --git a/Messenger/DoctrineOpenTransactionLoggerMiddleware.php b/Messenger/DoctrineOpenTransactionLoggerMiddleware.php index 75187a1e..76380154 100644 --- a/Messenger/DoctrineOpenTransactionLoggerMiddleware.php +++ b/Messenger/DoctrineOpenTransactionLoggerMiddleware.php @@ -41,11 +41,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 8d49caa3..56a5a664 100644 --- a/Tests/Messenger/DoctrineOpenTransactionLoggerMiddlewareTest.php +++ b/Tests/Messenger/DoctrineOpenTransactionLoggerMiddlewareTest.php @@ -51,9 +51,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());