|
5 | 5 |
|
6 | 6 | ## Context and Problem Statement
|
7 | 7 |
|
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. |
12 | 11 |
|
13 | 12 | ## Decision Drivers
|
14 | 13 |
|
15 | 14 | * avoid inconsistent data in DB
|
16 | 15 | * 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 |
17 | 40 |
|
18 | 41 | ## Decision Outcome
|
19 | 42 |
|
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. |
22 | 47 |
|
23 | 48 | ## References <!-- optional -->
|
24 | 49 |
|
|
0 commit comments