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 d8773ac

Browse filesBrowse files
committed
[DoctrineBridge] doctrine connection listener for long running runtime
1 parent 014bfac commit d8773ac
Copy full SHA for d8773ac

File tree

2 files changed

+121
-0
lines changed
Filter options

2 files changed

+121
-0
lines changed
+89Lines changed: 89 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,89 @@
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\Middleware;
13+
14+
use Doctrine\DBAL\Connection;
15+
use Doctrine\DBAL\Driver;
16+
use Doctrine\DBAL\Driver\Connection as DriverConnection;
17+
use Doctrine\DBAL\Driver\Middleware\AbstractDriverMiddleware;
18+
use Doctrine\DBAL\Exception as DBALException;
19+
use Doctrine\ORM\EntityManagerInterface;
20+
use Doctrine\Persistence\ManagerRegistry;
21+
use Symfony\Component\DependencyInjection\ContainerInterface;
22+
23+
class DoctrineConnectionDriver extends AbstractDriverMiddleware
24+
{
25+
public function __construct(private Driver $driver, private readonly ManagerRegistry $managerRegistry, private readonly ContainerInterface $container)
26+
{
27+
parent::__construct($driver);
28+
}
29+
30+
public function connect(array $params): DriverConnection
31+
{
32+
$connectionServices = $this->managerRegistry->getConnectionNames();
33+
34+
foreach ($connectionServices as $connectionServiceName) {
35+
if (!$this->container->initialized($connectionServiceName)) {
36+
continue;
37+
}
38+
39+
$connection = $this->container->get($connectionServiceName);
40+
41+
if (!$connection instanceof Connection) {
42+
continue;
43+
}
44+
45+
if ($connection->isConnected()) {
46+
$this->pingConnection($connection);
47+
}
48+
49+
$managerNames = $this->managerRegistry->getManagerNames();
50+
51+
foreach ($managerNames as $managerName) {
52+
if (!$this->container->initialized($managerName)) {
53+
continue;
54+
}
55+
56+
$manager = $this->container->get($managerName);
57+
58+
if (!$manager instanceof EntityManagerInterface) {
59+
continue;
60+
}
61+
62+
if (!$manager->isOpen()) {
63+
$this->managerRegistry->resetManager($managerName);
64+
}
65+
}
66+
}
67+
68+
return parent::connect($params);
69+
}
70+
71+
private function pingConnection(Connection $connection): void
72+
{
73+
try {
74+
$this->executeDummySql($connection);
75+
} catch (DBALException $e) {
76+
$connection->close();
77+
// Attempt to reestablish the lazy connection by sending another query.
78+
$this->executeDummySql($connection);
79+
}
80+
}
81+
82+
/**
83+
* @throws DBALException
84+
*/
85+
private function executeDummySql(Connection $connection): void
86+
{
87+
$connection->executeQuery($connection->getDatabasePlatform()->getDummySelectSQL());
88+
}
89+
}
+32Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
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\Middleware;
13+
14+
use Doctrine\DBAL\Driver;
15+
use Doctrine\DBAL\Driver\Middleware;
16+
use Doctrine\Persistence\ManagerRegistry;
17+
use Symfony\Component\DependencyInjection\ContainerInterface;
18+
19+
/**
20+
* Based on https://github.com/Baldinof/roadrunner-bundle/blob/3.x/src/Integration/Doctrine/DoctrineORMMiddleware.php.
21+
*/
22+
class DoctrineConnectionMiddleware implements Middleware
23+
{
24+
public function __construct(private ManagerRegistry $managerRegistry, private ContainerInterface $container)
25+
{
26+
}
27+
28+
public function wrap(Driver $driver): Driver
29+
{
30+
return new DoctrineConnectionDriver($driver, $this->managerRegistry, $this->container);
31+
}
32+
}

0 commit comments

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