Hi All,
We are trying to convert some of the classes in our application to EJBs so that multiple threads (EJBs) can be used to retrieve the data. But we are running in to multithreading issue. I need your advice to fix this issue.
I have 5 SessionFactories. I store this SessionFactories in HashMap, which is a static variable in the HibernateUtil class. This HibernateUtil class uses the Thread Local pattern to return Session(As explained in the Hibernate in Action.)
HibernateUtil is an instance variable in the stateless Session Bean. Now say I have multiple EJB objects in EJB pool, all of them have their own HibernateUtil object. But inside HibernateUtil, I have static HashMap to hold the different session factories.
My first concern is that, I have seen people advising not to use ThreadLocal with EJbs. Is this true??
In the above case, pool of EJBs holding reference to a static HashMap, When two or more instances of the EJBs are trying to retrieve (hashMap.get() ) SessionFactory from the HashMap, There is going to be multithreading issue because HashMap get() method is not thread safe.
Assume If there is no threading issue in the above case, say that more than one EJBs retrieve the same SessionFactory and they call the getSession(), this call will also not thread safe call…
I need your advise in how to overcome this threading issues. Hope you can suggest me a way to overcome this issue.
Thank You
D
Need help with Hibernate? Read this first:
http://www.hibernate.org/ForumMailingli ... AskForHelp
Hibernate version:
Mapping documents:
Code between sessionFactory.openSession() and session.close():
Full stack trace of any exception that occurs:
Name and version of the database you are using:
The generated SQL (show_sql=true):
Debug level Hibernate log excerpt: