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:

/**
* Maintains a static reference to a Spring {@link ApplicationContext}, and a
* static getContext() method, which will initialize the context if necessary.

* @author Stephen Masters
*/
public class GuvnorEnumContext {

private static ApplicationContext ctx = null;

GuvnorEnumContext() {
}

GuvnorEnumContext(ApplicationContext applicationContext) {
ctx = applicationContext;
}

public static ApplicationContext getContext() {
if (ctx == null) {
ctx = new AnnotationConfigApplicationContext(GuvnorEnumConfig.class);
}
return ctx;
}

}

 

/**
* Due to the fashion in which Guvnor enums are invoked, we maintain a
* static reference to the Spring {@link ApplicationContext} in the
* {@link GuvnorEnumContext}. This ensures that the context is only instantiated
* once.
*/
private static MyFinder finder() {
return (MyFinder) GuvnorEnumContext.getContext().getBean("myFinder");



On 28 Mar 2013, at 10:17, Stephen Masters <stephen.masters@me.com> wrote:

That sounds like a decent design to me. I don't much like having persistence inside the rules.

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?

Steve


On 28 Mar 2013, at 08:46, riri <irina.adam@gmail.com> wrote:

Hello everyone,

I am currently working on a Spring, Hibernate, Bitronix project and am
trying to integrate Drools functionality. I have a set of JPA Entities that
I can manage using Spring Data and Hibernate and I would like to use these
entities as facts inside a StatefulKnowledgeSession. As I understand it, I
need to have some mechanism of keeping the facts inside the working memory
in sync with the database and what I mean by that is:
1. update the facts in working memory when they are changed from outside
2. update the facts in the database when a RHS part of a rule changes
properties

This article  http://www.ibm.com/developerworks/java/library/j-drools5/
<http://www.ibm.com/developerworks/java/library/j-drools5/>   gives some
pointers on how to achieve 1. using AOP but in my case I do not have a
global knowledge session to insert, I need to be able to create a knowledge
base for each user of the application since each user will need to have his
own set of rules independent from other users.

What would be the best practices for achieving the synchronisation?
Currently for 1. I am updating the fact manually by retrieving the object
and calling update from java code. For 2. I have a
WorkingMemoryEventListener that is configured as a Spring @Component with
autowired services that can perform CRUD actions on entities and which are
called on objectUpdated.

I am new to all these technologies so I do not know if this is a good way to
go. I have a unit test where a rule modifies a property and the object is
updated in the database but if the Thread with the fireUntilHalt method is
not paused, the test finishes and the JTA manager is shut down before the
rules can fire and the transaction in the objectUpdated can be executed.

I would be very grateful for any insight from the more experienced users
that have come across such requirements before.

Best regards



--
View this message in context: http://drools.46999.n3.nabble.com/Working-memory-database-synchronisation-approach-tp4023094.html
Sent from the Drools: User forum mailing list archive at Nabble.com.
_______________________________________________
rules-users mailing list
rules-users@lists.jboss.org
https://lists.jboss.org/mailman/listinfo/rules-users


_______________________________________________
rules-users mailing list
rules-users@lists.jboss.org
https://lists.jboss.org/mailman/listinfo/rules-users