Thanks to visit codestin.com
Credit goes to github.com

Skip to content

[BridgeDoctrineMessenger] Doctrine ping connection middleware #31061

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
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
2 changes: 2 additions & 0 deletions src/Symfony/Bridge/Doctrine/CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,8 @@ CHANGELOG
* changed guessing of DECIMAL to set the `input` option of `NumberType` to string
* deprecated not passing an `IdReader` to the `DoctrineChoiceLoader` when query can be optimized with a single id field
* deprecated passing an `IdReader` to the `DoctrineChoiceLoader` when entities have a composite id
* added `DoctrinePingConnectionMiddleware`
* added `DoctrineCloseConnectionMiddleware`

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

/*
* This file is part of the Symfony package.
*
* (c) Fabien Potencier <[email protected]>
*
* 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\Common\Persistence\ManagerRegistry;
use Doctrine\ORM\EntityManagerInterface;
use Symfony\Component\Messenger\Envelope;
use Symfony\Component\Messenger\Middleware\MiddlewareInterface;
use Symfony\Component\Messenger\Middleware\StackInterface;

/**
* Closes connection and therefore saves number of connections.
*
* @author Fuong <[email protected]>
*
* @experimental in 4.3
*/
class DoctrineCloseConnectionMiddleware implements MiddlewareInterface
{
private $managerRegistry;
private $entityManagerName;

public function __construct(ManagerRegistry $managerRegistry, string $entityManagerName = null)
{
$this->managerRegistry = $managerRegistry;
$this->entityManagerName = $entityManagerName;
}

/**
* {@inheritdoc}
*/
public function handle(Envelope $envelope, StackInterface $stack): Envelope
{
$entityManager = $this->managerRegistry->getManager($this->entityManagerName);

if (!$entityManager instanceof EntityManagerInterface) {
throw new \InvalidArgumentException(sprintf('The ObjectManager with name "%s" must be an instance of EntityManagerInterface', $this->entityManagerName));
}

try {
$connection = $entityManager->getConnection();

return $stack->next()->handle($envelope, $stack);
} finally {
$connection->close();
}
}
}
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 <[email protected]>
*
* 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\Common\Persistence\ManagerRegistry;
use Doctrine\ORM\EntityManagerInterface;
use Symfony\Component\Messenger\Envelope;
use Symfony\Component\Messenger\Middleware\MiddlewareInterface;
use Symfony\Component\Messenger\Middleware\StackInterface;

/**
* Checks whether the connection is still open or reconnects otherwise.
*
* @author Fuong <[email protected]>
*
* @experimental in 4.3
*/
class DoctrinePingConnectionMiddleware implements MiddlewareInterface
{
private $managerRegistry;
private $entityManagerName;

public function __construct(ManagerRegistry $managerRegistry, string $entityManagerName = null)
{
$this->managerRegistry = $managerRegistry;
$this->entityManagerName = $entityManagerName;
}

/**
* {@inheritdoc}
*/
public function handle(Envelope $envelope, StackInterface $stack): Envelope
{
$entityManager = $this->managerRegistry->getManager($this->entityManagerName);

if (!$entityManager instanceof EntityManagerInterface) {
throw new \InvalidArgumentException(sprintf('The ObjectManager with name "%s" must be an instance of EntityManagerInterface', $this->entityManagerName));
}

$connection = $entityManager->getConnection();

if (!$connection->ping()) {
$connection->close();
$connection->connect();
}

if (!$entityManager->isOpen()) {
$this->managerRegistry->resetManager($this->entityManagerName);
}

return $stack->next()->handle($envelope, $stack);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
<?php

/*
* This file is part of the Symfony package.
*
* (c) Fabien Potencier <[email protected]>
*
* 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\Common\Persistence\ManagerRegistry;
use Doctrine\DBAL\Connection;
use Doctrine\ORM\EntityManagerInterface;
use Symfony\Bridge\Doctrine\Messenger\DoctrineCloseConnectionMiddleware;
use Symfony\Component\Messenger\Envelope;
use Symfony\Component\Messenger\Test\Middleware\MiddlewareTestCase;

class DoctrineCloseConnectionMiddlewareTest extends MiddlewareTestCase
{
private $connection;
private $entityManager;
private $managerRegistry;
private $middleware;
private $entityManagerName = 'default';

protected function setUp()
{
$this->connection = $this->createMock(Connection::class);

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

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

$this->middleware = new DoctrineCloseConnectionMiddleware(
$this->managerRegistry,
$this->entityManagerName
);
}

public function testMiddlewareCloseConnection()
{
$this->connection->expects($this->once())
->method('close')
;

$this->middleware->handle(new Envelope(new \stdClass()), $this->getStackMock());
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,74 @@
<?php

/*
* This file is part of the Symfony package.
*
* (c) Fabien Potencier <[email protected]>
*
* 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\Common\Persistence\ManagerRegistry;
use Doctrine\DBAL\Connection;
use Doctrine\ORM\EntityManagerInterface;
use Symfony\Bridge\Doctrine\Messenger\DoctrinePingConnectionMiddleware;
use Symfony\Component\Messenger\Envelope;
use Symfony\Component\Messenger\Test\Middleware\MiddlewareTestCase;

class DoctrinePingConnectionMiddlewareTest extends MiddlewareTestCase
{
private $connection;
private $entityManager;
private $managerRegistry;
private $middleware;
private $entityManagerName = 'default';

protected function setUp()
{
$this->connection = $this->createMock(Connection::class);

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

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

$this->middleware = new DoctrinePingConnectionMiddleware(
$this->managerRegistry,
$this->entityManagerName
);
}

public function testMiddlewarePingOk()
{
$this->connection->expects($this->once())
->method('ping')
->willReturn(false);

$this->connection->expects($this->once())
->method('close')
;
$this->connection->expects($this->once())
->method('connect')
;

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

public function testMiddlewarePingResetEntityManager()
{
$this->entityManager->expects($this->once())
->method('isOpen')
->willReturn(false)
;
$this->managerRegistry->expects($this->once())
->method('resetManager')
->with($this->entityManagerName)
;

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