hi!
I am sorry if i confuse you further... but this is what i understood ... any body feels my understanding is wrong please correct me.....(even i am on the verge of preparing fot the SCBCD)
I was also having the same question in mind when i was trying to practically implement to see the difference between the stateless and stateful. What i understood after studying the HF EJB and going back to the code is .....
1. It is true that the Stateless session beans will not save the conversational state between the two method calls. Which does not and need not directly imply that you cannot have member variables if the bean is stateless. you can have the member variables which because of the nature of most of the containers to maintain a pool for the stateless session beans, will work as static members. i.e..,as long as the same instance of the bean is being used between different calls you will see the value of the variable i changing every time you make a call.
This is because the container is given the definition that the bean is stateless and so it does not care about what is being stored in the bean and all it does is go and get the copy of the bean that is available. so if you are making calls to the bean in a sequence then there is a high possibility that same bean instance is being used for both the calls. so your variable value is retained from previous call and so it is incremented.
but the same thing if you declare the bean as stateful bean then the container makes sure the same copy of the bean is not being given to another client as now it has to maintain the state and so you will not see the value changing across different bean calls.
I dont know if i made it even more confusing .... but i will give you a piece of code which i was using to understand what is happenning.
////////////////////// My Public Methods /////////////////
public int square(int i){
return i * i;
}
public int add(int i, int j){
return i + j;
}
public int multiply(int i, int j){
return i * j;
}
public int multiplya(int a,int b){
product = a * b;
System.out.println("inside the bean. " + product);
return product;
}
public int getProduct(){
return product;
}
private int product;
This is the code same code that i have added both in the stateful and stateless bean
// Stateless Bean Client.
public static void main(
String args[]) {
try{
InitialContext ctx = new InitialContext();
Object objRef = ctx.lookup("ejb/com/sbc/test/MySessionBeanHome");
MySessionBeanHome homeObj = (MySessionBeanHome)PortableRemoteObject.narrow(objRef,MySessionBeanHome.class);
MySessionBean actualBean = homeObj.create();
System.out.println("Hello the prod of 2,3 " + actualBean.multiplya(2,3));
objRef = ctx.lookup("ejb/com/sbc/test/MySessionBeanHome");
homeObj = (MySessionBeanHome)PortableRemoteObject.narrow(objRef,MySessionBeanHome.class);
MySessionBean actualBean1 = homeObj.create();
System.out.println("Hello the prod of 2,4 " + actualBean1.multiplya(2,4));
System.out.println("Hello the prod of 2,4 get product" + actualBean1.getProduct());
System.out.println("Hello the prod of 2,3 get product" + actualBean.getProduct());
}catch(javax.naming.NamingException ne){
ne.printStackTrace();
}catch(java.rmi.RemoteException re){
re.printStackTrace();
}catch(javax.ejb.CreateException ce){
ce.printStackTrace();
}
}
// stateful client.
public static void main(String args[]) {
try {
InitialContext ctx = new InitialContext();
Object objRef =
ctx.lookup("ejb/com/sbc/MyStateFulBean/StatefulHome");
StatefulHome homeObj =
(StatefulHome) PortableRemoteObject.narrow(
objRef,
StatefulHome.class);
Stateful myBean = homeObj.create();
System.out.println("Hello the prod of 2,3 " + myBean.multiply(2,3));
objRef = ctx.lookup("ejb/com/sbc/MyStateFulBean/StatefulHome");
homeObj = (StatefulHome)PortableRemoteObject.narrow(objRef,StatefulHome.class);
Stateful actualBean1 = homeObj.create();
System.out.println("Hello the prod of 2,4 " + actualBean1.multiply(2,4));
System.out.println("Hello the prod of 2,4 get product" + actualBean1.getProduct());
System.out.println("Hello the prod of 2,3 get product" + myBean.getProduct());
} catch (javax.naming.NamingException ne) {
ne.printStackTrace();
} catch (java.rmi.RemoteException re) {
re.printStackTrace();
} catch (javax.ejb.CreateException ce) {
ce.printStackTrace();
}
}