2 Replies Latest reply on Jan 4, 2006 4:38 PM by toby saville

    task management and persistance questions

    toby saville Novice

      Hello,

      I am having trouble understanding the best time and place to persist my Process Instance when tasks are involved.

      I have a servlet that creates a new process instance from a definition which is retrieved via a user managed transaction:

      JbpmSession session = JbpmSessionFactory.getInstance().openJbpmSessionAndBeginTransaction()
      ProcessDefinition def = session.getGraphSession().
      findLatestProcessDefinition(?DEF_NAME?);
      ProcessInstance pi = new ProcessInstance(def);
      pi.signal();
      session.commitTransactionAndClose();
      

      The process instance then arrives at a task node, with an AssignmentHandler associated, which does the following:
      public void assign( Assignable assignable, ExecutionContext context ){
       assignable.setActorId(?myUsername?);
       context.getTaskInstance().start();
       JbpmSession session = JbpmSessionFactory.getInstance().openJbpmSessionAndBeginTransaction();
      session.getGraphSession().saveProcessInstance(context.getProcessInstance());
       session.commitTransactionAndClose();
      }
      

      However it is failing at the session..getGraphSession().saveProcessInstance(context.getProcessInstance());
      Line with the exception:
      Illegal attempt to associate a collection with two open sessions
      Can you please explain what I am doing wrong here? I have arrived at this junction after trying many different options and I cant think of any other way. I have the following questions:

      1.) can I start the transaction instance in the assignment handler? if not, where do I start it?
      2.) do I save the process instance in the assignment handler (or from any other class that is part of the process instance execution)? If not, how and where do I carry out the persistence after the process instance is in a wait state? If so, why am i getting the above error when saving?
      3.) currently I have 2 JbpmSessionFactorys. 1 for classes not involved in the process instance (servlets etc) and 1 for classes involved in the process instance (actions, assignment handlers etc). How can I have only 1 session factory thats used by all classes? I tried using JNDI, but we are using Tomcat as our application server, and its Initial Context is read only, so I cant place the session factory in there.

      Thanks for your help.

      toby

        • 1. Re: task management and persistance questions
          Alejandro Guizar Master

           

          "tobysaville" wrote:
          Can you please explain what I am doing wrong here?

          You are using the same persistent objects in two different hibernate sessions, which would screw the automatic change tracking and is unacceptable.
          can I start the transaction instance in the assignment handler? if not, where do I start it?

          You shouldn't start it there. Just use the same transaction you began before the signal().
          do I save the process instance in the assignment handler (or from any other class that is part of the process instance execution)?

          No. Save it after you signal():
          JbpmSession session = JbpmSessionFactory.getInstance().openJbpmSessionAndBeginTransaction()
          GraphSession graphSession = session.getGraphSession();
          ProcessDefinition def = graphSession.findLatestProcessDefinition(DEF_NAME);
          ProcessInstance pi = new ProcessInstance(def);
          pi.signal();
          graphSession.saveProcessInstance(pi);
          session.commitTransactionAndClose();

          How can I have only 1 session factory thats used by all classes?

          First build a unified configuration with all your mappings as described in section 3.1 of the user guide. Hibernate recommends a static singleton if your JNDI context is not bindable. See section 1.2.5 for more details.

          • 2. Re: task management and persistance questions
            toby saville Novice

            Thanks alot alex for your helpful response, appreciated.

            - toby