Hello, please read my post and give me some insight on this "strange" behavior. I am in the learning stage of understanding the difference between stateful and stateless session beans. Here is what I did:
I've created a stateLESS, container managed session bean; in the bean class there is a private int testInt variable, and there's a public void increment() method that increases testInt by 1 and then prints its value using System.out.println(testInt). This method is properly declared in the remote interface. In the ejbCreate, ejbRemove, setSessionContext, etc methods, there's System.out.println("ejbCreate"), System.out.println("ejbRemove"), System.out.println("setSessionContext") statements for tracing/debugging purpose.
My ejb-jar.xml file looks like
Transaction attributes for 'TestBean' methods
Then the client does the usual lookup("TestBean") without a hitch, and calls the increment() method several times. The interesting thing is, on the JBoss console, I see
ON THE OTHER HAND, if I change the <session-type> attribute from stateLESS to stateFUL, and keep the rest the same, I see the following on the JBoss console when I make several calls to the increment() method:
Don't stateful and stateless session beans have their meanings inverted???
Please please help me understand this. Thank you so much :)
I don't know if I should post all the codes, since it will take up extra space on the forum.
No, not a bug. Correct behavior.
It's most likely you misunderstand what SFSBs and SLSBs are.
This requires a longer explanation.
For stateless session beans, JBoss uses a pool of beans and supplies the client with any instance. In a idle system (normal developer mode), JBoss will create a pool with one instance and give you that instance on each call.
Your client does a create() and then an increment() four times. So JBoss creates an instance on the first call, and then reuses it three times.
That is why the variable is incremented. But, on a busy system, there would be more than one instance and you would get a different instance on each call. You would no longer see the apparent "stateful" behaviour.
On the stateful session bean, you are again doing a create() and then an increment(). But, in this case, each create() creates a new instance of the Stateful Bean. If you really want to use a Stateful Session Bean, you should create it only once from the client and then increment it four times.
Thank you so much James, I think you are totally right.
I did some further testing by doing System.out.println(this); in the bean class, so that I can see the bean's memory location on the jboss console.
For a slsb, the memory location has been always the same across multiple create() calls even from two different pc's and calls made several hours apart. This confirms that jboss only keeps one instance in the pool in my near-idle environment.
On the other hand, if I change the bean to a sfsb in ejb-jar.xml, System.out.println(this) prints a different memory location with each create() call! This behavior again matches your description :)
Thank you again James, now I think I got it!
I've never worked with Stateful EJBs and I've got a doubt:
you said "if you really want to use a Stateful Session Bean, you should create it only once from the client and then ..."
what happens if client is a servlet?: you cannot store EJB as a Servlet's attribute since Servlet is multithreaded; so you need to call objEJBSF = objEJBSFHome.create() every time you need to process a request. However, if a new instance is created every time then you lose all the information you had stored in EJB object; which could be the solution?
thanks in advance