Cattani Simone
Cattani Simone

Reputation: 1218

JSF logout with ViewScoped bean

In my application I have a top bar with a logout button that call the bean method

public String logout(){
    FacesContext.getCurrentInstance().getExternalContext().invalidateSession();
    return Navigator.goTo("/index.xhtml");
}

it works well, but I have a specific page where it fails with this exception

2014-01-23T15:17:42.405+0100|WARNING: StandardWrapperValve[Faces Servlet]: Servlet.service() for servlet Faces Servlet threw exception
java.lang.NullPointerException
    at org.jboss.weld.context.beanstore.http.AbstractSessionBeanStore.getLockStore(AbstractSessionBeanStore.java:120)
    at org.jboss.weld.context.beanstore.AttributeBeanStore.lock(AttributeBeanStore.java:219)
    at org.jboss.weld.context.AbstractContext.get(AbstractContext.java:97)
    at org.jboss.weld.context.PassivatingContextWrapper$AbstractPassivatingContextWrapper.get(PassivatingContextWrapper.java:64)
    at org.jboss.weld.bean.proxy.ContextBeanInstance.getInstance(ContextBeanInstance.java:93)
    at org.jboss.weld.bean.proxy.ProxyMethodHandler.invoke(ProxyMethodHandler.java:79)
    at org.omnifaces.cdi.viewscope.ViewScopeManager$Proxy$_$$_WeldClientProxy.preDestroyView(Unknown Source)
    at org.omnifaces.application.ViewScopeEventListener.processEvent(ViewScopeEventListener.java:56)
    at javax.faces.event.SystemEvent.processListener(SystemEvent.java:108)

The only difference between this page and the other is that this one use a ViewScoped Bean, but I don't understand how this could be a problem. solutions? thanks

Upvotes: 2

Views: 1482

Answers (2)

ontime
ontime

Reputation: 123

I had a similar problem with NullPointerException being thrown using Wildfly9.

When a user logs in into our system we log him in, and then we invalidate his previous sessions. The problem is that when we use a CDI SessionScoped bean then Wildfly is giving this Stacktrace:

java.lang.NullPointerException at org.jboss.weld.context.beanstore.http.AbstractSessionBeanStore.getLockStore(AbstractSessionBeanStore.java:112) [weld-core-impl-2.2.16.SP1.jar:2015-09-16 08:49] at org.jboss.weld.context.beanstore.AttributeBeanStore.lock(AttributeBeanStore.java:209) [weld-core-impl-2.2.16.SP1.jar:2015-09-16 08:49] at org.jboss.weld.context.AbstractContext.get(AbstractContext.java:90) [weld-core-impl-2.2.16.SP1.jar:2015-09-16 08:49] at org.jboss.weld.context.PassivatingContextWrapper$AbstractPassivatingContextWrapper.get(PassivatingContextWrapper.java:76) [weld-core-impl-2.2.16.SP1.jar:2015-09-16 08:49] at org.jboss.weld.bean.ContextualInstanceStrategy$DefaultContextualInstanceStrategy.get(ContextualInstanceStrategy.java:101) [weld-core-impl-2.2.16.SP1.jar:2015-09-16 08:49] at org.jboss.weld.bean.ContextualInstanceStrategy$CachingContextualInstanceStrategy.get(ContextualInstanceStrategy.java:178) [weld-core-impl-2.2.16.SP1.jar:2015-09-16 08:49] at org.jboss.weld.bean.ContextualInstance.get(ContextualInstance.java:50) [weld-core-impl-2.2.16.SP1.jar:2015-09-16 08:49] at org.jboss.weld.bean.proxy.ContextBeanInstance.getInstance(ContextBeanInstance.java:99) [weld-core-impl-2.2.16.SP1.jar:2015-09-16 08:49] at org.jboss.weld.bean.proxy.ProxyMethodHandler.getInstance(ProxyMethodHandler.java:125) [weld-core-impl-2.2.16.SP1.jar:2015-09-16 08:49]

I even tried the solution presented in How to hunt down obscure HA clustering bug in Wildfly 8.2.0.Final . The problem there was different but the Stacktrace was similar so I gave it a try but with no success.

This didn't make any sense. Why would invalidating one session have problems with session scoped beans belonging to another one. It is basically the same thing as when one user logs in and a different logs out of the system. Their session scoped beans should have no interference at all. As a last hope I tried to execute the code of invalidating previous sessions in a different thread and it worked. For some unknown reason to me, creating a new session and invalidating others in the same method is causing problems with the sessionscoped beans, but having them in a different thread works just fine.

I hope this helps someone else that gets stuck on the same issue.

Note: Maybe the approach from the start was wrong and we should have invalidated previous sessions and then log the user in. Haven't tested this one as it had a lot of work to be put in.

Upvotes: 0

Bruno
Bruno

Reputation: 145

Have you tried this ?

String path = FacesContext.getCurrentInstance().getExternalContext().getApplicationContextPath() + "/index.xhtml";
FacesContext.getCurrentInstance().getExternalContext().invalidateSession();
FacesContext.getCurrentInstance().getExternalContext().redirect(path);

It works for me...

Upvotes: 2

Related Questions