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?