:)<div>Keep your mind open, because there is no single solution for all the problems. <br><br><div class="gmail_quote">On Tue, May 29, 2012 at 1:15 PM, 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">Thank you very much Mauricio. You&#39;ve been of great help. Now is time for us to cook all this valuable information and go for a solution. xD<span class="HOEnZb"><font color="#888888"><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 Tue, May 29, 2012 at 5:59 PM, Mauricio Salatino <span dir="ltr">&lt;<a href="mailto:salaboy@gmail.com" target="_blank">salaboy@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">



Comments inline, <br><br><div class="gmail_quote"><div>On Tue, May 29, 2012 at 12:24 PM, 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">

   Sure!. I didn&#39;t mention that we&#39;re running in a OSGi environment and we&#39;re getting the session from JPAKnowledgeService like that:<br>

<p class="MsoNormal"><span style="font-size:10.0pt;line-height:115%"> </span></p>

<p style="margin-left:40px" class="MsoNormal"><font size="1"><span style="line-height:115%" lang="EN-US">EntityManagerFactory emf =
Persistence.createEntityManagerFactory(&quot;com.package&quot;);</span></font></p>

<p style="margin-left:40px" class="MsoNormal"><font size="1"><span style="line-height:115%" lang="EN-US">Environment env =
KnowledgeBaseFactory.newEnvironment();</span></font></p>

<p style="margin-left:40px" class="MsoNormal"><font size="1"><span style="line-height:115%" lang="EN-US">env.set(EnvironmentName.ENTITY_MANAGER_FACTORY, emf);</span></font></p>

<p style="margin-left:40px" class="MsoNormal"><font size="1"><span style="line-height:115%" lang="EN-US">env.set(EnvironmentName.TRANSACTION_MANAGER,(TransactionManager)
bundleContext.getService(this.tmServiceRef));</span></font></p>

<p style="margin-left:40px" class="MsoNormal"><font size="1"><span style="line-height:115%" lang="EN-US">env.set(EnvironmentName.TRANSACTION, (UserTransaction)
bundleContext.getService(this.userTransactionServiceRef));</span></font></p>

<p style="margin-left:40px" class="MsoNormal"><font size="1"><span style="line-height:115%" lang="EN-US">env.set(EnvironmentName.GLOBALS, new
MapGlobalResolver());</span></font></p>

<p style="margin-left:40px" class="MsoNormal"><font size="1"><span style="line-height:115%" lang="EN-US">env.set(EnvironmentName.OBJECT_MARSHALLING_STRATEGIES,
new ObjectMarshallingStrategy[] {
MarshallerFactory.newSerializeMarshallingStrategy() });</span></font></p>

<p style="margin-left:40px" class="MsoNormal"><font size="1"><span style="line-height:115%" lang="EN-US">final StatefulKnowledgeSession ksession =
JPAKnowledgeService.newStatefulKnowledgeSession(kbase, sessionConfig, env);</span></font></p><p class="MsoNormal"><br></p><p class="MsoNormal">   We are just following the manual ( and some acquired knowledge through the days ... ) .... I
 understand that inserting an event and starting a process at the same 
time would lead to the current situation, but Isn&#39;t this the reason why 
we are using drools with drools fusion + jbpm to build a CEP?</p></blockquote></div><div>I&#39;m not sure about your reasons :) </div><div><blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex">







<p class="MsoNormal"><br></p>   We chose Drools and a 
statefulsession in a fireUntilHalt loop just because we would be able to
 have our rules and processes reacting to events that enter the system 
