public class Test1(){
public vod method1(){
try{
Hashtable<String, String> env = new Hashtable<String, String>();
env.put(Context.INITIAL_CONTEXT_FACTORY, EJB_JNDI_FACTORY);
env.put(Context.PROVIDER_URL, EJB_URL);
InitialContext ctx = new InitialContext(env);
LogSearchRemote logSearchRemote = (LogSearchRemote) ctx.lookup(LOG_SEARCH_EJB_BINDNAME);
System.out.println("logSearchRemote = " + logSearchRemote);
logSearchRemote.setTest(5);
System.out.println("logSearchRemote.getTest() = " + logSearchRemote.getTest());
System.out.println("logSearchRemote.getTestAgain() = " + logSearchRemote.getTestAgain());
LogSearchRemote logSearchRemote2 = (LogSearchRemote) ctx.lookup(LOG_SEARCH_EJB_BINDNAME);
System.out.println("logSearchRemote2 = " + logSearchRemote2);
System.out.println("logSearchRemote2.getTest() = " + logSearchRemote2.getTest());
System.out.println("logSearchRemote2.getTestAgain() = " + logSearchRemote2.getTestAgain());
this.session = session;
session.setAttribute("LogSearchEJB", logSearchRemote);
System.out.println("logSearchRemote = " + logSearchRemote);
}catch(Exception e){
e.printStackTrace();
}
// if @stateless, throw exception "$Proxy53 cannot be cast to hk.gov.ehr.service.tch.als.admin.logsearch.ejb.LogSearchRemote"
// if @stateful, no error!!
LogSearchRemote logSearchRemote = (LogSearchRemote)session.getAttribute("LogSearchEJB");
//.....
}
}
1) for the above code, if LogSearchRemote implementation bean is stateful, then
LogSearchRemote logSearchRemote = (LogSearchRemote)session.getAttribute("LogSearchEJB");
has no error, but if LogSearchRemote implementation bean is stateless, then exception "$Proxy53 cannot be cast to hk.gov.ehr.service.tch.als.admin.logsearch.ejb.LogSearchRemote" is thrown, why?
2) for stateful session bean, I find each time the
LogSearchRemote logSearchRemote = (LogSearchRemote) ctx.lookup(LOG_SEARCH_EJB_BINDNAME);
return different logSearchRemote implementation bean,
but if stateless session bean, each time the
LogSearchRemote logSearchRemote = (LogSearchRemote) ctx.lookup(LOG_SEARCH_EJB_BINDNAME);
return the same bean!!
why is this case?
I expect stateless session bean should not keep state and each lookup should return a different implementation bean.
@Stateless(name = "AlsAdminLogSearch_1_0", mappedName = "ejb/AlsAdminLogSearch_1_0")
public class LogSearchBean implements LogSearchRemote{
private int test;
@Override
public void setTest(int value){
test = value;
}
@Override
public int getTest(){
return test;
}
@Override
public int getTestAgain(){
return test;
}
//...methods
}
3) when I call
logSearchRemote.setTest(5);
System.out.println("logSearchRemote.getTest() = " + logSearchRemote.getTest());
System.out.println("logSearchRemote.getTestAgain() = "
logSearchRemote.getTestAgain());
for stateless session bean, the getTest() and getTestAgain() can remeber the instance variable "test" in previous method call!!
Why will it remember? stateless session bean is not supposed to call different EJB instance for each method call?