Skip to content
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

Update in-memory collections before dispatching the postRemove event #11132

Closed
wants to merge 1 commit into from

Conversation

mpdude
Copy link
Contributor

@mpdude mpdude commented Dec 22, 2023

Make sure in-memory collections have removed entities unset before the postRemove event is dispatched. This is related to #11098, although by itself probably not going to fix the regression.

Background

When an entity is removed from the database, the UoW also takes care of removing that entity from all in-memory collections. #10763 moved this cleanup and taking snapshots of updated collections to a very late stage during the commit phase, in order to avoid other side effects.

Now, part of the issue in #11098 is that postRemove event listeners will be called at a point where the database-level DELETE has happened (although the transaction has not yet been committed), but the in-memory collections have not yet been updated.

Suggested change

This PR moves the code part that unsets removed entities from in-memory collections and takes collection snapshots (makes collections "clean") from after transaction commit to before the postRemove event. That brings the in-memory update closer to the database-level execution.

In the case of a transaction failure/abort, this leaves us with updated and snapshotted in-memory collections, but no database-level updates. But, at this point, probably things got inconsistent anyways, the EM will be closed and we need not be too worried about the state.

On the other hand, it might not be worth the effort to have clean/consistent collections at postRemove time, since that still leaves us with dirty/inconsistent states during postPersist or postUpdate.

@mpdude mpdude force-pushed the update-collections-before-postRemoveEvent branch 2 times, most recently from efd74f1 to 90b1735 Compare December 22, 2023 11:05
@mpdude
Copy link
Contributor Author

mpdude commented Dec 22, 2023

What happens when we DELETE entities from the database, remove them from in-memory collections as well and then the transaction fails?

Is a transaction failure something that leaves us in an inconsistent state anyway, thus closing the EM?

@mpdude mpdude marked this pull request as draft December 23, 2023 14:26
@beberlei
Copy link
Member

Is a transaction failure something that leaves us in an inconsistent state anyway, thus closing the EM?

Yes

@mpdude
Copy link
Contributor Author

mpdude commented Jan 2, 2024

@beberlei

After your comment #11098 (comment), would you say that we should close this PR here and not try to change or fix anything?

This means in-memory collections might still contain removed entities at the time postRemove is called (and additionally, be in a dirty state), whereas the database-level operations have been executed already, but the transation has not been committed yet.

@mpdude mpdude force-pushed the update-collections-before-postRemoveEvent branch from 90b1735 to 88d5a02 Compare January 2, 2024 10:00
@mpdude mpdude marked this pull request as ready for review January 2, 2024 10:00
@mpdude mpdude force-pushed the update-collections-before-postRemoveEvent branch from 88d5a02 to 0808234 Compare January 2, 2024 10:08
…e `postRemove` event is dispatched. This is related to #11098, although by itself probably not going to fix the regression.

#### Background 

When an entity is removed from the database, the UoW also takes care of removing that entity from all in-memory collections. #10763 moved this cleanup and taking snapshots of updated collections to a very late stage during the commit phase, in order to avoid other side effects.

Now, part of the issue in #11098 is that `postRemove` event listeners will be called at a point where the database-level `DELETE` has happened (although the transaction has not yet been committed), but the in-memory collections have not yet been updated.

#### Suggested change

This PR moves the code part that unsets removed entities from in-memory collections and takes collection snapshots (makes collections "clean") from after transaction commit to before the `postRemove` event. That brings the in-memory update closer to the database-level execution.

In the case of a transaction failure/abort, this leaves us with updated and snapshotted in-memory collections, but no database-level updates. But, at this point, probably things got inconsistent anyways, the EM will be closed and we need not be too worried about the state.
@mpdude
Copy link
Contributor Author

mpdude commented Jan 12, 2024

Closing this.

As of #11146, the documentation is more explicit about the in-memory collection states. For other events, this kind of consistency isn't achieved either, so I am not sure it's worth bothering.

@mpdude mpdude closed this Jan 12, 2024
@mpdude mpdude deleted the update-collections-before-postRemoveEvent branch January 12, 2024 21:42
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.

2 participants