I have a few Servlets which call remote EJB session to manage user's requests. At a first time I created a new manager in each method this way:
ManagerAdminRemote managerAdmin;
Context jndiContext = new javax.naming.InitialContext();
Object ref = jndiContext.lookup("ManagerAdmin/remote");
managerAdmin = (ManagerAdminRemote) PortableRemoteObject.narrow(ref, ManagerAdminRemote.class);
To avoid this repetition, I implemented the init
method like this:
public void init(ServletConfig config) throws ServletException {
super.init(config);
try {
Context jndiContext = new javax.naming.InitialContext();
Object ref = jndiContext.lookup("ManagerAdmin/remote");
managerAdmin = (ManagerAdminRemote) PortableRemoteObject.narrow(ref, ManagerAdminRemote.class);
} catch (NamingException e) {
e.printStackTrace();
}
}
With managerAdmin
declared as class attribute.
Session Bean ManagerAdmin
is stateless.
I'm using JBoss 5 and I saw that init()
is called the first time the Servlet is called. But I also noticed that all the users share the same objects declared as class attribute. So this way, for instance, different users will share the same managerAdmin
.
For now I didn't encounter any problem, but I'm asking: could this sharing bring problems of any kind? Delays? Or, since managers are stateless, is it fine?
Thanks in advance.