I&#39;m afraid this bug is not resolved and doesn&#39;t have nothing to do with lazy evaluation.<br><br>We&#39;ve been able to get to the source of the problem and this are the facts:<br><br>   - Drools and JBPM have different persistence managers.<br>

   - Drools uses JTA for persistence and JBPM does not.<br>   - When any JPA enabled object is persisted in Drools, a transaction begins, it&#39;s EntityManager ( Hibernate ) joins the transaction and the entity gets persisted. <br>

   - When any JPA enabled object ( in this case a ProcessInstanceInfo ) is persisted in JBPM, there are no transactions involved, and so the EntityManager ( Hibernate ) decides to delay the insert ( queuing it as there&#39;s no transaction in progress ). Then the processId never gets updated and the NPE arises.<br>

<br>The problem here seems to be that both Drools and JBPM manage persistence in different and incompatible ways. We&#39;ve been able to modify jbpm-persistence-jpa to open a JTA transaction before persisting ProcessInstanceInfo, getting the EntityManager joining that transaction and using the current Bitronix implementation already running in Drools ( to assist persistence for their objects ) .. like so:<br>

<br>    public void setKnowledgeRuntime(InternalKnowledgeRuntime kruntime) {<br>        this.kruntime = kruntime;<br>        Environment env = kruntime.getEnvironment();<br>        Object tm = env.get( EnvironmentName.TRANSACTION_MANAGER );<br>

        if (!(tm instanceof javax.transaction.TransactionManager)) {<br>            try {<br>            //     get Bitronix instance inside ...<br>                java.lang.reflect.Field field = tm.getClass().getDeclaredField(&quot;tm&quot;);<br>

                // who says private in Java is really private ... xD<br>                field.setAccessible(true);<br>                tm = field.get(tm);<br>            } catch (Exception e){<br>                e.printStackTrace();<br>

            }<br>        }<br>        this.txm = new JtaTransactionManager( env.get( EnvironmentName.TRANSACTION ), env.get( EnvironmentName.TRANSACTION_SYNCHRONIZATION_REGISTRY ), tm );<br>    }<br><br>    public void addProcessInstance(ProcessInstance processInstance) {<br>

        ProcessInstanceInfo processInstanceInfo = new ProcessInstanceInfo( processInstance, this.kruntime.getEnvironment() );<br>        ProcessPersistenceContext context = ((ProcessPersistenceContextManager) this.kruntime.getEnvironment().get( EnvironmentName.PERSISTENCE_CONTEXT_MANAGER )).getProcessPersistenceContext();<br>

        this.txm.begin();<br>        context.persist( processInstanceInfo );<br>        this.txm.commit();<br><br><br><br>I think we can agree that the previous lines of code are not the most elegant solution  ( at least not for me ).<br>

<br>So, Is there any timeline for merging DROOLS and JBPM 5 persistence managers?   <br><br>If someone gives me advice and architectural hints ( and if it is doable in a reasonable ammount of time ) I would be eager to submit a patch for this ....<br>

<br><br clear="all">Alberto R. Galdo<br><a href="mailto:argaldo@gmail.com">argaldo@gmail.com</a><br><br><br><div class="gmail_quote">On Wed, Mar 14, 2012 at 13:56, Alberto R. Galdo <span dir="ltr">&lt;<a href="mailto:argaldo@gmail.com">argaldo@gmail.com</a>&gt;</span> wrote:<br>

<blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex">Great news!<br><br>We were in the process of debugging JBPM trying to find the source of the bug ... and maybe days away from the solution ...<br>

<br>Is
 there any bug report in Jira and/or a patch we can apply without having
 to wait for the next release so we can quick-patch our systems?<span class="HOEnZb"><font color="#888888"><br><br><br clear="all">Alberto R. Galdo<br><a href="mailto:argaldo@gmail.com" target="_blank">argaldo@gmail.com</a></font></span><div class="HOEnZb">

<div class="h5"><br><br><br><div class="gmail_quote">On Wed, Mar 14, 2012 at 13:09, Marco Rietveld <span dir="ltr">&lt;<a href="mailto:mrietvel@redhat.com" target="_blank">mrietvel@redhat.com</a>&gt;</span> wrote:<br>
<blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex">
  
    
  
  <div bgcolor="#FFFFFF" text="#000000">
    <br>
    Hi Alberto, <br>
    <br>
    This is a bug that has been fixed in jBPM. It had to do with lazy
    initialization of a <font face="Default Sans
      Serif,Verdana,Arial,Helvetica,sans-serif">JPAProcessInstanceManager</font>
    field. <br>
    <br>
    We&#39;ll be releasing a new jBPM version sometime soon (synchronous
    with Drools, I think). The bug is fixed in there. <br>
    <br>
    Regards,<br>
    Marco<br>
    <br>
    03/08/2012 11:32 AM, Alberto R. Galdo:
    <blockquote type="cite"><div><div><font face="Default Sans
        Serif,Verdana,Arial,Helvetica,sans-serif">
        <div>Hi, </div>
        <div><br>
        </div>
        <div>  We&#39;re running an application that uses Drools + JBPM 5 +
          Drools integration our set-up can be seen as: </div>
        <div>
          <br>
        </div>
        <div>  Some rule fires and creates a JBPM process ( a fact gets
          inserted into drools using
          &quot;kcontext.getKnowledgeRuntime().startProcess(&quot;testProcess&quot;)&quot;
          ). We have a problem with the persistence of this processes.
          Persistence is implemented with JPA and JTA. Our application
          runs with fireUntilHalt() and when a process is launched from
          the consequence of any of the rules the persistence of the
          process fails. If the application runs with fireAllRules(),
          the persistence works like a charm.</div>
        <div>  </div>
        <div>  The error shown is as follow:</div>
        <div>  </div>
        <div>  Exception in thread &quot;Thread-5&quot; Exception executing
          consequence for rule &quot;Run Process&quot; in com.sample:
          java.lang.NullPointerException</div>
        <div><span style="white-space:pre-wrap"> </span>at
org.drools.runtime.rule.impl.DefaultConsequenceExceptionHandler.handleException(DefaultConsequenceExceptionHandler.java:39)</div>
        <div><span style="white-space:pre-wrap"> </span>at
          org.drools.common.DefaultAgenda.fireActivation(DefaultAgenda.java:1101)</div>
        <div><span style="white-space:pre-wrap"> </span>at
          org.drools.common.DefaultAgenda.fireNextItem(DefaultAgenda.java:1029)</div>
        <div><span style="white-space:pre-wrap"> </span>at
          org.drools.common.DefaultAgenda.fireUntilHalt(DefaultAgenda.java:1229)</div>
        <div><span style="white-space:pre-wrap"> </span>at
org.drools.common.AbstractWorkingMemory.fireUntilHalt(AbstractWorkingMemory.java:754)</div>
        <div><span style="white-space:pre-wrap"> </span>at
org.drools.common.AbstractWorkingMemory.fireUntilHalt(AbstractWorkingMemory.java:730)</div>
        <div><span style="white-space:pre-wrap"> </span>at
org.drools.command.runtime.rule.FireUntilHaltCommand$1.run(FireUntilHaltCommand.java:50)</div>
        <div><span style="white-space:pre-wrap"> </span>at
          java.lang.Thread.run(Thread.java:662)</div>
        <div>Caused by: java.lang.NullPointerException</div>
        <div><span style="white-space:pre-wrap"> </span>at
org.jbpm.persistence.processinstance.JPAProcessInstanceManager.addProcessInstance(JPAProcessInstanceManager.java:44)</div>
        <div><span style="white-space:pre-wrap"> </span>at
org.jbpm.process.instance.AbstractProcessInstanceFactory.createProcessInstance(AbstractProcessInstanceFactory.java:36)</div>
        <div><span style="white-space:pre-wrap"> </span>at
org.jbpm.process.instance.ProcessRuntimeImpl.startProcess(ProcessRuntimeImpl.java:182)</div>
        <div><span style="white-space:pre-wrap"> </span>at
org.jbpm.process.instance.ProcessRuntimeImpl.createProcessInstance(ProcessRuntimeImpl.java:154)</div>
        <div><span style="white-space:pre-wrap"> </span>at
org.jbpm.process.instance.ProcessRuntimeImpl.startProcess(ProcessRuntimeImpl.java:135)</div>
        <div><span style="white-space:pre-wrap"> </span>at
org.jbpm.process.instance.ProcessRuntimeImpl.startProcess(ProcessRuntimeImpl.java:130)</div>
        <div><span style="white-space:pre-wrap"> </span>at
org.drools.common.AbstractWorkingMemory.startProcess(AbstractWorkingMemory.java:1074)</div>
        <div><span style="white-space:pre-wrap"> </span>at
org.drools.impl.StatefulKnowledgeSessionImpl.startProcess(StatefulKnowledgeSessionImpl.java:301)</div>
        <div><span style="white-space:pre-wrap"> </span>at
          com.sample.Rule_Run_Process.defaultConsequence(Rule_Run_Process.java:9)</div>
        <div><span style="white-space:pre-wrap"> </span>at
          com.sample.Rule_Run_ProcessDefaultConsequenceInvoker.evaluate(Unknown
          Source)</div>
        <div><span style="white-space:pre-wrap"> </span>at
          org.drools.common.DefaultAgenda.fireActivation(DefaultAgenda.java:1091)</div>
        <div><span style="white-space:pre-wrap"> </span>... 6 more</div>
        <div><span style="white-space:pre-wrap"> </span></div>
        <div><span style="white-space:pre-wrap"> </span>The problem is
          in this function:</div>
        <div><span style="white-space:pre-wrap"> </span></div>
        <div><span style="white-space:pre-wrap"> </span>public void
          addProcessInstance(ProcessInstance processInstance) {</div>
        <div>        ProcessInstanceInfo processInstanceInfo = new
          ProcessInstanceInfo( processInstance,
          this.kruntime.getEnvironment() );</div>
        <div>        ProcessPersistenceContext context </div>
        <div>            = ((ProcessPersistenceContextManager)
          this.kruntime.getEnvironment()</div>
        <div>                    .get(
          EnvironmentName.PERSISTENCE_CONTEXT_MANAGER ))</div>
        <div>                    .getProcessPersistenceContext();</div>
        <div>        // @PrePersist added to ProcessInstanceInfo because
          of this</div>
        <div>        context.persist( processInstanceInfo );</div>
        <div>        ((org.jbpm.process.instance.ProcessInstance)
          processInstance).setId( processInstanceInfo.getId() );</div>
        <div>        processInstanceInfo.updateLastReadDate();</div>
        <div>        internalAddProcessInstance(processInstance);</div>
        <div>    }</div>
        <div><span style="white-space:pre-wrap"> </span></div>
        <div><span style="white-space:pre-wrap"> </span>We think after
          that persist sentence, the entity manager would have to run a
          flush sentence for the process instance is inserted into
          database and get the ID.</div>
        <div><span style="white-space:pre-wrap"> </span></div>
        <div><span style="white-space:pre-wrap"> </span>Greets.</div>
      </font>
      <br>
      <fieldset></fieldset>
      <br>
      </div></div><div><pre>_______________________________________________
rules-users mailing list
<a href="mailto:rules-users@lists.jboss.org" target="_blank">rules-users@lists.jboss.org</a>
<a href="https://lists.jboss.org/mailman/listinfo/rules-users" target="_blank">https://lists.jboss.org/mailman/listinfo/rules-users</a>
</pre>
    </div></blockquote><span><font color="#888888">
    <br>
    <br>
    <pre cols="72">-- 
jBPM/Drools developer
Utrecht, the Netherlands</pre>
  </font></span></div>

</blockquote></div><br>
</div></div></blockquote></div><br>