Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Appearance settings

Commit 0817d62

Browse filesBrowse files
committed
[Messenger] ensure exception on rollback does not hide previous exception
1 parent 69b7675 commit 0817d62
Copy full SHA for 0817d62

File tree

2 files changed

+29
-2
lines changed
Filter options

2 files changed

+29
-2
lines changed

‎src/Symfony/Bridge/Doctrine/Messenger/DoctrineTransactionMiddleware.php

Copy file name to clipboardExpand all lines: src/Symfony/Bridge/Doctrine/Messenger/DoctrineTransactionMiddleware.php
+8-2Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -27,22 +27,28 @@ class DoctrineTransactionMiddleware extends AbstractDoctrineMiddleware
2727
protected function handleForManager(EntityManagerInterface $entityManager, Envelope $envelope, StackInterface $stack): Envelope
2828
{
2929
$entityManager->getConnection()->beginTransaction();
30+
31+
$success = false;
3032
try {
3133
$envelope = $stack->next()->handle($envelope, $stack);
3234
$entityManager->flush();
3335
$entityManager->getConnection()->commit();
3436

37+
$success = true;
38+
3539
return $envelope;
3640
} catch (\Throwable $exception) {
37-
$entityManager->getConnection()->rollBack();
38-
3941
if ($exception instanceof HandlerFailedException) {
4042
// Remove all HandledStamp from the envelope so the retry will execute all handlers again.
4143
// When a handler fails, the queries of allegedly successful previous handlers just got rolled back.
4244
throw new HandlerFailedException($exception->getEnvelope()->withoutAll(HandledStamp::class), $exception->getWrappedExceptions());
4345
}
4446

4547
throw $exception;
48+
} finally {
49+
if (!$success) {
50+
$entityManager->getConnection()->rollBack();
51+
}
4652
}
4753
}
4854
}

‎src/Symfony/Bridge/Doctrine/Tests/Messenger/DoctrineTransactionMiddlewareTest.php

Copy file name to clipboardExpand all lines: src/Symfony/Bridge/Doctrine/Tests/Messenger/DoctrineTransactionMiddlewareTest.php
+21Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@
1212
namespace Symfony\Bridge\Doctrine\Tests\Messenger;
1313

1414
use Doctrine\DBAL\Connection;
15+
use Doctrine\DBAL\ConnectionException;
1516
use Doctrine\ORM\EntityManagerInterface;
1617
use Doctrine\Persistence\ManagerRegistry;
1718
use PHPUnit\Framework\MockObject\MockObject;
@@ -69,6 +70,26 @@ public function testTransactionIsRolledBackOnException()
6970
$this->middleware->handle(new Envelope(new \stdClass()), $this->getThrowingStackMock());
7071
}
7172

73+
public function testExceptionInRollBackDoesNotHidePreviousException()
74+
{
75+
$this->connection->expects($this->once())->method('beginTransaction');
76+
$this->connection->expects($this->once())->method('rollBack')->willThrowException(new \RuntimeException('Thrown from rollBack.'));
77+
78+
try {
79+
$this->middleware->handle(new Envelope(new \stdClass()), $this->getThrowingStackMock());
80+
} catch (\Throwable $exception) {
81+
}
82+
83+
self::assertNotNull($exception);
84+
self::assertInstanceOf(\RuntimeException::class, $exception);
85+
self::assertSame('Thrown from rollBack.', $exception->getMessage());
86+
87+
$previous = $exception->getPrevious();
88+
self::assertNotNull($previous);
89+
self::assertInstanceOf(\RuntimeException::class, $previous);
90+
self::assertSame('Thrown from next middleware.', $previous->getMessage());
91+
}
92+
7293
public function testInvalidEntityManagerThrowsException()
7394
{
7495
$managerRegistry = $this->createMock(ManagerRegistry::class);

0 commit comments

Comments
0 (0)
Morty Proxy This is a proxified and sanitized view of the page, visit original site.