Skip to content

Commit c497ed2

Browse files
committed
Show ow to configure a doctrine transaction middleware
1 parent 1d3d7e0 commit c497ed2

File tree

1 file changed

+45
-0
lines changed

1 file changed

+45
-0
lines changed

messenger/working-with-doctrine.rst

Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
.. index::
2+
single: Messenger; Working with Doctrine
3+
4+
Working with Doctrine
5+
=====================
6+
7+
If your message handlers writes to a database it is a good idea to wrap all those
8+
writes in a single Doctrine transaction. This make sure that if one of your database
9+
query fails, then all queries are rolled back and give you a change to handle the
10+
exception knowing that your database was not changed by your message handler.
11+
12+
To make sure your message bus wraps the handler in one transaction you must first
13+
register the middleware as a service.
14+
15+
.. configuration-block::
16+
17+
.. code-block:: yaml
18+
19+
# config/services.yaml
20+
services:
21+
app.messenger.middleware_factory.transaction:
22+
class: Symfony\Bridge\Doctrine\Messenger\DoctrineTransactionMiddlewareFactory
23+
arguments: ['@doctrine']
24+
25+
app.doctrine_transaction_middleware:
26+
class: Symfony\Bridge\Doctrine\Messenger\DoctrineTransactionMiddleware
27+
factory: ['@app.messenger.middleware_factory.transaction', 'createMiddleware']
28+
abstract: true
29+
arguments: ['default']
30+
31+
Next thing you need to do is to add the middleware to your bus configuration.
32+
33+
.. configuration-block::
34+
35+
.. code-block:: yaml
36+
37+
# config/packages/messenger.yaml
38+
framework:
39+
# ...
40+
default_bus: messenger.bus.command
41+
buses:
42+
messenger.bus.command:
43+
middleware:
44+
- messenger.middleware.validation
45+
- app.doctrine_transaction_middleware: ['default']

0 commit comments

Comments
 (0)