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 d0dbdd0

Browse filesBrowse files
committed
[Messenger] Add a middleware to ensure all transaction has been closed
1 parent cbe1f81 commit d0dbdd0
Copy full SHA for d0dbdd0

File tree

Expand file treeCollapse file tree

3 files changed

+113
-0
lines changed
Filter options
Expand file treeCollapse file tree

3 files changed

+113
-0
lines changed

‎src/Symfony/Bridge/Doctrine/CHANGELOG.md

Copy file name to clipboardExpand all lines: src/Symfony/Bridge/Doctrine/CHANGELOG.md
+4Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,10 @@
11
CHANGELOG
22
=========
33

4+
5.4
5+
---
6+
* Add a middleware to ensure all transaction has been closed `DoctrineTransactionWatchdogMiddleware`
7+
48
5.3
59
---
610

+52Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,52 @@
1+
<?php
2+
3+
/*
4+
* This file is part of the Symfony package.
5+
*
6+
* (c) Fabien Potencier <fabien@symfony.com>
7+
*
8+
* For the full copyright and license information, please view the LICENSE
9+
* file that was distributed with this source code.
10+
*/
11+
12+
namespace Symfony\Bridge\Doctrine\Messenger;
13+
14+
use Doctrine\ORM\EntityManagerInterface;
15+
use Doctrine\Persistence\ManagerRegistry;
16+
use Psr\Log\LoggerInterface;
17+
use Psr\Log\NullLogger;
18+
use Symfony\Component\Messenger\Envelope;
19+
use Symfony\Component\Messenger\Middleware\StackInterface;
20+
21+
/**
22+
* Search for open transactions, then log and close them.
23+
*
24+
* @author Grégoire Pineau <lyrixx@lyrixx.info>
25+
*/
26+
class DoctrineTransactionWatchdogMiddleware extends AbstractDoctrineMiddleware
27+
{
28+
public function __construct(ManagerRegistry $managerRegistry, string $entityManagerName = null, LoggerInterface $logger = null)
29+
{
30+
parent::__construct($managerRegistry, $entityManagerName);
31+
32+
$this->logger = $logger ?? new NullLogger();
33+
}
34+
35+
protected function handleForManager(EntityManagerInterface $entityManager, Envelope $envelope, StackInterface $stack): Envelope
36+
{
37+
try {
38+
return $stack->next()->handle($envelope, $stack);
39+
} finally {
40+
if ($entityManager->getConnection()->isTransactionActive()) {
41+
$this->logger->error('A handler opened a transaction but did not close it.', [
42+
'message' => $envelope->getMessage(),
43+
]);
44+
45+
$connection = $entityManager->getConnection();
46+
while ($connection->isTransactionActive()) {
47+
$connection->rollBack();
48+
}
49+
}
50+
}
51+
}
52+
}
+57Lines changed: 57 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,57 @@
1+
<?php
2+
3+
/*
4+
* This file is part of the Symfony package.
5+
*
6+
* (c) Fabien Potencier <fabien@symfony.com>
7+
*
8+
* For the full copyright and license information, please view the LICENSE
9+
* file that was distributed with this source code.
10+
*/
11+
12+
namespace Symfony\Bridge\Doctrine\Tests\Messenger;
13+
14+
use Doctrine\DBAL\Connection;
15+
use Doctrine\ORM\EntityManagerInterface;
16+
use Doctrine\Persistence\ManagerRegistry;
17+
use Psr\Log\Test\TestLogger;
18+
use Symfony\Bridge\Doctrine\Messenger\DoctrineTransactionWatchdogMiddleware;
19+
use Symfony\Component\Messenger\Envelope;
20+
use Symfony\Component\Messenger\Test\Middleware\MiddlewareTestCase;
21+
22+
class DoctrineTransactionWatchdogMiddlewareTest extends MiddlewareTestCase
23+
{
24+
private $logger;
25+
private $connection;
26+
private $entityManager;
27+
private $middleware;
28+
29+
protected function setUp(): void
30+
{
31+
$this->logger = new TestLogger();
32+
$this->connection = $this->createMock(Connection::class);
33+
34+
$this->entityManager = $this->createMock(EntityManagerInterface::class);
35+
$this->entityManager->method('getConnection')->willReturn($this->connection);
36+
37+
$managerRegistry = $this->createMock(ManagerRegistry::class);
38+
$managerRegistry->method('getManager')->willReturn($this->entityManager);
39+
40+
$this->middleware = new DoctrineTransactionWatchdogMiddleware($managerRegistry, null, $this->logger);
41+
}
42+
43+
public function testMiddlewareWrapsInTransactionAndFlushes()
44+
{
45+
$this->connection->expects($this->exactly(3))
46+
->method('isTransactionActive')
47+
->will($this->onConsecutiveCalls(true, true, false))
48+
;
49+
$this->connection->expects($this->exactly(1))
50+
->method('rollBack')
51+
;
52+
53+
$this->middleware->handle(new Envelope(new \stdClass()), $this->getStackMock());
54+
55+
$this->logger->hasErrorThatContains('A handler opened a transaction but did not close it.');
56+
}
57+
}

0 commit comments

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