EJB example for stateless and stateful beans difference?

You can use instance variables of a stateless session bean, but they're not guaranteed to be preserved across method calls. If both approaches behave the same, it simply means you're probably getting the same stateless session bean instance across method calls within the same session.

So if there were more Stateless bean instances in the pool, that would be another story... – Evgeny Oct 30 at 17:53 Well, it depends on which one gets selected. I guess it'd make sense to keep one from a pool for a session within a certain time frame, or only return it if there's high demand on the pool. This could also depend on the application server that's being used.

The only thing that matters is not to rely on the state of a stateless bean outside of one use. – G_H Oct 30 at 17:56 I understand, thanks. – Evgeny Oct 30 at 18:06.

Your first example returns the previously set number by chance only : the get method could have been invoked on a different bean instance. It just happened to use the same instance in this particular case, but you can't count on it. The second one is guaranteed to return you the previously set number, provided another request doesn't call the set method before the get method is called.

A stateful bean should not be injected into a servlet.

3 Just to add a rationale behind 'SFSB should not be injected into a servlet' - because the servlet instance is created only once for all requests, you will end with only one SFSB injected into the servlet. If you want 1 SFSB per user you should do JNDI lookup for every new user and store a reference to it in the user session. Doing the JNDI lookup for SFSB is guaranteed to return a fresh instance each time.

– Piotr Nowicki Oct 30 at 21:12 +1 for the remark that a SFSB should not be injected into a Servlet. – Arjan Tijms Oct 31 at 12:44.

You are not supposed to have member fields (aka state) in a stateless session bean. For a stateful session bean it's ok. That's the difference.

3 You may have state in a stateless bean. For example, references to other stateless beans. What you may not have is conversational state.

– JB Nizet Oct 30 at 17:50.

I cant really gove you an answer,but what I can give you is a way to a solution, that is you have to find the anglde that you relate to or peaks your interest. A good paper is one that people get drawn into because it reaches them ln some way.As for me WW11 to me, I think of the holocaust and the effect it had on the survivors, their families and those who stood by and did nothing until it was too late.

Related Questions