at their own pace ... and found ourselves in this nightmare when trying 
to make the system fault-tolerant through persistence... :(<br><br></blockquote></div><div>FireUntilHalt and Persistence will cause &quot;conflicts&quot;, because fireUntilHalts by default creates a different thread == race conditions.</div>



<div>

<div> </div><blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex">   Do you think we should change our fault-tolerance strategy by reconstructing the knowledge session, process and tasks state by our own means leaving Drools and JBPM JPA persistence appart?<span><font color="#888888"><br>







<br></font></span></blockquote></div><div> A common problem is to think that a session will do everything for you. I&#39;m not saying that you need to separate your processes from your rules, I&#39;m just saying that probably decoupling responsibilities will help you to tackle down some of the problems that you are having. If you have in memory processes and fusion entry points, you can keep those together and in a separate persistence session handle all the human task interactions and long running processes. If you are getting events in real time and you want to process them, you cannot expect to persist all the session state and have no performance impacts. </div>





<blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex"><span><font color="#888888"><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></div></div></blockquote><div><div><div>Cheers </div><blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex"><div><div><br><div class="gmail_quote">On Tue, May 29, 2012 at 4:43 PM, Mauricio Salatino <span dir="ltr">&lt;<a href="mailto:salaboy@gmail.com" target="_blank">salaboy@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">

Ok, so probably I&#39;m not understanding your scenario, but are you using the JPAKnowledgeService to create persistent sessions?<div>If you use that everything is aware of everything :) Drools and jBPM5 will run using the same persistence resources. For each interaction: startProcess, insert, update, fireAllRules a transaction will be created. If you are starting a process and at the same time inserting a Fusion event you will be generating two transaction that will fight for the resources (the same resources because you have only one session). </div>









<div>Obviously if you take the persistence mechanisms outside of the discussion everything will work perfectly fine, because you don&#39;t have any transactional resource to fight for :)</div><div><br></div><div>Hope it helps.</div>









<div>Cheers <div><div><br><br><div class="gmail_quote">On Tue, May 29, 2012 at 11:36 AM, 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">

   Your answer is confusing me, I think I&#39;m not getting it right. <br><br>   It seems that we you&#39;re saying that will have to modify JBPM or Drools behaviour to get our architecture done ... but I don&#39;t think we are doing nothing special than what Drools &amp; JBPM are able to do in a non-persisting enviroment ( at least without JBPM persisted ).<br>











<br>   Maybe deeping a bit more the architecture will lead to a better understanding of the problem:<br><br>   We have a Drools StatefulKnowledgeSession wich is populated with packages of rules and different BPMN 2.0 process definitions. That session in Drools is persisted using JPA and our JTA provider. Our solution is a CEP that uses events that get into the session using Drools Fusion. There, several rules fire and then several consequences start processes wich contain both Tasks and HumanTasks. Those rule activations are already in a queue, the Agenda. Given that JBPM acts as the processmanager provider for Drools, our processes get started in JBPM, and it is persisted using it&#39;s own JPA manager.  Just rules, that start processes in response to events in a fireUntilHalt loop in a persistent enviroment, both for Drools &amp; JBPM. Then some processes interact with the knowledge session using BussinessTask nodes( which make other rules fire and maybe launch a different set of processes ) and other simply get to the Stop node. I see nothing wrong here, in fact, taking the persistence appart from the equation, everything runs as beautifuly as expected. We&#39;ve tested that.<br>











<br>   I&#39;m just trying to undersand the problem, I&#39;m pretty 90% sure I&#39;m wrong, but what I think is happening here is that Drools isn&#39;t aware that JBPM may be using the same resources and vice-versa, Isn&#39;t it?<br>











<br>   Shouldn&#39;t the solution be to make Drools &amp; JBPM aware of each other in what is related to the syncronization of the transaction management to avoid race conditions?<span><font color="#888888"><br>

<br><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><div class="gmail_quote">On Tue, May 29, 2012 at 3:58 PM, Mauricio Salatino <span dir="ltr">&lt;<a href="mailto:salaboy@gmail.com" target="_blank">salaboy@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">I&#39;m in no way saying this:<div><div>&quot;  Let me see if I&#39;m getting this right, What you mean is that there is no way for the JPA persistence of Drools &amp; JBPM to coexist in the same knowledge session because of race conditions between them competing for their shared resources? I&#39;m afraid so.. :(</div>













<div><br></div></div><div>They can coexist. As you can see they are working together. If you take a look at the underlying layers you will see that each operation that you run against the session is wrapped in a transaction. That leads to your architecture, If you have a single entry point for your session everything will work correctly. You can achieve this by receiving all the operations in a queue and then execute one after the other. </div>













<div>If you have multiple threads interacting with the session, you can implement a retrying mechanism if the transaction gets rolled back and as soon as the transaction win the right to commit it will work. It really depends on the problem that you are trying to solve.</div>













<div><br></div><div>Cheers</div><div><div><div><br><div><div><div class="gmail_quote">On Tue, May 29, 2012 at 10:42 AM, 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">   Thanks Mauricio for your kind response, and kick, as usual ;-)<br><br>   Sure, we see lots of rolled back transactions ... Everywhere!<br>













<br>   Let me see if I&#39;m getting this right, What you mean is that there is no way for the JPA persistence of Drools &amp; JBPM to coexist in the same knowledge session because of race conditions between them competing for their shared resources? I&#39;m afraid so.. :(<br>















<br>   If it is so, Are there any plans for the integration of a shared JPA persistence management of both Drools &amp; JBPM, wich may be the solution for this problem? ... Will we be better thinking to change our architecture ASAP?<br>















<br>Greets,<br><br clear="all">Alberto R. Galdo<br><a href="mailto:argaldo@gmail.com" target="_blank">argaldo@gmail.com</a><div><div><br><br><br><div class="gmail_quote">On Tue, May 29, 2012 at 1:54 PM, Mauricio Salatino <span dir="ltr">&lt;<a href="mailto:salaboy@gmail.com" target="_blank">salaboy@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">Ok, so what is happening is that two or more threads are fighting for the resources, causing that two or more transactions wants to be completed, but just one can win. All the other must be retried. Are you seeing rolled back exceptions in your stack trace? <div>

















My question to you is if you really need to handle everything in just one big persistent session. I&#39;ve solved similar issues in the past using more than one session, for example one session to run business processes(persistent) and one or more for receiving and reacting to events. If you have an async way to communicate both sessions there should be no problem.</div>

















<div>Hope it helps!</div><div>Cheers<br><br><div class="gmail_quote"><div><div>On Tue, May 29, 2012 at 7:30 AM, Alberto R. Galdo <span dir="ltr">&lt;<a href="mailto:argaldo@gmail.com" target="_blank">argaldo@gmail.com</a>&gt;</span> wrote:<br>

















</div></div><blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex"><div><div>Hi all,<br><br>   We&#39;ve been for long time now developing a complex event processing system that ( simplified version ahead !! ) involves a set of rules that in turn activates a set processes that fulfill human tasks and other kind of tasks.. This all is running in a StatefulKnowledgeSession with JPA persistence configured both for Drools and JBPM. We are running an stack of Drools, JBPM, Drools Integration, Drools fussion, etc..<br>



















<br>   We&#39;ve been able to persist Drools sessions &amp; JBPM processes in the same Persistence Context  ( not without pain :( ) using different JTA implementations including ( but not limited to ) Bitronix &amp; Atomikos.<br>



















<br>    What we are observing here is what seems to be some kind of race 
condition between Drools and JBPM when running the knowledge session when JPA&amp;JTA persistence is configured. Very often, as soon as after 2-3 
processes get created as rule&#39;s consequences are fired in response of events inside the session we see how JBPM finds its instance nullified by Drools when
 it tries to end a process and persist it.<br><br>   We&#39;ve been able to find where Drools decides to delete an instance of the process ... at a given time Drools executes JPAProcessInstanceManager.clearProcessInstances() [1] when it finalizes a SingleSessionCommand wich in turn calls disconnect() for *all* the local-stored processinstances ( wich gets populated with instances of processes every time a process is started in the knowledge session ):<br>



















<br><pre><div>    <span>public</span> <span>void</span> <span>clearProcessInstances</span><span>()</span> <span>{</span></div><div>

        <span>for</span> <span>(</span><span>ProcessInstance</span> <span>processInstance:</span> <span>new</span> <span>ArrayList</span><span>&lt;</span><span>ProcessInstance</span><span>&gt;(</span><span>processInstances</span><span>.</span><span>values</span><span>()))</span> <span>{</span></div>



















<div>            <span>((</span><span>ProcessInstanceImpl</span><span>)</span> <span>processInstance</span><span>).</span><span>disconnect</span><span>();</span></div>

<div>        <span>}</span></div><div>    <span>}</span></div></pre>    <br>    So, Drools decides to disconnect all process instances in it&#39;s JPA context without taking in account the state the process is in, and when an processinstance that is not stopped gets removed then JBPM finds it&#39;s NullPointerException...<br>



















<br>    We&#39;ve modified the code to make Drools aware of the state of the process before wiping it from the context   ( no problem here, there will be no leak as a running processinstance will be removed in future calls of &quot;clearProcessInstances&quot; given the process is closed ). But unfortunatelly this seems to resolve this problem, but lots of other problems ( wich seems also race conditions arise :  for instance: Drools closes connections to the database and JBPM finds the connection closed, <br>



















<br><br>    So, we are really worried about using Drools &amp; JBPM in a persisted environment. Maybe our asumptions are wrong...  Is it possible to have an scenario like ours given the current Drools &amp; JBPM integration status for a persistent statefulKnowledge Session?  Did anyone build a complex event processing system like ours in a unaltered persistence environment such as provided in Drools and JBPM by default?<br>



















<br><br>Greets,<br><br>[1] <a href="https://github.com/droolsjbpm/jbpm/blob/master/jbpm-persistence-jpa/src/main/java/org/jbpm/persistence/processinstance/JPAProcessInstanceManager.java" target="_blank">https://github.com/droolsjbpm/jbpm/blob/master/jbpm-persistence-jpa/src/main/java/org/jbpm/persistence/processinstance/JPAProcessInstanceManager.java</a><span><font color="#888888"><br>



















<br><br><br><br><br><br clear="all">Alberto R. Galdo<br><a href="mailto:argaldo@gmail.com" target="_blank">argaldo@gmail.co</a><br>
</font></span><br></div></div>_______________________________________________<br>
rules-users mailing list<br>
<a href="mailto:rules-users@lists.jboss.org" target="_blank">rules-users@lists.jboss.org</a><br>
<a href="https://lists.jboss.org/mailman/listinfo/rules-users" target="_blank">https://lists.jboss.org/mailman/listinfo/rules-users</a><br>
<br></blockquote></div><span><font color="#888888"><br><br clear="all"><div><br></div>-- <br> - MyJourney @ <a href="http://salaboy.wordpress.com" target="_blank">http://salaboy.wordpress.com</a><div> - Co-Founder @ <a href="http://www.jugargentina.org" target="_blank">http://www.jugargentina.org</a><br>

















 - Co-Founder @ <a href="http://www.jbug.com.ar" target="_blank">http://www.jbug.com.ar</a><br> <br> - Salatino &quot;Salaboy&quot; Mauricio -</div><br>
</font></span></div>
<br>_______________________________________________<br>
rules-users mailing list<br>
<a href="mailto:rules-users@lists.jboss.org" target="_blank">rules-users@lists.jboss.org</a><br>
<a href="https://lists.jboss.org/mailman/listinfo/rules-users" target="_blank">https://lists.jboss.org/mailman/listinfo/rules-users</a><br>
<br></blockquote></div><br>
</div></div><br>_______________________________________________<br>
rules-users mailing list<br>
<a href="mailto:rules-users@lists.jboss.org" target="_blank">rules-users@lists.jboss.org</a><br>
<a href="https://lists.jboss.org/mailman/listinfo/rules-users" target="_blank">https://lists.jboss.org/mailman/listinfo/rules-users</a><br>
<br></blockquote></div><br><br clear="all"><div><br></div>-- <br> - MyJourney @ <a href="http://salaboy.wordpress.com" target="_blank">http://salaboy.wordpress.com</a><div> - Co-Founder @ <a href="http://www.jugargentina.org" target="_blank">http://www.jugargentina.org</a><br>













 - Co-Founder @ <a href="http://www.jbug.com.ar" target="_blank">http://www.jbug.com.ar</a><br> <br> - Salatino &quot;Salaboy&quot; Mauricio -</div><br>
</div></div></div>
</div></div><br>_______________________________________________<br>
rules-users mailing list<br>
<a href="mailto:rules-users@lists.jboss.org" target="_blank">rules-users@lists.jboss.org</a><br>
<a href="https://lists.jboss.org/mailman/listinfo/rules-users" target="_blank">https://lists.jboss.org/mailman/listinfo/rules-users</a><br>
<br></blockquote></div><br>
</div></div><br>_______________________________________________<br>
rules-users mailing list<br>
<a href="mailto:rules-users@lists.jboss.org" target="_blank">rules-users@lists.jboss.org</a><br>
<a href="https://lists.jboss.org/mailman/listinfo/rules-users" target="_blank">https://lists.jboss.org/mailman/listinfo/rules-users</a><br>
<br></blockquote></div><br><br clear="all"><div><br></div>-- <br> - MyJourney @ <a href="http://salaboy.wordpress.com" target="_blank">http://salaboy.wordpress.com</a><div> - Co-Founder @ <a href="http://www.jugargentina.org" target="_blank">http://www.jugargentina.org</a><br>









 - Co-Founder @ <a href="http://www.jbug.com.ar" target="_blank">http://www.jbug.com.ar</a><br> <br> - Salatino &quot;Salaboy&quot; Mauricio -</div><br>
</div></div></div>
<br>_______________________________________________<br>
rules-users mailing list<br>
<a href="mailto:rules-users@lists.jboss.org" target="_blank">rules-users@lists.jboss.org</a><br>
<a href="https://lists.jboss.org/mailman/listinfo/rules-users" target="_blank">https://lists.jboss.org/mailman/listinfo/rules-users</a><br>
<br></blockquote></div><br>
</div></div><br>_______________________________________________<br>
rules-users mailing list<br>
<a href="mailto:rules-users@lists.jboss.org" target="_blank">rules-users@lists.jboss.org</a><br>
<a href="https://lists.jboss.org/mailman/listinfo/rules-users" target="_blank">https://lists.jboss.org/mailman/listinfo/rules-users</a><br>
<br></blockquote></div></div></div><div><div><br><br clear="all"><div><br></div>-- <br> - MyJourney @ <a href="http://salaboy.wordpress.com" target="_blank">http://salaboy.wordpress.com</a><div>

 - Co-Founder @ <a href="http://www.jugargentina.org" target="_blank">http://www.jugargentina.org</a><br>

 - Co-Founder @ <a href="http://www.jbug.com.ar" target="_blank">http://www.jbug.com.ar</a><br> <br> - Salatino &quot;Salaboy&quot; Mauricio -</div><br>
</div></div><br>_______________________________________________<br>
rules-users mailing list<br>
<a href="mailto:rules-users@lists.jboss.org" target="_blank">rules-users@lists.jboss.org</a><br>
<a href="https://lists.jboss.org/mailman/listinfo/rules-users" target="_blank">https://lists.jboss.org/mailman/listinfo/rules-users</a><br>
<br></blockquote></div><br>
</div></div><br>_______________________________________________<br>
rules-users mailing list<br>
<a href="mailto:rules-users@lists.jboss.org">rules-users@lists.jboss.org</a><br>
<a href="https://lists.jboss.org/mailman/listinfo/rules-users" target="_blank">https://lists.jboss.org/mailman/listinfo/rules-users</a><br>
<br></blockquote></div><br><br clear="all"><div><br></div>-- <br> - MyJourney @ <a href="http://salaboy.wordpress.com" target="_blank">http://salaboy.wordpress.com</a><div> - Co-Founder @ <a href="http://www.jugargentina.org" target="_blank">http://www.jugargentina.org</a><br>

 - Co-Founder @ <a href="http://www.jbug.com.ar" target="_blank">http://www.jbug.com.ar</a><br> <br> - Salatino &quot;Salaboy&quot; Mauricio -</div><br>
</div>