-
Notifications
You must be signed in to change notification settings - Fork 71
Clear the doctrine entity manager in every request #127
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Conversation
Bootstraps/Symfony.php
Outdated
@@ -137,6 +137,7 @@ public function postHandle($app) | |||
if (!$em->getManager()->isOpen()) { | |||
$em->resetManager(); | |||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Else clear sinc reset would surely clear it, too?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Good point. Its changed
+1. Even if it costs performance its surely what an application would expect and is tested for. |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Most Symfony applications use a single entity manager, but don't forget there can be many.
$em->getManager()
is actually a shortcut to $em->getManager('default')
.
To handle all cases we should reset / clear all $em->getManagers()
and rename the $em
variable (the doctrine service is not an Entity Manager itself, but a registry of entity managers).
@bpolaszek would you mind opening a PR or issue lest we forget as this PR is closed? |
Thanks @ddna, sorry did not have enough time last week :) |
I have been trying a lot to find an alternative to this, but unfortunately not clearing Doctrine at the end of the request causes 2 major issues:
Once an entity is fetched from the database, Doctrine will not fetch it again in the next request, but reuse the same entity. This is good for performance, but makes it extremely easy to have inconsistent state, because any changes made to an entity or a child collection will get carried over to the next request, regardless if the entity was persisted or not. This causes bugs like PHP-PM issue with taxons setParent Sylius/Sylius#9629 that are both very hard to track down and to fix.
Because entities are cached and not fetched again in the next request, any changes made to the database from an external source (e.g. a worker or third party script) are not detected by the app, which continues to use the cached version of the entity as long as the worker remains alive.
This PR fixes this by clearing the entity manager at the end of each request, so that fresh queries will be performed in the next.