views:

127

answers:

2

I've a stateless session bean in an ejb container. If I invoke it from a jsf2 form it works fine, but if I recall the form again it shows me the same data I've inserted before. It happens even if I close and reopen the browser. I must wait several minutes until the form shows empty fields. The stateless session bean is not recreated for each request. It behaves like a statefull one. What's the wrong?

Application server: Glassfish 3.0.1

A: 

The stateless session bean is not recreated for each request.

this is not a correct term. there is no guaranty to create new instance of stateless bean for each request.

container will pool some instances of stateless bean for performance reason.

the number of pooled instances depends on container configuration.

mohammad shamsi
Why pool if stateless session beans can't have state? I expect they are recreated for each request. Otherwise what's the difference between stateless and stateful session bean? Thanks
Alf
pooling is for performance reason.the difference between stateful and stateless is that stateless beans are shared between users but stateful bean is belong to a user until the end of lifecycle.google "stateful and statelss session bean" to find more about them.
mohammad shamsi
Wow! My original problem was: EJB3 + Struts2. Struts create new session bean for each request even if it is stateful. Now I discover that JSF2 reuse the same session bean for each request even if it is stateless. Amazing! I hope exists a StrutsFaces2, in medio stat virtus :)
Alf
its not related to struts or jsf. they won't decide about number of ejb instances.
mohammad shamsi
A: 

My original problem was: EJB3 + Struts2. Struts create new session bean for each request even if it is stateful.

Struts doesn't do anything by itself, it only does what you tell him to do. And I suspect that you're just misusing things. Showing some code to illustrate the problem might help.

Now I discover that JSF2 reuse the same session bean for each request even if it is stateless.

This is not true in general, you can get any instance of a stateless session bean (SLSB). And even if for some reasons you get the same instance in your particular situation (maybe because of the beans pool configuration), this shouldn't be an issue at all when using SLSB, you should not care of what instance you get and certainly not rely on the state of instances (since they're stateless).

Back to your question, I suspect that you're misusing SLSB and expecting things that are not true:

  • Don't expect to get newly initialized instances between calls, that's not what stateless means.
    • Actually, don't expect anything about the instance you'll get.
  • Don't rely on the state of a SLSB instance (they are stateless, you're not supposed to rely on state).
    • Don't rely on instance variable between remote calls.
    • Actually, avoid using instance variables, there is probably no need for them.

Related questions

Pascal Thivent
Yes, I admit, I misuse :) If I inject SLSB into managed bean annotated with @RequestScoped, SLSB is recreated for each request. I thought I could use per request SLSB directly without a redundant managed bean.
Alf