Reputation: 11337
Few facts regarding the setup:
Session
(at TLS)ObjA
may be created by thread A and later be manipulated by thread B and even later be updated by thread A and yet again be manipulated by thread Z but this time using HQL!Having the above setup I'm getting errors such as:
org.hibernate.HibernateException: illegally attempted to associate a proxy with two open Sessions
org.hibernate.StaleObjectStateException: Row was updated or deleted by another transaction (or unsaved-value mapping was incorrect)
My understanding is that
Session
, being that a session "owns" the objects "attached" to it (those it created/loaded)The question is obvious:
Session
every object after fetching it from the datastore, and attaching it to a Session
later just before updating it would be the ideal way to handle it?I could of course avoid all of it if I'd use only one Session
object but that sounds so... blocking (thread-wise).
I'm sure these sort of issues were solved long time ago, just need to find it (without re-inventing the wheel).
Upvotes: 1
Views: 4784
Reputation: 2327
The object is attached to a Hibernate session, yes. It can only be attached to one at a time, yes. Unless you close a Hibernate session, you could try to evict()
the object, thus detaching it from the session, making it possible to load it in another thread. Problem is, that this may be unpractical, depending on your program flow.
Upvotes: 1