diff --git a/DependencyInjection/Configuration.php b/DependencyInjection/Configuration.php
index 68e40fc1..65e4259c 100644
--- a/DependencyInjection/Configuration.php
+++ b/DependencyInjection/Configuration.php
@@ -221,10 +221,9 @@ private function getDbalConnectionsNode(): ArrayNodeDefinition
->end()
->booleanNode('disable_type_comments')->end()
->scalarNode('server_version')->end()
- ->integerNode('check_connection_timing')->end()
+ ->integerNode('check_connection_timing')->defaultValue(30)->end()
->scalarNode('driver_class')->end()
->scalarNode('wrapper_class')->end()
- ->scalarNode('check_connection_frequency')->defaultValue(30)->end()
->booleanNode('keep_slave')
->setDeprecated(
'doctrine/doctrine-bundle',
diff --git a/DependencyInjection/DoctrineExtension.php b/DependencyInjection/DoctrineExtension.php
index f08570e8..579fdc69 100644
--- a/DependencyInjection/DoctrineExtension.php
+++ b/DependencyInjection/DoctrineExtension.php
@@ -203,12 +203,7 @@ protected function dbalLoad(array $config, ContainerBuilder $container)
$connWithProfiling = [];
$connWithBacktrace = [];
$timingByConnection = [];
- $skipTiming = false;
-
- if (! $container->hasParameter('kernel.runtime_mode') && ! $container->hasParameter('kernel.runtime_mode.worker')) {
- $container->removeDefinition('doctrine.listeners.doctrine_connection_listener');
- $skipTiming = true;
- }
+ $connWithTimingCheck = [];
foreach ($config['connections'] as $name => $connection) {
if ($connection['logging']) {
@@ -223,16 +218,16 @@ protected function dbalLoad(array $config, ContainerBuilder $container)
}
}
- if (! $skipTiming) {
- $timingByConnection[] = ['name' => $connections[$name], 'timing' => $connection['check_connection_timing']];
+ if ($connection['check_connection_timing']) {
+ $connWithTimingCheck[] = $name;
}
+ $timingByConnection[$connections[$name]] = $connection['check_connection_timing'];
+
$this->loadDbalConnection($name, $connection, $container);
}
- if (! $skipTiming) {
- $container->getDefinition('doctrine.orm.listeners.doctrine_connection_listener')->setArgument(1, $timingByConnection);
- }
+ $container->getDefinition('doctrine.orm.listeners.doctrine_connection_listener')->setArgument(2, $timingByConnection);
$container->registerForAutoconfiguration(MiddlewareInterface::class)->addTag('doctrine.middleware');
@@ -250,7 +245,7 @@ protected function dbalLoad(array $config, ContainerBuilder $container)
}
});
- $this->registerDbalMiddlewares($container, $connWithLogging, $connWithProfiling, $connWithBacktrace);
+ $this->registerDbalMiddlewares($container, $connWithLogging, $connWithProfiling, $connWithBacktrace, $connWithTimingCheck);
}
/**
@@ -1201,7 +1196,8 @@ private function registerDbalMiddlewares(
ContainerBuilder $container,
array $connWithLogging,
array $connWithProfiling,
- array $connWithBacktrace
+ array $connWithBacktrace,
+ array $connWithTimingCheck
): void {
$loader = new XmlFileLoader($container, new FileLocator(__DIR__ . '/../Resources/config'));
$loader->load('middlewares.xml');
@@ -1217,5 +1213,11 @@ private function registerDbalMiddlewares(
$debugMiddlewareAbstractDef
->addTag('doctrine.middleware', ['connection' => $connName]);
}
+
+ $checkTimingMiddlewareAbstractDef = $container->getDefinition('doctrine.connection.keep.alive_middleware');
+ foreach ($connWithTimingCheck as $connName) {
+ $checkTimingMiddlewareAbstractDef
+ ->addTag('doctrine.middleware', ['connection' => $connName, 'priority' => 10]);
+ }
}
}
diff --git a/Middleware/ConnectionKeepAlive.php b/Middleware/ConnectionKeepAlive.php
new file mode 100644
index 00000000..f36f0776
--- /dev/null
+++ b/Middleware/ConnectionKeepAlive.php
@@ -0,0 +1,52 @@
+connectionName = $connectionName;
+ $this->connectionTimingWeakMap = $connectionTimingWeakMap;
+ }
+
+ public function setConnectionName(string $name): void
+ {
+ $this->connectionName = $name;
+ }
+
+ public function wrap(Driver $driver): Driver
+ {
+ return new class($driver, $this->connectionTimingWeakMap, $this->connectionName) extends AbstractDriverMiddleware {
+ private Driver $driver;
+ private ConnectionTimingWeakMap $connectionTimingWeakMap;
+ private string $connectionName;
+
+ public function __construct(Driver $driver, ConnectionTimingWeakMap $connectionTimingWeakMap, string $connectionName = 'default')
+ {
+ $this->connectionName = $connectionName;
+ $this->connectionTimingWeakMap = $connectionTimingWeakMap;
+ $this->driver = $driver;
+
+ parent::__construct($driver);
+ }
+
+ public function connect(array $params): DriverConnection
+ {
+ $connection = parent::connect($params);
+ $this->connectionTimingWeakMap->setTimingPerConnection($connection, $this->connectionName);
+
+ return $connection;
+ }
+ };
+ }
+}
diff --git a/Resources/config/dbal.xml b/Resources/config/dbal.xml
index 3057d3b3..7d02f86f 100644
--- a/Resources/config/dbal.xml
+++ b/Resources/config/dbal.xml
@@ -101,7 +101,8 @@
-
+
+
diff --git a/Resources/config/middlewares.xml b/Resources/config/middlewares.xml
index d6bf92bc..4a92e52d 100644
--- a/Resources/config/middlewares.xml
+++ b/Resources/config/middlewares.xml
@@ -17,5 +17,8 @@
+
+
+