From c497ed2975a3a5f10c1519db1c7b1650048d1dcf Mon Sep 17 00:00:00 2001 From: Tobias Nyholm Date: Wed, 4 Jul 2018 16:15:24 +0200 Subject: [PATCH 1/3] Show ow to configure a doctrine transaction middleware --- messenger/working-with-doctrine.rst | 45 +++++++++++++++++++++++++++++ 1 file changed, 45 insertions(+) create mode 100644 messenger/working-with-doctrine.rst diff --git a/messenger/working-with-doctrine.rst b/messenger/working-with-doctrine.rst new file mode 100644 index 00000000000..87e4bb73311 --- /dev/null +++ b/messenger/working-with-doctrine.rst @@ -0,0 +1,45 @@ +.. index:: + single: Messenger; Working with Doctrine + +Working with Doctrine +===================== + +If your message handlers writes to a database it is a good idea to wrap all those +writes in a single Doctrine transaction. This make sure that if one of your database +query fails, then all queries are rolled back and give you a change to handle the +exception knowing that your database was not changed by your message handler. + +To make sure your message bus wraps the handler in one transaction you must first +register the middleware as a service. + +.. configuration-block:: + + .. code-block:: yaml + + # config/services.yaml + services: + app.messenger.middleware_factory.transaction: + class: Symfony\Bridge\Doctrine\Messenger\DoctrineTransactionMiddlewareFactory + arguments: ['@doctrine'] + + app.doctrine_transaction_middleware: + class: Symfony\Bridge\Doctrine\Messenger\DoctrineTransactionMiddleware + factory: ['@app.messenger.middleware_factory.transaction', 'createMiddleware'] + abstract: true + arguments: ['default'] + +Next thing you need to do is to add the middleware to your bus configuration. + +.. configuration-block:: + + .. code-block:: yaml + + # config/packages/messenger.yaml + framework: + # ... + default_bus: messenger.bus.command + buses: + messenger.bus.command: + middleware: + - messenger.middleware.validation + - app.doctrine_transaction_middleware: ['default'] From cc05ed2948ff8cacbdd7117c8a4ca6314c4d4cc1 Mon Sep 17 00:00:00 2001 From: Tobias Nyholm Date: Mon, 8 Apr 2019 21:04:57 +0200 Subject: [PATCH 2/3] Adding PHP and XML configuration --- messenger/working-with-doctrine.rst | 63 +++++++++++++++++++---------- 1 file changed, 41 insertions(+), 22 deletions(-) diff --git a/messenger/working-with-doctrine.rst b/messenger/working-with-doctrine.rst index 87e4bb73311..1c2fb605e8a 100644 --- a/messenger/working-with-doctrine.rst +++ b/messenger/working-with-doctrine.rst @@ -9,25 +9,6 @@ writes in a single Doctrine transaction. This make sure that if one of your data query fails, then all queries are rolled back and give you a change to handle the exception knowing that your database was not changed by your message handler. -To make sure your message bus wraps the handler in one transaction you must first -register the middleware as a service. - -.. configuration-block:: - - .. code-block:: yaml - - # config/services.yaml - services: - app.messenger.middleware_factory.transaction: - class: Symfony\Bridge\Doctrine\Messenger\DoctrineTransactionMiddlewareFactory - arguments: ['@doctrine'] - - app.doctrine_transaction_middleware: - class: Symfony\Bridge\Doctrine\Messenger\DoctrineTransactionMiddleware - factory: ['@app.messenger.middleware_factory.transaction', 'createMiddleware'] - abstract: true - arguments: ['default'] - Next thing you need to do is to add the middleware to your bus configuration. .. configuration-block:: @@ -37,9 +18,47 @@ Next thing you need to do is to add the middleware to your bus configuration. # config/packages/messenger.yaml framework: # ... - default_bus: messenger.bus.command buses: messenger.bus.command: middleware: - - messenger.middleware.validation - - app.doctrine_transaction_middleware: ['default'] + - validation + - doctrine_transaction + + .. code-block:: xml + + + + + + + + + + + + + + + + .. code-block:: php + + // config/packages/messenger.php + $container->loadFromExtension('framework', [ + 'messenger' => [ + 'buses' => [ + 'messenger.bus.commands' => [ + 'middleware' => [ + 'validation', + 'doctrine_transaction', + ], + ], + ], + ], + ]); + From 5b0d719751e479f198653102f421439568d7b4c8 Mon Sep 17 00:00:00 2001 From: Nyholm Date: Sun, 28 Apr 2019 09:15:50 +0200 Subject: [PATCH 3/3] typos --- messenger/working-with-doctrine.rst | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/messenger/working-with-doctrine.rst b/messenger/working-with-doctrine.rst index 1c2fb605e8a..317477b0729 100644 --- a/messenger/working-with-doctrine.rst +++ b/messenger/working-with-doctrine.rst @@ -6,8 +6,8 @@ Working with Doctrine If your message handlers writes to a database it is a good idea to wrap all those writes in a single Doctrine transaction. This make sure that if one of your database -query fails, then all queries are rolled back and give you a change to handle the -exception knowing that your database was not changed by your message handler. +query fails, then all queries are rolled back and give you a chance to handle the +exception knowing that your database was not changed by your message handler(s). Next thing you need to do is to add the middleware to your bus configuration.