Wednesday, August 1, 2018

Hibernate Objects,Sessions across threads - Illegal attempt to associate a collection with two open sessions

In the land of Hibernate a concept of Session is important.
it plays the role of tracking changes that happen to (hibernate objects) from the moment you open it.
and you can't actually do much without it, it's your entry to hibernate capabilities

so if you want to load an object you have to obtain a session instance first then load the object:

Session s = sessionFactory.getCurrentSession();
MyObject mo = s.byId(MyObject.class).load(123);

now that object (mo) is associated with that session
if you do something like

mo.setName('new Name');


the session will track that this object is dirty. and it does much more that you can read about.

What I want to get to is that in case you have to do some background work with your object you have to be careful that:
1- the session is not thread safe (i.e. it can't be passed between threads and assume no concurrency issues will happen)
2- the objects associated with one session cannot be associated with another open session

so if you try to do something like this:

// this is psudo code, just to get the point:\
MyObject mo = dao.load(123);
new Thread( () -> {
     mo.setName('abc');
     dao.update(mo);
}).run();

// Data access methods (usually in a dao)
MyObject load(int id) {
  return   sessionFactory.getCurrentSession().byId(MyObject.class).load(123);
}

void update(MyObject m) {
   Session s = sessionFactory.getCurrentSession();
   s.save(m);
}

that won't work, and hibernate will complain :
Illegal attempt to associate a collection with two open sessions

because we passed the object between two threads while the first session could still be open when the second other thread starts.
and what makes it worse is that you probably won't see the exception message because the other thread won't stop your main thread execution and you will never know why the update failed.

so as a good practice is to have good error handling in background threads.


I had to learn this the hard way after debugging something similar where my update had to be done in background and spent few hours debugging a ghost issue without clue until I placed some std out /err good old print statements because debugging multiple threads can be hard when there is alot of moving parts.

Solution:
1- don't pass hibernate object
2- pass the id of the object instead / or anything to reload it fast in the other thread, that way it will not be considered the same object.
 

// transaction A
MyObject mo = dao.load(123); <-- will work in session 1
//do stuff with mo

new Thread( () -> {
     // transaction B
     // reload the hibernate object and don't use the one from other thread
     MyObject mo2 = dao.load(123); <-- this will be associated with session 2

     mo2.setName('abc');
     dao.update(mo2);
}).run();

another solutions involve using evict(); and merge(); 

Cheers !

read more:
1- https://developer.jboss.org/wiki/Sessionsandtransactions?_sscc=t
2-https://stackoverflow.com/questions/11673303/multithread-hibernateexception-illegal-attempt-to-associate-a-collection-with
3- https://stackoverflow.com/questions/11237924/hibernate-multi-thread-multi-session-and-objects


No comments:

Post a Comment

Note: Only a member of this blog may post a comment.

Istio —simple fast way to start

istio archeticture (source istio.io) I would like to share with you a sample repo to start and help you continue your jou...