Skip to content

Commit a123a45

Browse files
bug symfony#57816 [DoctrineBridge] fix messenger bus dispatch inside an active transaction (IndraGunawan)
This PR was squashed before being merged into the 5.4 branch. Discussion ---------- [DoctrineBridge] fix messenger bus dispatch inside an active transaction | Q | A | ------------- | --- | Branch? | 5.4 | Bug fix? | yes | New feature? | no <!-- please update src/**/CHANGELOG.md files --> | Deprecations? | no <!-- please update UPGRADE-*.md and src/**/CHANGELOG.md files --> | Issues | Fix #... <!-- prefix each issue number with "Fix #", no need to create an issue if none exists, explain below instead --> | License | MIT it was working fine with the below config and command. ```yaml # config/packages/messenger.yaml framework: messenger: buses: sync_bus: middleware: - validation - doctrine_ping_connection - doctrine_close_connection - doctrine_open_transaction_logger - doctrine_transaction routing: App\Message\SyncJob: sync ``` ```php <?php // src/Command/TestComand.php namespace App\Command; use App\Message\SyncJob; use Doctrine\ORM\EntityManagerInterface; // use ... #[AsCommand( name: 'app:test', description: 'Add a short description for your command', )] class TestCommand extends Command { public function __construct( private readonly EntityManagerInterface $em, private readonly MessageBusInterface $bus, ) { parent::__construct(); } protected function execute(InputInterface $input, OutputInterface $output): int { $io = new SymfonyStyle($input, $output); $this->bus->dispatch(new SyncJob('someone')); $io->success('You have a new command! Now make it your own! Pass --help to see your options.'); return Command::SUCCESS; } } ``` An issue occurs if I dispatch the message inside a transaction ```php $this->em->wrapInTransaction(function ($em) { // another process here $this->bus->dispatch(new SyncJob('someone')); }); ``` an error comes up ``` ERROR [app] A handler opened a transaction but did not close it. ``` This PR aims to fix this issue without changing the existing behaviour Commits ------- f164370 [DoctrineBridge] fix messenger bus dispatch inside an active transaction
2 parents 989e5f5 + f164370 commit a123a45

File tree

2 files changed

+5
-4
lines changed

2 files changed

+5
-4
lines changed

src/Symfony/Bridge/Doctrine/Messenger/DoctrineOpenTransactionLoggerMiddleware.php

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -43,11 +43,12 @@ protected function handleForManager(EntityManagerInterface $entityManager, Envel
4343
}
4444

4545
$this->isHandling = true;
46+
$initialTransactionLevel = $entityManager->getConnection()->getTransactionNestingLevel();
4647

4748
try {
4849
return $stack->next()->handle($envelope, $stack);
4950
} finally {
50-
if ($entityManager->getConnection()->isTransactionActive()) {
51+
if ($entityManager->getConnection()->getTransactionNestingLevel() > $initialTransactionLevel) {
5152
$this->logger->error('A handler opened a transaction but did not close it.', [
5253
'message' => $envelope->getMessage(),
5354
]);

src/Symfony/Bridge/Doctrine/Tests/Messenger/DoctrineOpenTransactionLoggerMiddlewareTest.php

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -50,9 +50,9 @@ public function log($level, $message, $context = []): void
5050

5151
public function testMiddlewareWrapsInTransactionAndFlushes()
5252
{
53-
$this->connection->expects($this->exactly(1))
54-
->method('isTransactionActive')
55-
->willReturn(true, true, false)
53+
$this->connection->expects($this->exactly(2))
54+
->method('getTransactionNestingLevel')
55+
->willReturn(0, 1)
5656
;
5757

5858
$this->middleware->handle(new Envelope(new \stdClass()), $this->getStackMock());

0 commit comments

Comments
 (0)