Skip to content

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

Merged
merged 2 commits into from
Aug 24, 2018
Merged

Conversation

dnna
Copy link
Contributor

@dnna dnna commented Aug 23, 2018

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.

@@ -137,6 +137,7 @@ public function postHandle($app)
if (!$em->getManager()->isOpen()) {
$em->resetManager();
}
Copy link
Contributor

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?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Good point. Its changed

@andig
Copy link
Contributor

andig commented Aug 23, 2018

+1. Even if it costs performance its surely what an application would expect and is tested for.

@andig andig merged commit 6d1da1a into php-pm:master Aug 24, 2018
Copy link

@bpolaszek bpolaszek left a 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).

@andig
Copy link
Contributor

andig commented Aug 27, 2018

@bpolaszek would you mind opening a PR or issue lest we forget as this PR is closed?

@bpolaszek
Copy link

Thanks @ddna, sorry did not have enough time last week :)

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.

3 participants