Skip to content

Commit be65540

Browse files
committedMar 25, 2022
suggested changes, typos fixed
suggested changes typos
1 parent 598c062 commit be65540

File tree

1 file changed

+31
-6
lines changed

1 file changed

+31
-6
lines changed
 

‎adr/2022_03_24_remove_flush_from_handlers.md

+31-6
Original file line numberDiff line numberDiff line change
@@ -5,20 +5,45 @@
55

66
## Context and Problem Statement
77

8-
Using flush() in command handlers can cause problems with inconsistent DB state. Doctrine always uses transactions but on
9-
flush() Doctrine will execute all the changes that have been tracked so far. Implicitly flushing will commit all the
10-
changes queued up so far. Not just the entity you just persisted. This is also one of the reasons why it is dangerous to
11-
just call flush.
8+
When working with entities and persisting the current state to DB one has to call a flush method on entity manager.
9+
By default, it starts and commits transactions to used DB. The other possibility is to start the transaction manually,
10+
which will suspend the auto-commit feature of Doctrine.
1211

1312
## Decision Drivers
1413

1514
* avoid inconsistent data in DB
1615
* flexibility to rollback changes
16+
* provide an easy way to interface with object state before committing transaction
17+
18+
## Considered Options
19+
20+
### Flushing in handlers
21+
22+
Using flush() in command handlers can cause problems with inconsistent DB states if the end-user decides to adjust
23+
models definitions. Implicitly flushing will commit all the changes queued up so far, which may lead to DB error or
24+
committing not consistent data. What is more, we would need an additional feature that would allow adjusting the entity's
25+
state before the transaction will close.
26+
27+
* Bad, because implicitly flushing will commit all the changes queued up so far
28+
* Bad, because we would need an additional feature
29+
30+
### Flushing outside of handlers with the manually triggered transaction
31+
32+
Manual setting up of transactions around handlers clearly defines their boundaries. Even tho this may still lead to DB
33+
error or committing not consistent data, setting boundaries outside of our code improves DX greatly, as an adjustment
34+
on objects may be made with decorator pattern outside of predefined handler.
35+
36+
* Good, because it improves DX
37+
* Good, because transactions around handlers clearly define their boundaries
38+
* Good, because we have flexibility to rollback changes
39+
* Bad, because it may still lead to errors or committing not consistent data
1740

1841
## Decision Outcome
1942

20-
We decided to remove all flushes from command handlers and let DoctrineTransactionMiddleware do it for us because
21-
it gives us ability to avoid an inconsistent data and if something fails we have flexibility to rollback changes.
43+
Chosen option: **"Flushing outside of handlers with the manually triggered transaction"**
44+
45+
It is the most straightforward solution which will suspend the auto-commit feature of Doctrine and gives us more control
46+
and flexibility when working with data.
2247

2348
## References <!-- optional -->
2449

0 commit comments

Comments
 (0)
Please sign in to comment.