Unable to invalidate session when application contains @Stateful @ViewScoped beans
pdhaigh Mar 11, 2014 4:05 AMHi,
I have found that in my application I am unable to invalidate the session - either manually by called session.invalidate(), or automatically by allowing the session to timeout.
The error I see is:
22:33:59,183 ERROR stderr (default task-9) Exception in thread "default task-9" org.jboss.weld.exceptions.IllegalArgumentException: WELD-000085: Cannot destroy null instance of Session bean class uk.co.app.utilities.EntityManagerProducer with qualifiers [@Any @Default; local interfaces are EntityManagerProducer
22:33:59,183 ERROR stderr (default task-9) at org.jboss.weld.bean.SessionBean.destroy(SessionBean.java:156)
22:33:59,183 ERROR stderr (default task-9) at com.sun.faces.application.view.ViewScopeContextManager.destroyBeans(ViewScopeContextManager.java:161)
22:33:59,184 ERROR stderr (default task-9) at com.sun.faces.application.view.ViewScopeContextManager.sessionDestroyed(ViewScopeContextManager.java:290)
22:33:59,184 ERROR stderr (default task-9) at com.sun.faces.application.view.ViewScopeManager.sessionDestroyed(ViewScopeManager.java:349)
22:33:59,184 ERROR stderr (default task-9) at com.sun.faces.application.WebappLifecycleListener.sessionDestroyed(WebappLifecycleListener.java:181)
22:33:59,185 ERROR stderr (default task-9) at com.sun.faces.config.ConfigureListener.sessionDestroyed(ConfigureListener.java:368)
22:33:59,185 ERROR stderr (default task-9) at io.undertow.servlet.core.ApplicationListeners.sessionDestroyed(ApplicationListeners.java:264)
22:33:59,185 ERROR stderr (default task-9) at io.undertow.servlet.core.SessionListenerBridge.sessionDestroyed(SessionListenerBridge.java:48)
22:33:59,185 ERROR stderr (default task-9) at io.undertow.server.session.SessionListeners.sessionDestroyed(SessionListeners.java:38)
22:33:59,186 ERROR stderr (default task-9) at io.undertow.server.session.InMemorySessionManager$SessionImpl.invalidate(InMemorySessionManager.java:376)
22:33:59,186 ERROR stderr (default task-9) at io.undertow.server.session.InMemorySessionManager$SessionImpl$1$1.run(InMemorySessionManager.java:228)
22:33:59,186 ERROR stderr (default task-9) at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1145)
22:33:59,186 ERROR stderr (default task-9) at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:615)
22:33:59,187 ERROR stderr (default task-9) at java.lang.Thread.run(Thread.java:744)
The error is thrown from within the below Weld method:
@Override
public void destroy(T instance, CreationalContext<T> creationalContext) {
if (instance == null) {
throw BeanLogger.LOG.cannotDestroyNullBean(this);
}
if (!(instance instanceof EnterpriseBeanInstance)) {
throw BeanLogger.LOG.cannotDestroyEnterpriseBeanNotCreated(instance);
}
EnterpriseBeanInstance enterpriseBeanInstance = (EnterpriseBeanInstance) instance;
enterpriseBeanInstance.destroy(Marker.INSTANCE, this, creationalContext);
creationalContext.release();
}
The class referenced in the stack trace is:
import javax.faces.view.ViewScoped;
import javax.persistence.EntityManager;
import javax.persistence.PersistenceContext;
import javax.persistence.PersistenceContextType;
import org.hibernate.FlushMode;
import org.hibernate.Session;
@ViewScoped
@Stateful
public class EntityManagerProducer implements Serializable
{
<snip>
}
If I change this (and other classes) to:
import javax.enterprise.context.SessionScoped;
import javax.persistence.EntityManager;
import javax.persistence.PersistenceContext;
import javax.persistence.PersistenceContextType;
import org.hibernate.FlushMode;
import org.hibernate.Session;
@SessionScoped
@Stateful
public class EntityManagerProducer implements Serializable
{
<snip>
}
Then the session can be invalidated. Further, if I remove the @Stateful annotation, the session can be invalidated. The combination of @Stateful and @ViewScoped appears to cause the issue.
Is the combination of @Stateful and @ViewScoped prohibited? My understanding was that the new JSF ViewScoped annotation can be used interchangably with javax.enterprise.context scopes? Aside from this issue, the combination seems to be working completely fine..