File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes EJB and other Java EE Technologies and the fly likes Singletons in EJB Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Murach's Java Servlets and JSP this week in the Servlets forum!
JavaRanch » Java Forums » Java » EJB and other Java EE Technologies
Bookmark "Singletons in EJB" Watch "Singletons in EJB" New topic
Author

Singletons in EJB

Sajid Moinuddin
Ranch Hand

Joined: Mar 19, 2005
Posts: 85
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
Ramveer Singh
Greenhorn

Joined: Apr 20, 2006
Posts: 1
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
Pradeep bhatt
Ranch Hand

Joined: Feb 27, 2002
Posts: 8903

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.


Groovy
 
wood burning stoves
 
subject: Singletons in EJB
 
Similar Threads
a singleton issue.
Singletons in EJB
Can synchronization completely guarantee thread safety?
Singletons and memory leaks
Access specifiers for constructors