-
-
Notifications
You must be signed in to change notification settings - Fork 5.1k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Show ow to configure a doctrine transaction middleware
- Loading branch information
Showing
1 changed file
with
45 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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'] |