10 Replies Latest reply on Jan 27, 2010 12:21 PM by verborghs

    Session invalidation causes no active scope fault

      When executing following code (from a JSF action handler):



      @Named("destroySession")
      @RequestScoped
      public class DestroySessionAction {
      
         public void execute() {
            HttpSession session = (HttpSession) FacesContext
                    .getCurrentInstance()
                    .getExternalContext()
                    .getSession(true);
      
            session.invalidate();
         }
      }




      The message No active contexts for scope type javax.enterprise.context.SessionScoped get shown. I think this is normal, but wouldn't it be better, esp. in a JSF context for a new Session to be created.


      I can solve it by making the action handler as follows:



      @Named("destroySession")
      @RequestScoped
      public class DestroySessionAction {
      
         public String execute() {
            HttpSession session = (HttpSession) FacesContext
                    .getCurrentInstance()
                    .getExternalContext()
                    .getSession(true);
      
            session.invalidate();
            return "/scopes?faces-redirect=true";
         }
      }



      scopes.xhtml is the JSF page that invokes the action handler and I am using a @SessionScoped bean on it.



        • 1. Re: Session invalidation causes no active scope fault
          nickarls

          In any case the session context would be gone and the new one would be all empty, I guess.

          • 2. Re: Session invalidation causes no active scope fault

            The problem is more that the JSF framework, in the case of not using a redirect, tries to instantiate the @SessionScoped bean, but the session context isn't active yet.


            When using the JSF sessionscoped annotation, a new session get created after the invalidate call and a new bean gets created.


            As far as I can gasp the framework, I think in the class org.jboss.weld.servlet.ServletLifecycle the beginSession method should activate the SessionContext object.

            • 3. Re: Session invalidation causes no active scope fault
              nickarls

              https://jira.jboss.org/jira/browse/WELD-380 just got committed, could you check out if it applies to your case?

              • 4. Re: Session invalidation causes no active scope fault

                It seems to be the same problem, I'll try to install JBoss if time allows and run a further test.


                The only difference I might see in the testcase is that I try to use a @SessionScoped bean (without redirect) on the target page.




                javax.enterprise.context.ContextNotActiveException: No active contexts for scope type javax.enterprise.context.SessionScoped
                     at org.jboss.weld.BeanManagerImpl.getContext(BeanManagerImpl.java:928)
                     at org.jboss.weld.bean.proxy.ClientProxyMethodHandler.getProxiedInstance(ClientProxyMethodHandler.java:140)
                     at org.jboss.weld.bean.proxy.ClientProxyMethodHandler.invoke(ClientProxyMethodHandler.java:101)
                     at be.howest.model.Bean_$$_javassist_318.getValue1(Bean_$$_javassist_318.java)
                     at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
                     at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:39)
                     at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:25)
                     at java.lang.reflect.Method.invoke(Method.java:597)
                     at javax.el.BeanELResolver.getValue(BeanELResolver.java:302)
                     at javax.el.CompositeELResolver.getValue(CompositeELResolver.java:175)
                     at com.sun.faces.el.FacesCompositeELResolver.getValue(FacesCompositeELResolver.java:72)
                     at com.sun.el.parser.AstValue.getValue(AstValue.java:116)
                     at com.sun.el.parser.AstValue.getValue(AstValue.java:163)
                     at com.sun.el.ValueExpressionImpl.getValue(ValueExpressionImpl.java:219)
                     at org.jboss.weld.el.WeldValueExpression.getValue(WeldValueExpression.java:71)
                     at com.sun.faces.facelets.el.TagValueExpression.getValue(TagValueExpression.java:102)
                     at javax.faces.component.ComponentStateHelper.eval(ComponentStateHelper.java:190)
                     at javax.faces.component.ComponentStateHelper.eval(ComponentStateHelper.java:178)
                     at javax.faces.component.UIOutput.getValue(UIOutput.java:168)
                     at com.sun.faces.renderkit.html_basic.HtmlBasicInputRenderer.getValue(HtmlBasicInputRenderer.java:205)
                     at com.sun.faces.renderkit.html_basic.HtmlBasicRenderer.getCurrentValue(HtmlBasicRenderer.java:338)
                     at com.sun.faces.renderkit.html_basic.HtmlBasicRenderer.encodeEnd(HtmlBasicRenderer.java:164)
                     at javax.faces.component.UIComponentBase.encodeEnd(UIComponentBase.java:878)
                     at javax.faces.component.UIComponent.encodeAll(UIComponent.java:1620)
                     at javax.faces.render.Renderer.encodeChildren(Renderer.java:168)
                     at javax.faces.component.UIComponentBase.encodeChildren(UIComponentBase.java:848)
                     at javax.faces.component.UIComponent.encodeAll(UIComponent.java:1613)
                     at javax.faces.component.UIComponent.encodeAll(UIComponent.java:1616)
                     at javax.faces.component.UIComponent.encodeAll(UIComponent.java:1616)
                     at com.sun.faces.application.view.FaceletViewHandlingStrategy.renderView(FaceletViewHandlingStrategy.java:380)
                     at com.sun.faces.application.view.MultiViewHandler.renderView(MultiViewHandler.java:126)
                     at javax.faces.application.ViewHandlerWrapper.renderView(ViewHandlerWrapper.java:273)
                     at com.sun.faces.lifecycle.RenderResponsePhase.execute(RenderResponsePhase.java:127)
                     at com.sun.faces.lifecycle.Phase.doPhase(Phase.java:101)
                     at com.sun.faces.lifecycle.LifecycleImpl.render(LifecycleImpl.java:139)
                     at javax.faces.webapp.FacesServlet.service(FacesServlet.java:313)
                     at org.apache.catalina.core.StandardWrapper.service(StandardWrapper.java:1523)
                     at org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:279)
                     at org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:188)
                     at org.apache.catalina.core.StandardPipeline.invoke(StandardPipeline.java:641)
                     at com.sun.enterprise.web.WebPipeline.invoke(WebPipeline.java:97)
                     at com.sun.enterprise.web.PESessionLockingStandardPipeline.invoke(PESessionLockingStandardPipeline.java:85)
                     at org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:185)
                     at org.apache.catalina.connector.CoyoteAdapter.doService(CoyoteAdapter.java:332)
                     at org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:233)
                     at com.sun.enterprise.v3.services.impl.ContainerMapper.service(ContainerMapper.java:165)
                     at com.sun.grizzly.http.ProcessorTask.invokeAdapter(ProcessorTask.java:791)
                     at com.sun.grizzly.http.ProcessorTask.doProcess(ProcessorTask.java:693)
                     at com.sun.grizzly.http.ProcessorTask.process(ProcessorTask.java:954)
                     at com.sun.grizzly.http.DefaultProtocolFilter.execute(DefaultProtocolFilter.java:170)
                     at com.sun.grizzly.DefaultProtocolChain.executeProtocolFilter(DefaultProtocolChain.java:135)
                     at com.sun.grizzly.DefaultProtocolChain.execute(DefaultProtocolChain.java:102)
                     at com.sun.grizzly.DefaultProtocolChain.execute(DefaultProtocolChain.java:88)
                     at com.sun.grizzly.http.HttpProtocolChain.execute(HttpProtocolChain.java:76)
                     at com.sun.grizzly.ProtocolChainContextTask.doCall(ProtocolChainContextTask.java:53)
                     at com.sun.grizzly.SelectionKeyContextTask.call(SelectionKeyContextTask.java:57)
                     at com.sun.grizzly.ContextTask.run(ContextTask.java:69)
                     at com.sun.grizzly.util.AbstractThreadPool$Worker.doWork(AbstractThreadPool.java:330)
                     at com.sun.grizzly.util.AbstractThreadPool$Worker.run(AbstractThreadPool.java:309)
                     at java.lang.Thread.run(Thread.java:619)




                In my case it starts at: be.howest.model.Bean in the other case it starts at org.jboss.weld.conversation.ServletConversationManager, both are @SessionScoped beans. I think this is something that is not being tested in the Test that is available in WELD-380

                • 5. Re: Session invalidation causes no active scope fault

                  I tried to replicate this on JBoss AS 6 and got the same problem as on Glassfish v3.


                  Demo maven project is here (uses jboss-maven-plugin.)


                  • 6. Re: Session invalidation causes no active scope fault
                    nickarls

                    Hmm, how do you create a new session mid-request? I don't think you'll get a new one until the next request. You can invalidate the session but request.getSession() will probably return the current (invalidated) one. If you use a session scoped bean after the session has invalidated, it will do a BeanStore.get and that will delegate into an attribute-get on the session and that in turn will throw a IllegalStateException...

                    • 7. Re: Session invalidation causes no active scope fault

                      Well, the fact is when using the optional JSF annotations like @javax.faces.bean.ManagedBean (and @javax.faces.bean.RequestScoped and @javax.faces.bean.SessionScoped) it works as expected.


                      The exception is ContextNotActiveException, caused by setting setActive(false) on the scope.
                      I personally would like to see a session getting created when there was none available.


                      Putting following code after session.invalidate() shows that calling getSession(true) gives a new session back, and not the old one.


                      session = (HttpSession) FacesContext
                                    .getCurrentInstance()
                                    .getExternalContext()
                                    .getSession(true);
                       System.out.println(session.isNew());



                      A reason where this might be useful is invalidating a session and starting a new one to avoid session fixation.

                      • 8. Re: Session invalidation causes no active scope fault
                        nickarls

                        OK, it actually gives you a new one. In that case it can probably be worked around. Would you mind filing a feature JIRA for this?

                        • 9. Re: Session invalidation causes no active scope fault

                          WELD-403 created

                          • 10. Re: Session invalidation causes no active scope fault

                            WELD-403 created