I am using Drools with Spring Boot. In my project, I am making singleton Beans of KieSession, KieContainer and KieServices. The KieSession will be used in different singleton services and controllers. I wanted to know whether KieSesion.insert()
and KieSession.fireAllRules()
are thread-safe with singleton bean implementations. Or should I create a utility class having synchronized access to KieSesion.insert() and KieSession.fireAllRules()?
The KieSession
has been threadsafe since Drools 6, from the manual
Engine’s code dealing with multi-threading has been partially rewritten in order to remove a large number of synchronisation points and improve stability and predictability. In particular this new implementation allows a clearer separation and better interaction between the User thread (performing the insert/update/delete actions on the session), the Drools engine thread (doing the proper rules evaluation) and the Timer one (performing time-based actions like events expiration).
Since 7.52.0.Final
, users can disable thread-safety if not needed to gain some performance. Here's the relevant release note:
As per the default configuration, a KieSession is thread-safe and can be shared safely and used by multiple threads at the same time. However, if a KieSession is running, it requires additional synchronization points to support the thread-safety, which is not required, and eventually, it slows the performance of the KieSession. Therefore, a new ThreadSafeOption is introduced, which you can use to optionally disable the thread-safety. The ThreadSafeOption consists of two values including YES (default) and NO.