Removing / destroying a session-driven CDI bean

I have a session driven CDI bean:

@Named @SessionScoped public class SampleBean implements Serializable { // ... } 

I need to remove this bean from the session after a specific thread for which I used the following code, as shown in this answer :

 ExternalContext ec = FacesContext.getCurrentInstance().getExternalContext(); ec.getSessionMap().remove("sampleBean"); 

However, it does not work, and the SampleBean remains in the session.
Did I miss something?

+4
source share
2 answers

Unlike JSF managed beans, CDI managed beans are not saved directly by their managed bean on the session map. Instead, they are stored in server memory using the CDI manager implementation (Weld, OpenWebBeans, etc.), using, for example, the session identifier as a key.

So the trick you used there is not applicable to managed CDI beans. You need to look for an alternative approach. The correct approach in this particular case is to use @ConversationScoped instead of @SessionScoped . Properly designed web applications should never need to manually close an area. So using @SessionScoped for conversation / thread was already wrong in the first place.

+8
source

And this one ?

FacesContext .getCurrentInstance () .getApplication () .createValueBinding ("# {yourBeanName}"). setValue (FacesContext.getCurrentInstance (), null);

-one
source

All Articles