Skip to content

Adding DoctrineClearEntityManagerWorkerSubscriber to reset EM in worker #34156

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 1 commit into from
Oct 31, 2019

Conversation

weaverryan
Copy link
Member

@weaverryan weaverryan commented Oct 28, 2019

Q A
Branch? 4.4
Bug fix? yes & no :)
New feature? yes
Deprecations? no
Tickets Fix #34073
License MIT
Doc PR symfony/symfony-docs#12575
DoctrineBundle PR doctrine/DoctrineBundle#1043

Hi!

I've seen a few developers get "bit" by an issue recently: after running messenger:consume, the 2nd, 3rd, 4th, etc message that are handled are getting out-of-date data from Doctrine. The reason is simple:

A) Consume the 1st message, it queries for Foo id=1 to do something
B) 10 minutes go by
C) Consume the 2nd message. It also queries for Foo id=1, but because this is already in the identity map, Doctrine re-uses the data that is now 10 minutes old.

Even though one worker process handles many messages, the system should (as much as possible) isolate each handler from each other. This is one very practical place we can help people. Also, checking the code, I don't think clearing the entity manager will cause any issues for an EM whose Connection has not been "connected" yet (i.e. it will not cause a connection to be established).

We would wire this in DoctrineBundle, and could make it disable-able... in case that's something that's needed.

@weaverryan weaverryan requested a review from sroze as a code owner October 28, 2019 17:12
@weaverryan weaverryan force-pushed the reset-entity-manager-messenger-event branch from bf118dd to 9eda8d7 Compare October 28, 2019 17:13
@weaverryan weaverryan added this to the 4.4 milestone Oct 28, 2019
@Tobion
Copy link
Contributor

Tobion commented Oct 28, 2019

Seems fine to me to add this.

@weaverryan weaverryan force-pushed the reset-entity-manager-messenger-event branch from 9eda8d7 to 5c3f338 Compare October 28, 2019 17:27
@bendavies
Copy link
Contributor

👍 I have pretty much exactly this in a project

@Koc
Copy link
Contributor

Koc commented Oct 29, 2019

@weaverryan should doctrine middleware for same functionality be removed? Because now we are having 2 approaches to do same thing

@weaverryan
Copy link
Member Author

@weaverryan should doctrine middleware for same functionality be removed? Because now we are having 2 approaches to do same thing

Excellent question :). You probably know better than me. I personally think this should be done always and has no downside (well, a few micro-seconds added after handling each message to clear)... so there would indeed be no use-case anymore for the "clear" middleware. Do you agree?

@Koc
Copy link
Contributor

Koc commented Oct 29, 2019

yes, event subscriber completely replaces middleware, so we can remove it.

@weaverryan weaverryan force-pushed the reset-entity-manager-messenger-event branch from 5c3f338 to a8fe494 Compare October 31, 2019 17:24
@weaverryan weaverryan force-pushed the reset-entity-manager-messenger-event branch from a8fe494 to e7b9888 Compare October 31, 2019 17:27
@weaverryan
Copy link
Member Author

DoctrineClearEntityManagerMiddleware is gone and docs PR added

@weaverryan
Copy link
Member Author

Failures unrelated - coming from 4.4 branch

@fabpot
Copy link
Member

fabpot commented Oct 31, 2019

Thank you @weaverryan.

fabpot added a commit that referenced this pull request Oct 31, 2019
…eset EM in worker (weaverryan)

This PR was merged into the 4.4 branch.

Discussion
----------

Adding DoctrineClearEntityManagerWorkerSubscriber to reset EM in worker

| Q             | A
| ------------- | ---
| Branch?       | 4.4
| Bug fix?      | yes & no :)
| New feature?  | yes
| Deprecations? | no
| Tickets       | Fix #34073
| License       | MIT
| Doc PR        | symfony/symfony-docs#12575
| DoctrineBundle PR | doctrine/DoctrineBundle#1043

Hi!

I've seen a few developers get "bit" by an issue recently: after running `messenger:consume`, the 2nd, 3rd, 4th, etc message that are handled are getting out-of-date data from Doctrine. The reason is simple:

A) Consume the 1st message, it queries for `Foo id=1` to do something
B) 10 minutes go by
C) Consume the 2nd message. It also queries for `Foo id=1`, but because this is already in the identity map, Doctrine re-uses the data that is now *10* minutes old.

Even though one worker process handles many messages, the system should (as much as possible) isolate each handler from each other. This is one very practical place we can help people. Also, checking the code, I don't think clearing the entity manager will cause any issues for an EM whose Connection has not been "connected" yet (i.e. it will not cause a connection to be established).

We would wire this in DoctrineBundle, and could make it disable-able... in case that's something that's needed.

Commits
-------

e7b9888 Adding DoctrineClearEntityManagerWorkerSubscriber to reset entity manager in worker
@fabpot fabpot merged commit e7b9888 into symfony:4.4 Oct 31, 2019
@weaverryan weaverryan deleted the reset-entity-manager-messenger-event branch November 1, 2019 20:07
OskarStark added a commit to symfony/symfony-docs that referenced this pull request Nov 2, 2019
…eware (weaverryan)

This PR was merged into the 4.4 branch.

Discussion
----------

Removing notes about doctrine_clear_entity_manager middleware

See symfony/symfony#34156

That class was added in 4.4... then removed for 4.4 - so it was never released.

I don't think the new "subscriber" added there needs docs - I'll open a PR in DoctrineBundle so that it's (ideally) enabled by default. I see this more as a bug fix that should... just work without you knowing/caring.

Commits
-------

fdf457a removing notes about doctrine_clear_entity_manager middleware
@fabpot fabpot mentioned this pull request Nov 12, 2019
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

[Messenger] Use Doctrine middleware by default
6 participants