English spoken conference

Symfony 5: The Fast Track

A new book to learn about developing modern Symfony 5 applications.

Support this project

New in Symfony 4.4: Messenger Middleware to Clear Doctrine Entity Manager

Contributed by
Konstantin Myakshin
in #31334.

UPDATE: this feature was finally removed from Symfony before Symfony 4.4 release. Instead, DoctrineBundle now does the same thing as explained in this blog post but without you having to configure anything.


In the Messenger component, middleware is used to configure what happens when you dispatch a message to a message bus. In Symfony 4.4 we've added a new middleware to clear Doctrine's entity manager after each message is consumed.

Enable it by adding messenger.middleware.doctrine_clear_entity_manager to the middleware of your buses:

  • YAML
    1
    2
    3
    4
    5
    6
    7
    8
    9
    # config/packages/messenger.yaml
    framework:
        messenger:
            buses:
                messenger.bus.default:
                    default_middleware: false
                    middleware:
                        # ...
                        - 'doctrine_clear_entity_manager'
    
  • XML
     1
     2
     3
     4
     5
     6
     7
     8
     9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    <!-- config/packages/messenger.xml -->
    <?xml version="1.0" encoding="UTF-8" ?>
    <container xmlns="http://symfony.com/schema/dic/services"
        xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
        xmlns:framework="http://symfony.com/schema/dic/symfony"
        xsi:schemaLocation="http://symfony.com/schema/dic/services
            https://symfony.com/schema/dic/services/services-1.0.xsd
            http://symfony.com/schema/dic/symfony
            https://symfony.com/schema/dic/symfony/symfony-1.0.xsd">
    
        <framework:config>
            <framework:messenger>
                <!-- ... -->
                <framework:middleware id="doctrine_clear_entity_manager"/>
                <framework:bus name="messenger.bus.default" default-middleware="false"/>
            </framework:messenger>
        </framework:config>
    </container>
    
  • PHP
     1
     2
     3
     4
     5
     6
     7
     8
     9
    10
    11
    12
    13
    14
    // config/packages/messenger.php
    $container->loadFromExtension('framework', [
        'messenger' => [
            'buses' => [
                'messenger.bus.default' => [
                    'middleware' => [
                        // ...
                        'doctrine_clear_entity_manager',
                    ],
                    'default_middleware' => false,
                ],
            ],
        ],
    ]);
    

The first advantage of this middleware is that it reduces the memory consumption when handling messages in long-running processes. The second advantage is that it prevents unexpected side-effects. For example, in the case of an user account recovery process (where an email is sent asynchronously with Messenger and AMQP), if the email address is updated after the first try, the second email is sent to the old email address. Using this middleware will solve that problem.

New in Symfony 4.4: Messenger Middleware to Clear Doctrine Entity Manager symfony.com/blog/new-in-symfony-4-4-messenger-middleware-to-clear-doctrine-entity-manager

Tweet this

Comments

Nice one!
If I am not mistaken one will be able to add it via a shorter `doctrine_clear_entity_manager` id.
@Valentin you are right! I've fixed it. Thanks.
Please note that this blog post has been updated because this feature was finally removed from Symfony 4.4 and moved to DoctrineBundle.
Login with SymfonyConnect to post a comment