0

I'm reading about JSF2 and Managed Beans. I've got a question related to passivation.

I've recently asked for different use cases of @Stateful EJB's, @SessionScoped and @ManagedBean here:

sessionscoped managed bean vs stateful ejb.

Now, stateful EJB's are eligible for passivation and activation which allow them to be temporarily passivated to persistent storage to decrease memory use when they are idle, I haven't seen this feature available to managedbeans. So it got me thinking that maybe I should go for @RequestScoped Managed Beans and prefer @Stateful EJB's for shopping carts etc instead. Using @Sessionscoped Managed Beans only to store minimal user information.

Is this correct? Are there some guidelines for this?

Community
  • 1
  • 1
arg20
  • 4,893
  • 1
  • 50
  • 74

1 Answers1

2

A request scoped managed bean wouldn't work here. To access a particular stateful session bean instance, you need its stub.

If you were to use request scoped managed beans, there would be no place to store this stub and you would get a new instance with every request. This completely beats the reason for using stateful session beans in the first place.

You could however use a view scoped JSF managed bean (if the action takes place on a single page) or a conversation scoped CDI bean (if the action takes place on multiple pages). Especially with the latter you can tie the scope of your conversation to the life-time of the stateful session bean.

Do note that all of this requires at least an intermediate understanding of Java EE. If you're not careful with passivation of stateful session beans (e.g. never call an @Remove annotated method) it will gradually eat away the HDD space of your server.

Arjan Tijms
  • 37,782
  • 12
  • 108
  • 140
  • So you are suggesting I go for conversation scoped beans instead of a session bean right?. I thought of that. But even though now I have a much better understanding of stateful EJBs, they seem more and more useless everytime. Except for the extended persistence context, I cannot think of a use for them in a web application. – arg20 Feb 14 '11 at 00:44
  • The extended persistence context is key when you need to maintain (optimistic) JPA locks on an entity, e.g. to ensure that when an item has been placed in a shopping cart, the last one isn't sold to another customer at the same time. – Arjan Tijms Feb 14 '11 at 08:38
  • 1
    I didn't btw suggest to go for a conversation scoped bean instead of a stateful session bean, but rather to combine them. – Arjan Tijms Feb 14 '11 at 08:39