Is it safe to access EJB home object from multiple threads?
I have read this thread: J2EE/EJB + service locator: is it safe to cache EJB Home lookup result ? I use the same approach, i.e. I obtain EJB home object for my entity bean and cache it in a servlet.开发者_开发百科
My question is: is it safe to share this object between multiple threads?
From EJB 2.1 spec I found only that concurrent calls to entity beans [via local / remote interface] are serialized internally by the container. However, the spec doesn't expand on concurrent calls to home objects.
Does anybody have an idea? The reference to the exact place in a spec / doc would be very welcome as well.
EJBHome and EJBObject are equally thread safe. The container takes all responsibility for the thread safety of those implementations.
Very often an app server will create one instance of a bean's EJBHome or EJBLocalHome and tie it directly into JNDI for all the application to share. I bet if you looked up your EJBLocalHome twice from inside a servlet and did an == compare on the two, there'd be good odds that it was the exact same instance.
Besides technical safety, there's the matter of mental safety.
Taking that into account, every usage of EJB 2.1's home objects should be considered unsafe. You'll be much better of looking into the much saner EJB 3 approach than wasting any time with EJB 2.x.
I don't think EJBHome is thread safe because
First to get EJBHOme Object we get the help of Synchronised Object such as Properties and Hashtable
Second if we implement BusinessDeleigate Design Pattern to cache EJBHome Object we are using Synchronised Map to store the EJBHome. So at a time only one thread can access to EJBHome.
精彩评论