title: Completing a Task Fails after Calling statefulSession.dispose
body: <p>I have a problem which I think is similar to these but my scenario is slightly different. </p>
  
  <p>How to make a process instance go on with jBPM5?<br>
  <a href="http://community.jboss.org/message/600654" rel="nofollow">http://community.jboss.org/message/600654</a></p>
  
  <p>Human task API, how to move forward in the workflow ?
  <a href="http://community.jboss.org/message/614986#614986" rel="nofollow">http://community.jboss.org/message/614986#614986</a></p>
  
  <p>Here is my problem. I create a process using the following:</p>
  
  <pre><code>Environment env = KnowledgeBaseFactory.newEnvironment();
  env.set(EnvironmentName.ENTITY_MANAGER_FACTORY, entityManagerFactory);
  InitialContext ctx = new InitialContext();
  UserTransaction transactionManager = (UserTransaction) ctx.lookup("java:comp/UserTransaction");
  env.set(EnvironmentName.TRANSACTION_MANAGER, transactionManager);
  
  StatefulKnowledgeSession knowledgeSession = JPAKnowledgeService.newStatefulKnowledgeSession(knowledgeBase, null, env);
  WorkItemHandler handler = new CommandBasedWSHumanTaskHandler(knowledgeSession);
  knowledgeSession.getWorkItemManager().registerWorkItemHandler("Human Task", handler);
  
  statefulSession.startProcess(processDefinitionId, processVariables);
  
  statefulSession.dispose(); (Remember this line)
  </code></pre>
  
  <p>This works fine. The process is created and the first Human Task exists as expected.</p>
  
  <p>My next step is to use the Mina Task Client, via the task client to assign and complete the task. The assign works perfectly however I get the following exception when completing the task.</p>
  
  <pre><code>SEVERE: Could not commit session
  java.lang.NullPointerException
      at org.drools.persistence.jpa.JpaPersistenceContextManager.beginCommandScopedEntityManager(JpaPersistenceContextManager.java:67)
      at org.drools.persistence.SingleSessionCommandService.execute(SingleSessionCommandService.java:287)
      at org.drools.command.impl.CommandBasedStatefulKnowledgeSession$1.completeWorkItem(CommandBasedStatefulKnowledgeSession.java:149)
      at org.jbpm.process.workitem.wsht.CommandBasedWSHumanTaskHandler$GetResultContentResponseHandler.execute(CommandBasedWSHumanTaskHandler.java:295)
      at org.jbpm.task.service.TaskClientHandler.messageReceived(TaskClientHandler.java:153)
      at org.jbpm.task.service.mina.MinaTaskClientHandler.messageReceived(MinaTaskClientHandler.java:47)
      at org.apache.mina.core.filterchain.DefaultIoFilterChain$TailFilter.messageReceived(DefaultIoFilterChain.java:713)
  </code></pre>
  
  <p>This results in the task being completed but the next task is not created.</p>
  
  <p>This exception is thrown because the org.drools.persistence.jpa.JpaPersistenceContextManager.appScopedEntityManager is null. This field is initialised when the JpaPersistenceContextManager is created and is nulled when the dispose method is called. </p>
  
  <p>I was able to have the task complete properly by commenting out</p>
  
  <pre><code>// statefulSession.dispose();
  </code></pre>
  
  <p>Not a good solution. I can't keep sessions open forever.</p>
  
  <p>The crux of the problem is that when completing the task it attempts to use the JpaPersistenceContextManager that was used to create the process, which has already been disposed of.</p>
  
  <p>My question is, how do I re-intitialise the JpaPersistenceContextManager to use ensure it has an active entityManager? Remember I'm making a call via the Mina Task Client so have no direct access to the JpaPersistenceContextManager.</p>