0
votes

In my models, I have a few methods which are more complex and can throw exceptions. When these exceptions occur, they can leave the model in an inconsistent state. This situation has never been a problem so far, because the controller was aware of this behaviour and never used/persisted an inconsistent entity in the datastore - that model's instance was usually left alone for the garbage collector.

However, while reading the Objectify's documentation, I noticed that the session cache holds the exact same Java objects that are returned to the application. Can this behaviour ruins my assumptions of inconsistent entities being discarded?

Is this the expected behaviour? From now on, I must always leave all my model's instances in a consistent state, no matter what. Because they can all be implicitly saved in the cache. If that's the case, I must review all the methods that throw exceptions, even the non-checked exceptions, to ensure that my model's instance are always consistent and usable.

And also, can this be a problem with multi-threaded java applications? If a user opens two browser tabs, and happens to make two requests which will access the same object in the cache. Is that possible? Am I required to make all my model's classes thread-safe?

How do you guys work-around these issues?

Thank you.

1

1 Answers

0
votes

Objectify sessions != user sessions. Ofy sessions are a session of work; they should never be shared across threads and should be created and thrown out as you need them. They are similar to JPA sessions in this regard.