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

[Messenger] Add a middleware to log when transaction has been left open #41265

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Oct 5, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions 4 src/Symfony/Bridge/Doctrine/CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,10 @@
CHANGELOG
=========

5.4
---
* Add a middleware to log when transaction has been left open `DoctrineOpenTransactionLoggerMiddleware`

5.3
---

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
<?php

/*
* This file is part of the Symfony package.
*
* (c) Fabien Potencier <fabien@symfony.com>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/

namespace Symfony\Bridge\Doctrine\Messenger;

use Doctrine\ORM\EntityManagerInterface;
use Doctrine\Persistence\ManagerRegistry;
use Psr\Log\LoggerInterface;
use Psr\Log\NullLogger;
use Symfony\Component\Messenger\Envelope;
use Symfony\Component\Messenger\Middleware\StackInterface;

/**
* Middleware to log when transaction has been left open.
*
* @author Grégoire Pineau <lyrixx@lyrixx.info>
*/
class DoctrineOpenTransactionLoggerMiddleware extends AbstractDoctrineMiddleware
{
private $logger;

public function __construct(ManagerRegistry $managerRegistry, string $entityManagerName = null, LoggerInterface $logger = null)
{
parent::__construct($managerRegistry, $entityManagerName);

$this->logger = $logger ?? new NullLogger();
}

protected function handleForManager(EntityManagerInterface $entityManager, Envelope $envelope, StackInterface $stack): Envelope
{
try {
return $stack->next()->handle($envelope, $stack);
} finally {
if ($entityManager->getConnection()->isTransactionActive()) {
$this->logger->error('A handler opened a transaction but did not close it.', [
'message' => $envelope->getMessage(),
]);
}
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,62 @@
<?php

/*
* This file is part of the Symfony package.
*
* (c) Fabien Potencier <fabien@symfony.com>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/

namespace Symfony\Bridge\Doctrine\Tests\Messenger;

use Doctrine\DBAL\Connection;
use Doctrine\ORM\EntityManagerInterface;
use Doctrine\Persistence\ManagerRegistry;
use Psr\Log\AbstractLogger;
use Symfony\Bridge\Doctrine\Messenger\DoctrineOpenTransactionLoggerMiddleware;
use Symfony\Component\Messenger\Envelope;
use Symfony\Component\Messenger\Test\Middleware\MiddlewareTestCase;

class DoctrineOpenTransactionLoggerMiddlewareTest extends MiddlewareTestCase
{
private $logger;
private $connection;
private $entityManager;
private $middleware;

protected function setUp(): void
{
$this->logger = new class() extends AbstractLogger {
public $logs = [];

public function log($level, $message, $context = []): void
{
$this->logs[$level][] = $message;
}
};

$this->connection = $this->createMock(Connection::class);

$this->entityManager = $this->createMock(EntityManagerInterface::class);
$this->entityManager->method('getConnection')->willReturn($this->connection);

$managerRegistry = $this->createMock(ManagerRegistry::class);
$managerRegistry->method('getManager')->willReturn($this->entityManager);

$this->middleware = new DoctrineOpenTransactionLoggerMiddleware($managerRegistry, null, $this->logger);
}

public function testMiddlewareWrapsInTransactionAndFlushes()
{
$this->connection->expects($this->exactly(1))
->method('isTransactionActive')
->will($this->onConsecutiveCalls(true, true, false))
;

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

$this->assertSame(['error' => ['A handler opened a transaction but did not close it.']], $this->logger->logs);
}
}
3 changes: 2 additions & 1 deletion 3 src/Symfony/Bridge/Doctrine/composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,8 @@
"doctrine/collections": "~1.0",
"doctrine/data-fixtures": "^1.1",
"doctrine/dbal": "^2.13|^3.0",
"doctrine/orm": "^2.7.3"
"doctrine/orm": "^2.7.3",
"psr/log": "^1|^2|^3"
},
"conflict": {
"doctrine/dbal": "<2.13",
Expand Down
Morty Proxy This is a proxified and sanitized view of the page, visit original site.