morphia icon indicating copy to clipboard operation
morphia copied to clipboard

@PrePersist method not invoked sometimes

Open arivazhagan-jeganathan opened this issue 3 years ago • 8 comments

We use @PrePersist to initialize few fields during Save of an entity. This feature seems to be working fine most of the times. But in rare cases, this PrePersist module code is never invoked. Also we dont have the fields initialized. This leads to many processing bugs due to missing field.

  • Server Version: 4.2.x
  • Driver Version: 4.4.0
  • Morphia Version:Using Morphia 2.2.8. [ Same issue happened in 2.2.3 as well]

@evanchooly , please suggest why this would happen. No Errors on console as well. Is there a way to track why this is skipped?

arivazhagan-jeganathan avatar Nov 09 '22 12:11 arivazhagan-jeganathan

It's hard to say without more details so a reproducer might help. You might also make sure you're explicitly mapping all your entities. It shouldn't matter. Morphia should dynamically pick those up along the way but there have been gaps over the years. I think I've plugged them all but I wouldn't rule out a gap that's been missed.

evanchooly avatar Nov 09 '22 15:11 evanchooly

@evanchooly , is there a guideline on how to use EntityListener or PrePersist Annotations. Like, based on code, MorphiaCodecProvider will ensure LifeCycleEncoder be applied. Should CodecRegistry be registered at Database level or at Mapper level? Any documents to go over this please?

arivazhagan-jeganathan avatar Nov 09 '22 15:11 arivazhagan-jeganathan

There's this page in the guide.

evanchooly avatar Nov 09 '22 16:11 evanchooly

Thank you @evanchooly .. Is there a logger [on morphia] which could be used for analysis on why this is missed? Could you please share the areas where this was fixed, so could try to understand code and look out for other similar places

arivazhagan-jeganathan avatar Nov 09 '22 16:11 arivazhagan-jeganathan

I don't have any specific changes to point you to, no, as those were done ages ago. There is also not much in the way of logging in general. If you could isolate a reproducer where this happens that would be immensely helpful.

evanchooly avatar Nov 16 '22 01:11 evanchooly

@evanchooly , it is not consistently reproduceable.

  1. Added @prepersist method on @Entity annotated class. Should the LifecycleEncoder be specified anywhere explicitly during DataStore construction?

it looks like, sometimes, DAO makes use of LifecycleEncoder but sometimes it just uses Normal EntityEncoder. Any insights please on missing configuration stuff?

P.S: have collected heap dump. Anything that can be verified at Entity or DAO level to determine whats wrong?

Heap Dump screen shot of MorphiaCodecProvider for our Entity: image (14)

This show, LifecycleEncoder is registered for the EntityModel. BUt still the lifecycle method was not invoked.

arivazhagan-jeganathan avatar Nov 17 '22 12:11 arivazhagan-jeganathan

Sorry, i dropped the ball on this one. any chance you could provide a reproducer? There's another issue involving lifecycle events and parent classes so you might be up against that one, too.

evanchooly avatar Jan 22 '23 03:01 evanchooly

Confirm issue. Got the same problem. Didn't find any system or method of reproduction.

Ivan-nikitko avatar Dec 11 '23 12:12 Ivan-nikitko