The question is in the context of Grails GORM programming, but it would be interesting to know the situation with this in other Hibernate uses, if there are indeed any real differences.
When one tries to save an object which was not selected with locking, one can get a StaleObjectStateException, if the database object was simultaneously updated by another thread/application. Hibernate docs say that after such an exception the whole session is unusable, because this exception may somehow mess up the session state. It seems that every object which was opened in this session also becomes unusable, even if such object was in fact not even modified by another thread. It seems that one cannot even .refresh()/modify/.save() the object in the same session either, even though logically that should work.
So what is the deal with the session breaking and is there a preferred way to deal with it? Re-selecting the object itself is not a problem, but the fact that every other object selected in that session becomes unusable is very problematic. I have a complicated method which loads a lot of these objects and keeps them in memory in different places, how do I go about handling this after an exception? I don't want to just rollback everything the method has done when one of the objects gets this exception, the logic is smart enough to just handle it, if there was a way to keep the session/objects operational. Does one really have to go through every single object in memory and somehow refresh it just because one of them was updated by another thread?
Is a StatelessSession something to consider?
Also maybe I am missing something here? There is a plethora of quesions about StaleObjectStateException, and almost none of them even list this as a concern? Isn't the fact that any object selected in the same session can randomly become unusable if just a single one of them was updated in the background a big goddamn inconvenience?