10 Replies Latest reply on May 15, 2008 8:56 PM by wjq

    jbpmContext.close() save process instance automaticly

    wjq Newbie

      I noticed that in jbpm 3.2.2, a call on jbpmContext.close() will save process instance automaticly even I dont call jbpmContext.save(processInstance) explicitly. Why ?

      How can I close the jbpmContext but not save the processInstance ?

      The following is my test code.

      ... ...
      
       JbpmConfiguration jbpmConfiguration = JbpmConfiguration.getInstance();
       JbpmContext jbpmContext = jbpmConfiguration.createJbpmContext();
       try {
      
       GraphSession graphSession = jbpmContext.getGraphSession();
       System.out.println("test: finding process def: " + processName);
       ProcessDefinition processDefinition = graphSession
       .findLatestProcessDefinition(processName);
       System.out.println("test: found process def: " + processName
       + " . create a new instance ...");
       ProcessInstance processInstance = new ProcessInstance(
       processDefinition);
       System.out.println("new instance created: " + processName
       + " . signal to run ...");
       processInstance.signal();
       System.out.println("done signal: " + processName
       + " . save process instance ....");
       // Token token = processInstance.getRootToken();
       // token.signal();
       jbpmContext.save(processInstance);
       System.out.println("save instance OK !");
       } finally {
       jbpmContext.close();
       }
      
       ... ...
      


        • 1. Re: jbpmContext.close() save process instance automaticly
          Mauricio Salatino Master

          Every time that jBPM reach a wait state the process is persisted..
          and when you call jbpmContext.close() you are closing a hibernate session so you are flushing all the changes to the database..
          Explain why you dont wanna save the changes....???

          • 2. Re: jbpmContext.close() save process instance automaticly
            wjq Newbie

            salaboy21, thx for your response.
            I'm a newbie on jbpm. I think we of course sometimes dont want save the process instance.
            For example,after create a new process instance, a method will be called to do something. i want to save the process instance only if the call is success returned.If the method call failed(e.g. throws out an exception),i want the new created process instance been discarded.

            Since the jbpmContext do has a save method ,so I think it should allow me to control whether save or not.

            • 3. Re: jbpmContext.close() save process instance automaticly
              Mauricio Salatino Master

              if you have this scenario
              you can try something like this:

              if(call())
               processInstance=ProcessInstance.parseXML......
              else
               processInstance=null
              


              make my point?

              • 4. Re: jbpmContext.close() save process instance automaticly
                Ronald van Kuijk Master

                salaboy is correct. If you want to determine if a process is instance is needed, determine that in advance. If not, just do not start the process.

                • 5. Re: jbpmContext.close() save process instance automaticly
                  wjq Newbie

                  Determine if create the instance beforehand may be right for the example scenario. Then the question is ,what is the save method good for ? or when the save method of jbpmConext is needed?

                  • 6. Re: jbpmContext.close() save process instance automaticly
                    David Roberts Apprentice

                    If you don't want changes to a process or task instance saved or persisted to the db when you close the JbpmContext, you could try calling jbpmContext.setRollbackOnly();

                    When the JbpmContext is then closed, it should roll back any changes made. That's how I do it anyway, and it works.

                    As for the save method, I think it is used when creating a new process instance. If I remember correctly, in the jbpm-console source code, when it creates a new process instance, there is a piece of code that goes as follows:

                    // Nothing else saves the process, so we must
                    jbpmContext.save(processInstance);

                    Not sure why they said 'Nothing else saves the process, so we must'. Perhaps because it's a new process instance?

                    Hope this helps.

                    • 7. Re: jbpmContext.close() save process instance automaticly
                      wjq Newbie

                      Thanks dleerob.

                      But i still confusing about the issue. because in the userguide doc of jbpm3.2,there are sample code which call save method explicitly. And they say (in section 7.1.2. Convenience methods on JbpmContext) "Note that if you use the xxxForUpdate methods in the JbpmContext, an explicit invocation of the jbpmContext.save is not necessary any more because it will then occur automatically during the close of the jbpmContext" . So it seemed that if you do NOT use the xxxForUpdate methods,you have to call the save method.

                      I noticed there is a setRollbackOnly() method too. Since it works for you anyway, I will follow. but hope somebody can clarify it.

                      • 8. Re: jbpmContext.close() save process instance automaticly
                        Alex Kavo Novice

                        Hello joy_wind, I would like to know where your code is located. I mean do you start it from a jsp file, servlet?
                        I tried your code in a jsp file but I always get an error.

                        • 9. Re: jbpmContext.close() save process instance automaticly
                          Ronald van Kuijk Master

                          coolex,

                          you have a completely different problem, your code looks good... see my other post

                          • 10. Re: jbpmContext.close() save process instance automaticly
                            wjq Newbie

                            coolex,

                            I run my test code as a standalone application(e.g, in a main method), not in a web container.

                            My test app is like the following:

                            import org.apache.log4j.Logger;
                            import org.jbpm.JbpmConfiguration;
                            import org.jbpm.JbpmContext;
                            import org.jbpm.db.GraphSession;
                            import org.jbpm.graph.def.ProcessDefinition;
                            import org.jbpm.graph.exe.ProcessInstance;
                            
                            public class Test {
                             private static Logger logger = Logger.getLogger(Test.class);
                            
                             public static void main(String[] args) throws Exception {
                             JbpmConfiguration jbpmConfiguration = JbpmConfiguration.getInstance();
                             JbpmContext jbpmContext = jbpmConfiguration.createJbpmContext();
                             try {
                            
                             GraphSession graphSession = jbpmContext.getGraphSession();
                             logger.info("loading definition ...");
                             ProcessDefinition processDefinition = graphSession
                             .findLatestProcessDefinition("test-process");
                             logger.info("create new process instance ...");
                             ProcessInstance processInstance = new ProcessInstance(
                             processDefinition);
                             } finally {
                             logger.info("closing context !");
                             jbpmContext.close();
                             }
                             }
                            }

                            As the code shows,I just create a new processInstance and do nothing else.But after jbpmContext.close(),jbpm create a new instance in my database even if i dont invoke the save method.
                            By check log output i found jbpm run serveral INSERT sql statements on my database when jbpmContext.close().