4 Replies Latest reply on Sep 25, 2006 3:37 PM by brado

    Bizarro jBPMException when trying to create a new JbpmContex

    brado

      I am using jBPM 3.1.2, and have incorporated my own servlets into what is basically the jbpm.war webapp. My servlets start by doing nothing more complicated than create a jbpmContext, using the following code:

      JbpmConfiguration jc = JbpmConfiguration.getInstance();
      JbpmContext jbpmContext = jc.createJbpmContext();


      This code results in this exception:

      org.jbpm.JbpmException: closed JbpmContext in different order then they were created... check your try-finally's around JbpmContexts blocks
       at org.jbpm.JbpmConfiguration.popJbpmContext(JbpmConfiguration.java:525)
       at org.jbpm.JbpmConfiguration.jbpmContextClosed(JbpmConfiguration.java:537)
       at org.jbpm.JbpmContext.close(JbpmContext.java:144)
       at org.jbpm.web.JbpmContextFilter.doFilter(JbpmContextFilter.java:85)
       at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:202)
       at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:173)
       at org.jbpm.webapp.filter.LogFilter.doFilter(LogFilter.java:65)
       at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:202)
       at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:173)
       at org.jboss.web.tomcat.filters.ReplyHeaderFilter.doFilter(ReplyHeaderFilter.java:96)
       at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:202)
       at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:173)
       at org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:213)
       at org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:178)
       at org.jboss.web.tomcat.security.SecurityAssociationValve.invoke(SecurityAssociationValve.java:175)
       at org.jboss.web.tomcat.security.JaccContextValve.invoke(JaccContextValve.java:74)
       at org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:126)
       at org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:105)
       at org.apache.catalina.core.StandardEngineValve.invoke(StandardEngineValve.java:107)
       at org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:148)
       at org.apache.coyote.http11.Http11Processor.process(Http11Processor.java:869)
       at org.apache.coyote.http11.Http11BaseProtocol$Http11ConnectionHandler.processConnection(Http11BaseProtocol.java:664)
       at org.apache.tomcat.util.net.PoolTcpEndpoint.processSocket(PoolTcpEndpoint.java:527)
       at org.apache.tomcat.util.net.MasterSlaveWorkerThread.run(MasterSlaveWorkerThread.java:112)
       at java.lang.Thread.run(Thread.java:595)
      


      This is bizarre. The only thing my webapp does after loading is what the jbpm.war does by default, which is start the JbpmThreadsServlet. Does anyone have any idea why this is happening?

      Thanks,

      Brad

        • 1. Re: Bizarro jBPMException when trying to create a new JbpmCo
          cpob

          Because you are using the JbpmContextFilter, which opens and closes the context for you in your servlet. Your servlet should not create a context, but should get the current context.

          JbpmConfiguration jc = JbpmConfiguration.getInstance();JbpmContext jbpmContext = jc.getCurrentJbpmContext();


          • 2. Re: Bizarro jBPMException when trying to create a new JbpmCo
            brado

            cpob,

            Thanks for the very helpful reply and answer. I greatly appreciate it. Your answer brings up another question I have had, which is when should new JbpmContext be created, and when should the current context be obtained instead?

            The reason that this question hasn't been completely clear to me is because of a couple reasons:

            1) It appears that any saving of data to the database (such as process instance state) doesn't commit until the JbpmContext is closed. This then seemed to suggest that a JbpmContext needed to be created and closed each time data needed to be saved to the database.

            2) While I have seen documentation saying that JbpmConfiguration was re-entrant, I have seen no such indication that JbpmContext is. I wasn't sure that if I was working with a JbpmContext in a servlet (or several servlets) whether it would be safe for all of them to be operating simultaneously on the current jbpmContext, rather than all using their own jbpmContext instance for thread safety.

            What is the recommended approach to using jbpmContexts? Can the "current" jbpmContext be used across multiple servlets and multiple threads without there being a problem, or does each one need to create its own jbpmContext, and how does this affect the committing of data to the database?

            Thanks for your help,

            Brad

            • 3. Re: Bizarro jBPMException when trying to create a new JbpmCo
              koen.aers

              Brad,

              The jbpm context has a lifetime that is equal to the lifetime of the request (in a webapplication). So it is one context per thread, the JbpmContext is not threadsafe.

              Regards,
              Koen

              • 4. Re: Bizarro jBPMException when trying to create a new JbpmCo
                brado

                Koen,

                Thanks for the reply. If the lifetime of the jbpmContext is equal to the lifetime of the request (in a webapplication), then I have a few more questions:

                1) What enforces this lifetime, a jbpmContext.close() has to be invoked somewhere, doesn't it?

                2) Is the purpose of jbpmConfiguration.getCurrentContext() so that you don't have to pass a reference to the jbpmContext to all the methods called by the request code (servlet)? I am guessing that must be the reason, as general usage of jbpmContexts suggest creating a new jbpmContext instance each time its used for some purpose.

                Thanks again for your help.

                Brad