Skip to content

Write the metadata through the store's own entity manager - #38

Closed
loevgaard wants to merge 2 commits into
2.xfrom
metadata-store-own-entity-manager
Closed

loevgaard wants to merge 2 commits into
2.xfrom
metadata-store-own-entity-manager

Conversation

@loevgaard

Copy link
Copy Markdown
Member

Closes #16.

Summary

DoctrineOrmBasedMetadataStore::store() writes changed metadata through a new EntityManager on the connection, Configuration and EventManager of the application's entity manager, instead of flushing the application's.

 store(client)
   if metadata hasn't changed
     return                                   # no entity manager, no query
-  manager = application's entity manager
+  manager = new EntityManager(application's connection, configuration, event manager)
   entity = manager.find(client id) ?? persist a new entity
   entity.setMetadata(metadata)
   manager.flush()                            # a unique violation from a concurrent INSERT is ignored

Before, that flush also wrote every change the application made to its managed entities without flushing it, e.g. the half-done changes of a controller that threw, since kernel.finish_request runs then too. And when a concurrent request had inserted the client's row first, the failed INSERT closed the application's entity manager for the rest of the request. Now the store's unit of work holds only the client's row, and a failed INSERT closes only the store's entity manager. The concurrent row still wins. load() still reads through the application's entity manager.

Sharing the Configuration and EventManager keeps a custom metadata_class's mapping, constructor, lifecycle callbacks and entity listeners, and the application's listeners, running as before. ORM 3.7 declares getEventManager() as returning EventManagerInterface, but its constructor takes only EventManager, so an inline @var narrows it: ORM 3.x still returns an EventManager, and the constructor's native type enforces it.

ADR-0005 records the decision and the rejected options: writing with DBAL, an entity manager the application configures, and one private entity manager. README, UPGRADE-2.0.md and CLAUDE.md describe the new write path. src/ now uses the ORM's classes directly, so composer-dependency-analyser.php drops its doctrine/orm ignore.

Evidence

  • Before: the new MetadataTest::it_does_not_store_changes_that_the_application_did_not_flush fails in all three metadata class environments. Another client's row, changed in memory by the request but never flushed, is stored as {"changed":"in memory only"}.
    After: it passes. The other client's row keeps {"foo":"bar"}.
  • Before: it_keeps_the_row_of_a_concurrent_request_that_stored_it_first, with the new isOpen() assertion, fails: "The failed write closed the entity manager of the application".
    After: it passes.
  • The full suite (154 tests), PHPStan and ECS pass. Infection gives 100% MSI and 100% covered MSI.
  • CI's lowest job, reproduced locally (PHP 8.1, ORM 2.17.0, DBAL 3.7.0, DoctrineBundle 2.11.3, Symfony 6.4.0): new EntityManager(Connection, Configuration, ?EventManager) is public there, and all 154 tests pass.

Merge Danger

Door: two-way

A revert brings back the flush of the application's entity manager. The table, its columns and the stored rows don't change.

Blast Radius: Doctrine listeners

  • The application's Doctrine listeners (onFlush, postFlush, …) also see the store's flush, with the store's entity manager in their event arguments.
  • An application that, knowingly or not, relied on the bundle's flush to save its own changes at the end of a request loses those changes. That flush only happened when the metadata changed.
  • The store's entity manager enables none of the application's SQL filters, so it finds a row that a filter hides from load().
  • A write costs one more SELECT, and the application's copy of the row that load() found isn't refreshed after the write.

DoctrineOrmBasedMetadataStore::store() flushed the application's entity
manager, so it also wrote every change the application made to its
managed entities without flushing it, e.g. the half-done changes of a
controller that threw. And when a concurrent request had inserted the
client's row first, the failed INSERT closed the application's entity
manager.

store() now writes through a new EntityManager on the connection,
Configuration and EventManager of the application's, and drops it after
the flush. Its unit of work only holds the client's row, and a failed
INSERT closes only that entity manager. load() still reads through the
application's entity manager.

Closes #16
@loevgaard

Copy link
Copy Markdown
Member Author

Superseded by #40. Writing through a new entity manager per write kept the ORM where a key/value store doesn't need it: an entity manager built outside DoctrineBundle, the application's flush listeners on every metadata write, a SELECT before every write, and reliance on behaviour ORM 3.7 deprecated. #40 reads and writes the table with DBAL instead (see #39 and ADR-0005 there).

@loevgaard loevgaard closed this Nov 30, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

The metadata persister flushes the application's unflushed changes

1 participant