-->
These old forums are deprecated now and set to read-only. We are waiting for you on our new forums!
More modern, Discourse-based and with GitHub/Google/Twitter authentication built-in.

All times are UTC - 5 hours [ DST ]



Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 2 posts ] 
Author Message
 Post subject: Multi Threading issue
PostPosted: Tue May 16, 2006 4:48 pm 
Newbie

Joined: Tue Apr 04, 2006 9:56 am
Posts: 10
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:

_________________
Cheers,
Don't forget to rate the post, Thank You


Top
 Profile  
 
 Post subject:
PostPosted: Thu May 18, 2006 6:38 pm 
Expert
Expert

Joined: Fri Jul 22, 2005 2:42 pm
Posts: 670
Location: Seattle, WA
I suggest placing SessionFactories into JNDI space and access them via your utility classes. As for ThreadLocals variables, yes, it will not work when EJBs live in different JVMs, or in other cases, for example when something gets invoked asynchronously, even within one JVM. So yes, ThreadLocal is not reliable pattern in the EJB space.

_________________
--------------
Konstantin

SourceLabs - dependable OpenSource systems


Top
 Profile  
 
Display posts from previous:  Sort by  
Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 2 posts ] 

All times are UTC - 5 hours [ DST ]


You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum

Search for:
© Copyright 2014, Red Hat Inc. All rights reserved. JBoss and Hibernate are registered trademarks and servicemarks of Red Hat, Inc.