0

we are moving along to persist drools session with JPAKnowledgeService. we configure and implement according to the guide in drools user manual as below:

        KnowledgeBase kbase = KnowledgeBaseFactory.newKnowledgeBase();
        loadDRL(kbase); // method to load the DRL rules to kbase

        Environment env = KnowledgeBaseFactory.newEnvironment();
        env.set( EnvironmentName.ENTITY_MANAGER_FACTORY, Persistence.createEntityManagerFactory( "org.drools.persistence.jpa"));
        env.set( EnvironmentName.TRANSACTION_MANAGER, TransactionManagerServices.getTransactionManager());

StatefulKnowledgeSession ksession = JPAKnowledgeService.newStatefulKnowledgeSession(kbase, null, env);
        ksession.insert(new Event());
        ksession.fireAllRules();

        ksession = JPAKnowledgeService.loadStatefulKnowledgeSession(ksession.getId(), kbase, null, env);
        ksession.insert(new Event());
        ksession.fireAllRules();

        ksession.dispose();

The first time we use the stateful session, we need to new it JPAKnowledgeService.newStatefulKnowledgeSession(kbase, null, env). the second or third time we use the same session in same thread, we can reload the session from DB JPAKnowledgeService.loadStatefulKnowledgeSession(ksession.getId(), kbase, null, env) or just use the session in working memory.

but even to the same place, in a concurrent environment, how do we determine if need to load the session from DB or just use the session in the working memory? will it auto detect if there is session in the working memory and if not will load from DB?

Also how about in the spring configure environment, we cannot control if to new the session or load the session from DB or just use the session in working memory, how it performs?

Scarlett
  • 731
  • 8
  • 19
  • Using Spring, I would recommend creating a `@Service` which acts as a session factory. That way you get one instance of that service and it can hold a map of session IDs to sessions. If the session isn't in the map, then load it from the DB. – Steve Feb 20 '14 at 08:48
  • 1
    And ... if you have one knowledge session per user session, then you could also use session scoped beans, which load from DB on startup. – Steve Feb 20 '14 at 08:51
  • @Steve can you share the link/code of session scoped beans? I have posted another question http://stackoverflow.com/questions/21928187/how-to-configure-to-reload-the-drools-knowledge-session-when-servertomcat-rest. during my testing I cannot make it reload from DB on startup. is there anything I miss? – Scarlett Feb 21 '14 at 07:40
  • You annotate a `@Service` with `@Scope("session")`. `@Autowire` it into a `@Controller` and each user will have their own personal service instance associated with their HTTP session. – Steve Feb 21 '14 at 09:08

0 Answers0