#110227Maintain database connections in long-running Messenger workers (ping/close middlewares)
Description
Problem
Long-running Symfony Messenger workers started via typo3 messenger:consume keep
a PHP process – and its database connections – alive across many messages. A
database connection that stays idle between two messages can be dropped by the
server (for example MySQL’s wait_timeout), so the next message handled by the
worker fails with errors such as “MySQL server has gone away”.
Symfony ships DoctrinePingConnectionMiddleware and
@DoctrineCloseConnectionMiddleware@ for exactly this, but they live in the
*non-vendored* symfony/doctrine-bridge package and are built around
@ManagerRegistry@ / EntityManagerInterface (Doctrine ORM). TYPO3 has no ORM
manager and routes connections through its own
@TYPO3\CMS\Core\Database\ConnectionPool@, so the Symfony middlewares cannot be
reused as-is.
Proposed solution
Provide two TYPO3-native messenger middlewares operating on ConnectionPool:
TYPO3\CMS\Core\Messenger\Middleware\DoctrinePingConnectionMiddleware-
before handling a message, pings each open connection with a lightweight
dummy select and transparently closes+reconnects a connection that turned out
to be stale.TYPO3\CMS\Core\Messenger\Middleware\DoctrineCloseConnectionMiddleware-
after handling a message, closes the open connections so the next message
starts with a fresh connection. Closing happens even when the handler throws.
Both middlewares are registered by default and wrap the message handling
(close outermost, ping innermost, around HandleMessageMiddleware).
Behaviour
- They act only on asynchronous consumption: a message is processed only when
it carries aConsumedByWorkerStamp(added by the worker). A synchronous
dispatch – including theSyncTransport– is passed through untouched, so
regular request handling never pings or closes connections. - They operate on currently open connections only (a new
ConnectionPool::getOpenConnectionNames()accessor), so no connection is
opened just to ping or close it. - Staleness detection uses the platform dummy select
(AbstractPlatform::getDummySelectSQL()); on failure the connection is closed
and the check is retried, which reconnects lazily. This is DBAL 4 compatible
(Connection::ping()was removed in DBAL 4).
Extensibility
Two cancellable PSR-14 events allow listeners to exclude individual connections:
TYPO3\CMS\Core\Messenger\Event\BeforeMessengerDoctrinePingEventTYPO3\CMS\Core\Messenger\Event\BeforeMessengerDoctrineCloseEvent
Each exposes getConnectionName() and skip() / shouldSkip() and stops
propagation once skipped. A typical use is keeping a dedicated connection open
across messages (skip its close) while still validating it before the next
message (ping).
Example
Enabling is not required – the maintenance is active for every worker. To keep a
specific connection open across messages, register a listener:
use TYPO3\CMS\Core\Attribute\AsEventListener;
use TYPO3\CMS\Core\Messenger\Event\BeforeMessengerDoctrineCloseEvent;
final class KeepReportingConnectionOpen
{
#[AsEventListener]
public function __invoke(BeforeMessengerDoctrineCloseEvent $event): void
{
if ($event->getConnectionName() === 'Reporting') {
$event->skip();
}
}
}
Notes
- No new configuration option is introduced; the middlewares are always active
for worker (async) message handling and inert for synchronous dispatch. - Complements the transport routing / async separation work
(#101699, #110223, #107032).
Reviews 4
Related issues 1
History 211
-
Gerrit Code Review pushed patch set 1 for main to change 94834 status
NewUnder Review <span data-processor="relativetime" data-value="2026-07-19T15:32:46+00:00" > 07/19/26, 15:32 </span> -
Gerrit Code Review pushed patch set 2 for main to change 94834 <span data-processor="relativetime" data-value="2026-07-19T16:00:44+00:00" > 07/19/26, 16:00 </span>
-
Gerrit Code Review pushed patch set 3 for main to change 94834 <span data-processor="relativetime" data-value="2026-07-19T16:28:31+00:00" > 07/19/26, 16:28 </span>
-
Gerrit Code Review pushed patch set 4 for main to change 94834 <span data-processor="relativetime" data-value="2026-07-19T17:31:22+00:00" > 07/19/26, 17:31 </span>
-
Gerrit Code Review pushed patch set 5 for main to change 94834 <span data-processor="relativetime" data-value="2026-07-19T18:29:44+00:00" > 07/19/26, 18:29 </span>
-
Gerrit Code Review pushed patch set 6 for main to change 94834 <span data-processor="relativetime" data-value="2026-07-19T20:38:23+00:00" > 07/19/26, 20:38 </span>
-
Gerrit Code Review pushed patch set 7 for main to change 94834 <span data-processor="relativetime" data-value="2026-07-20T13:14:11+00:00" > 07/20/26, 13:14 </span>
-
Gerrit Code Review pushed patch set 8 for main to change 94834 <span data-processor="relativetime" data-value="2026-07-28T06:29:54+00:00" > 07/28/26, 06:29 </span>
-
Gerrit Code Review pushed patch set 9 for main to change 94834 <span data-processor="relativetime" data-value="2026-08-01T20:36:48+00:00" > 08/01/26, 20:36 </span>
-
Stefan Bürk commented and changed status
Under ReviewResolved done0 %100 % <span data-processor="relativetime" data-value="2026-10-08T12:25:13+00:00" > 10/08/26, 12:25 </span> -
Georg Ringer changed Related to #111029 <span data-processor="relativetime" data-value="2026-10-12T19:29:23+00:00" > 10/12/26, 19:29 </span>
Applied in changeset commit:1b416fa15221a6ef4e386c3bb2e39a387d393789.