Sure!<div><br></div><div><br></div><div>Drools does JPA persistence using this set of managers:</div><div><br></div><div><a href="https://github.com/droolsjbpm/drools/blob/master/drools-persistence-jpa/src/main/java/org/drools/persistence/jpa/JpaPersistenceContextManager.java">https://github.com/droolsjbpm/drools/blob/master/drools-persistence-jpa/src/main/java/org/drools/persistence/jpa/JpaPersistenceContextManager.java</a></div>

<div><br></div><div><a href="https://github.com/droolsjbpm/drools/blob/master/drools-persistence-jpa/src/main/java/org/drools/persistence/jpa/JpaPersistenceContext.java">https://github.com/droolsjbpm/drools/blob/master/drools-persistence-jpa/src/main/java/org/drools/persistence/jpa/JpaPersistenceContext.java</a></div>

<div><br></div><div>and JTA transaction management using:</div><div><br></div><div><a href="https://github.com/droolsjbpm/drools/blob/master/drools-persistence-jpa/src/main/java/org/drools/persistence/jta/JtaTransactionManager.java">https://github.com/droolsjbpm/drools/blob/master/drools-persistence-jpa/src/main/java/org/drools/persistence/jta/JtaTransactionManager.java</a></div>

<div><br></div><div><br></div><div>As you can see in JpaPersistenceContext, Drools only has support for persisting this set of entities:  SessionInfo &amp; WorkItemInfo</div><div><br></div><div><br></div><div>JBPM has its own persistence management implemented in this classes:</div>

<div><br></div><div><a href="https://github.com/droolsjbpm/jbpm/blob/master/jbpm-persistence-jpa/src/main/java/org/jbpm/persistence/JpaProcessPersistenceContext.java">https://github.com/droolsjbpm/jbpm/blob/master/jbpm-persistence-jpa/src/main/java/org/jbpm/persistence/JpaProcessPersistenceContext.java</a></div>

<div><br></div><div><a href="https://github.com/droolsjbpm/jbpm/blob/master/jbpm-persistence-jpa/src/main/java/org/jbpm/persistence/processinstance/JPAProcessInstanceManager.java">https://github.com/droolsjbpm/jbpm/blob/master/jbpm-persistence-jpa/src/main/java/org/jbpm/persistence/processinstance/JPAProcessInstanceManager.java</a></div>

<div><br></div><div>So JBPM can persist ProcessInstanceInfo entities but there&#39;s no support for JTA transactions, the code doesn&#39;t join a current JTA transaction, it even doesn&#39;t use transactions at all.</div>

<div><br></div><div>The problem arises when using Hibernate as EntityManager for persistence and running in a fireUntilHalt loop, given the lack of transactions support in JBPM, when a processinstance gets created and JBPM tries to persist it Hibernate delays the insertion as there is no transaction involved, then the primary key stays null and so the NPE.</div>

<div><br></div><div><br></div><div><br></div><div><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 Mon, Mar 19, 2012 at 22:34, Marco Rietveld <span dir="ltr">&lt;<a href="mailto:mrietvel@redhat.com">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">
    Sorry, to be clear: a github link to the code you quoted is what I
    was after. <br>
    <br>
    03/19/2012 10:33 PM, Marco Rietveld:
    <div><div class="h5"><blockquote type="cite">
      
      Alberto, <br>
      <br>
      Sorry to hear that the bug isn&#39;t fixed. <br>
      <br>
      Could you clarify what you mean by these points? <br>
         - Drools and JBPM have different persistence managers.<br>
         - Drools uses JTA for persistence and JBPM does not.<br>
      <br>
      I was pretty sure you could specify a JTA data source in the
      persistence.xml that jBPM uses. <br>
      <br>
      As for the persistence managers, what do you mean exactly? <br>
      <br>
      Also, if you could maybe give me a github link referring to the
      code you&#39;re using, that would be great. <br>
      <br>
      (Just to make sure, you are referring to jbpm <b>5</b>, right?)<br>
      <br>
      Thanks,<br>
      Marco<br>
      <br>
      <br>
      03/19/2012 06:23 PM, Alberto R. Galdo:
      <blockquote type="cite">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 =
        ((​ProcessPersistenceContextManag​er)
        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" target="_blank">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" target="_blank">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><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>
              <div><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.​DefaultConsequenceExceptionHan​dler.handleException(​DefaultConsequenceExceptionHan​dler.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_​ProcessDefaultConsequenceInvok​er.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>            =
                                ((​ProcessPersistenceContextManag​er)
                                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>
      </blockquote>
      <br>
      <br>
      <pre cols="72">-- 
jBPM/Drools developer
Utrecht, the Netherlands</pre>
    </blockquote>
    <br>
    <br>
    <pre cols="72">-- 
jBPM/Drools developer
Utrecht, the Netherlands</pre>
  </div></div></div>

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