Question

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?

Was it helpful?

Solution

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.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top