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

Problem with defining Doctrine middleware for different connections in Messenger #37568

Copy link
Copy link
Closed
@X-Coder264

Description

@X-Coder264
Issue body actions

Symfony version(s) affected: 4.4.9

Description
I have multiple connections in my app so I've added the Doctrine close and ping connection middleware to my Messenger bus like this:

framework:
    messenger:
        buses:
            messenger.bus.default:
                middleware:
                    - doctrine_ping_connection: ['main']
                    - doctrine_ping_connection: ['stats']
                    - doctrine_ping_connection: ['logs']
                    - doctrine_close_connection: ['main']
                    - doctrine_close_connection: ['stats']
                    - doctrine_close_connection: ['logs']

After deploying this to production we started getting MySQL server has gone away errors. Turns out that instead of the three connections being closed once the last connection (logs) was closed three times.

The problem is in the MessengerPass -> https://github.com/symfony/symfony/blob/v4.4.9/src/Symfony/Component/Messenger/DependencyInjection/MessengerPass.php#L318-L334 because it doesn't take arguments into account to create an unique middleware service ID.

The documentation about middleware is really scarce (https://symfony.com/doc/current/messenger.html#middleware-for-doctrine) so even though this is not documented I've expected that this would work.

As a temporary workaround I've registered new services for this:

framework:
    messenger:
        buses:
            messenger.bus.default:
                middleware:
                    - messenger.middleware.doctrine_ping_connection_main
                    - messenger.middleware.doctrine_ping_connection_stats
                    - messenger.middleware.doctrine_ping_connection_logs
                    - messenger.middleware.doctrine_close_connection_main
                    - messenger.middleware.doctrine_close_connection_stats
                    - messenger.middleware.doctrine_close_connection_logs

    # in services.yaml
    messenger.middleware.doctrine_ping_connection_main:
        class: Symfony\Bridge\Doctrine\Messenger\DoctrinePingConnectionMiddleware
        arguments:
            $entityManagerName: 'main'

    messenger.middleware.doctrine_ping_connection_stats:
        class: Symfony\Bridge\Doctrine\Messenger\DoctrinePingConnectionMiddleware
        arguments:
            $entityManagerName: 'stats'

    messenger.middleware.doctrine_ping_connection_logs:
        class: Symfony\Bridge\Doctrine\Messenger\DoctrinePingConnectionMiddleware
        arguments:
            $entityManagerName: 'logs'

    messenger.middleware.doctrine_close_connection_main:
        class: Symfony\Bridge\Doctrine\Messenger\DoctrineCloseConnectionMiddleware
        arguments:
            $entityManagerName: 'main'

    messenger.middleware.doctrine_close_connection_stats:
        class: Symfony\Bridge\Doctrine\Messenger\DoctrineCloseConnectionMiddleware
        arguments:
            $entityManagerName: 'stats'

    messenger.middleware.doctrine_close_connection_logs:
        class: Symfony\Bridge\Doctrine\Messenger\DoctrineCloseConnectionMiddleware
        arguments:
            $entityManagerName: 'logs'

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions

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