<html><head><meta http-equiv="Content-Type" content="text/html charset=us-ascii"></head><body style="word-wrap: break-word; -webkit-nbsp-mode: space; -webkit-line-break: after-white-space; "><div>fyi - There may be simpler/cleaner ways of doing it, but if you wish to ensure that you always have access to your Spring context even when you're out roaming on a separate thread, you can get static access to the context like this below. I use it so that I can use Spring to help me get data for Guvnor enums:</div><div><br></div><div><!--?xml version="1.0" encoding="UTF-8" standalone="no"?-->

<p style="font-family: Arial; ">/**<br clear="none">* Maintains a static reference to a Spring {@link ApplicationContext}, and a<br clear="none">* static getContext() method, which will initialize the context if necessary.<br clear="none">*&nbsp;<br clear="none">* @author Stephen Masters<br clear="none">*/<br clear="none">public class GuvnorEnumContext {
</p><p style="font-family: Arial; ">private static ApplicationContext ctx = null;
</p><p style="font-family: Arial; ">GuvnorEnumContext() {<br clear="none">}
</p><p style="font-family: Arial; ">GuvnorEnumContext(ApplicationContext applicationContext) {<br clear="none">ctx = applicationContext;<br clear="none">}
</p><p style="font-family: Arial; ">public static ApplicationContext getContext() {<br clear="none">if (ctx == null) {<br clear="none">ctx = new AnnotationConfigApplicationContext(GuvnorEnumConfig.class);<br clear="none">}<br clear="none">return ctx;<br clear="none">}
</p><p style="font-family: Arial; ">}
</p><div style="font-family: Arial; ">&nbsp;
<br class="webkit-block-placeholder"></div><p style="font-family: Arial; ">/**<br clear="none">* Due to the fashion in which Guvnor enums are invoked, we maintain a<br clear="none">* static reference to the Spring {@link ApplicationContext} in the<br clear="none">* {@link GuvnorEnumContext}. This ensures that the context is only instantiated<br clear="none">* once.<br clear="none">*/<br clear="none">private static MyFinder finder() {<br clear="none">return (MyFinder) GuvnorEnumContext.getContext().getBean("myFinder");<br clear="none">}&nbsp;</p></div><div><br></div><br><div><div>On 28 Mar 2013, at 10:17, Stephen Masters &lt;<a href="mailto:stephen.masters@me.com">stephen.masters@me.com</a>&gt; wrote:</div><br class="Apple-interchange-newline"><blockquote type="cite">That sounds like a decent design to me. I don't much like having persistence inside the rules.<br><br>The issue you have seems more related to the fact that you're executing the rules in a separate thread than your persistence design. I don't suppose you're actually spawning a thread from inside a JTA transaction in which to run your rules?<br><br>Steve<br><br><br>On 28 Mar 2013, at 08:46, riri &lt;<a href="mailto:irina.adam@gmail.com">irina.adam@gmail.com</a>&gt; wrote:<br><br><blockquote type="cite">Hello everyone,<br><br>I am currently working on a Spring, Hibernate, Bitronix project and am<br>trying to integrate Drools functionality. I have a set of JPA Entities that<br>I can manage using Spring Data and Hibernate and I would like to use these<br>entities as facts inside a StatefulKnowledgeSession. As I understand it, I<br>need to have some mechanism of keeping the facts inside the working memory<br>in sync with the database and what I mean by that is:<br>1. update the facts in working memory when they are changed from outside<br>2. update the facts in the database when a RHS part of a rule changes<br>properties<br><br>This article &nbsp;<a href="http://www.ibm.com/developerworks/java/library/j-drools5/">http://www.ibm.com/developerworks/java/library/j-drools5/</a><br>&lt;<a href="http://www.ibm.com/developerworks/java/library/j-drools5/">http://www.ibm.com/developerworks/java/library/j-drools5/</a>&gt; &nbsp;&nbsp;gives some<br>pointers on how to achieve 1. using AOP but in my case I do not have a<br>global knowledge session to insert, I need to be able to create a knowledge<br>base for each user of the application since each user will need to have his<br>own set of rules independent from other users.<br><br>What would be the best practices for achieving the synchronisation?<br>Currently for 1. I am updating the fact manually by retrieving the object<br>and calling update from java code. For 2. I have a<br>WorkingMemoryEventListener that is configured as a Spring @Component with<br>autowired services that can perform CRUD actions on entities and which are<br>called on objectUpdated.<br><br>I am new to all these technologies so I do not know if this is a good way to<br>go. I have a unit test where a rule modifies a property and the object is<br>updated in the database but if the Thread with the fireUntilHalt method is<br>not paused, the test finishes and the JTA manager is shut down before the<br>rules can fire and the transaction in the objectUpdated can be executed.<br><br>I would be very grateful for any insight from the more experienced users<br>that have come across such requirements before.<br><br>Best regards<br><br><br><br>--<br>View this message in context: <a href="http://drools.46999.n3.nabble.com/Working-memory-database-synchronisation-approach-tp4023094.html">http://drools.46999.n3.nabble.com/Working-memory-database-synchronisation-approach-tp4023094.html</a><br>Sent from the Drools: User forum mailing list archive at <a href="http://Nabble.com">Nabble.com</a>.<br>_______________________________________________<br>rules-users mailing list<br><a href="mailto:rules-users@lists.jboss.org">rules-users@lists.jboss.org</a><br>https://lists.jboss.org/mailman/listinfo/rules-users<br></blockquote><br><br>_______________________________________________<br>rules-users mailing list<br><a href="mailto:rules-users@lists.jboss.org">rules-users@lists.jboss.org</a><br>https://lists.jboss.org/mailman/listinfo/rules-users<br></blockquote></div><br></body></html>