• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

Singletons in EJB

 
Ranch Hand
Posts: 85
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
hi all,

I am new to ejb and I came from the spring world. However, I have some doubts about the threading policy in ejb.

First, According to best practices, I should not use synchronized blocks in ejb. Then, How do i implement singletons? Or, is it not valid to use singletons in ejb? Suppose I want to use an utility class like HomeLocator. Now, the purpose of this class is to lookup homes and do some caching. I don't want to create a new instance of this class each time i want to look up a home. So I would like to use it as a singleton. Again the block in which it's calling lookup on Context, should also be synchronized since the lookup method is not threadsafe. How do I implement these logics without using singleton and thus synchronized blocks?

Second, I want to use some kind of static method which will lookup a home interface of some ejb, find a particular component interface and do some manipulation. I will call these static methods say from my ejbCreate. Now, what will be the thread safety policy for them? I know the container is supposed to give me thread safety. But how will it give it? Will it synchronize my calls? I am a bit confused here. Should I instead make these kind of methods some instance methods of some class and create a new instance of that class each time I want to call such a method. That will give me thread safety. But This particular method does not use any instance specific information, So according to my logic, it should go into a static method.But then again, I want my calls be performed without any waiting for thread synchronization. What's the solution?

I feel kinda embarrased asking such questions. But hey, according HFEJB, there is no dull question ...



regards,
Sajid Moinuddin
 
Greenhorn
Posts: 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi Sajid,
i guess the following piece of code can address your query.


public class EJBServiceLocator {
private static EJBServiceLocator myServiceLocator;
private static InitialContext context= null;
private static Map homes;

private EJBServiceLocator() {
homes= Collections.synchronizedMap(new HashMap());
}

public static EJBServiceLocator getInstance() throws NamingException {
return getInstance(null);
}

public static EJBServiceLocator getInstance(Hashtable contextParams) throws NamingException {
if (myServiceLocator == null) {
myServiceLocator= new EJBServiceLocator();
if (context == null) {
if (contextParams == null) {
context= new InitialContext();
} else {
context= new InitialContext (contextParams);
}
}
}
return myServiceLocator;
}

public EJBHome getHome(String name, Class clazz) throws NamingException {
EJBHome aHome= (EJBHome) homes.get(name);
if (aHome == null) {
Object objref= context.lookup(name);
aHome= (EJBHome) PortableRemoteObject.narrow(objref, clazz);
homes.put(name, aHome);
}
return aHome;
}

}



Please shoot a mail at ramveersingh@yahoo.co.in if u have any further queries.

cheers
ramveer
 
Ranch Hand
Posts: 8945
Firefox Browser Spring Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Again the block in which it's calling lookup on Context, should also be synchronized since the lookup method is not threadsafe. How do I implement these logics without using singleton and thus synchronized blocks?



I wouldn't bother to make these classes thread safe. It is possible that more than one instance could get created, so what is the harm ? Other option is to implement lazy loading singleton where again no threading issues come into picture.
 
Don't get me started about those stupid light bulbs.
reply
    Bookmark Topic Watch Topic
  • New Topic