tkobayas commented on issue #6318:
URL: 
https://github.com/apache/incubator-kie-drools/issues/6318#issuecomment-2826571909

   Hi @gpadbidri ,
   
   App's `evaluateRules` method has a concurrency issue. It builds rules 
without specifying a ReleaseId, so the default ReleaseId is 
"org.default:artifact:1.0.0". The created `KieModule` is cached in singleton 
KieRepositoryImpl with a key "org.default:artifact:1.0.0". Then 
`kieServices.newKieContainer(kieModule.getReleaseId())` takes the `KieModule`. 
If `evaluateRules` method is executed concurrently, one thread may use a 
`KieModule` which is built by another thread. It explains that it hits 
"Unexpected global".
   
   Solutions would be:
   
   A) Specify a unique ReleaseId
   
   You can specify a unique ReleaseId like this:
   
   ```
           // Instead of these code...
           // KieBuilder kieBuilder = kieServices.newKieBuilder(kieFileSystem);
           // kieBuilder.buildAll();
           // KieModule kieModule = kieBuilder.getKieModule();
           // session = 
kieServices.newKieContainer(kieModule.getReleaseId()).newKieSession();
   
           ReleaseId releaseId = kieServices.newReleaseId("com.sample", 
"my-sample-" + UUID.randomUUID(), "1.0.0");
           kieFileSystem.generateAndWritePomXML(releaseId);
           kieServices.newKieBuilder(kieFileSystem).buildAll();
           session = kieServices.newKieContainer(releaseId).newKieSession();
   ```
   
   also remove the kieModule at the end of the mothod.
   
   ```
       } finally {
           ...
           kieServices.getRepository().removeKieModule(releaseId);
       }
   ```
   
   or
   
   B) Redesign to share rules
   
   Building rules for every request is an excessive approach and I haven't seen 
before. I hope you can generalize the rules and change the behavior using facts 
and globals. Then, you will be able to build the rules only once, share the 
KieContainer and safely call newKieSession() in multithreads.


-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: [email protected]

For queries about this service, please contact Infrastructure at:
[email protected]


---------------------------------------------------------------------
To unsubscribe, e-mail: [email protected]
For additional commands, e-mail: [email protected]

Reply via email to