What is difference between stateless and stateful knowledge sessions.I read some documents both are maintained state.But when can i use stateless/stateful knowledge sessions.
Stateless: The facts/working memory is inserted to Knowledge base session before firing rules. These facts can be set by calling public methods on an object while executing rules and after setting these objects are returned back with changed values.
Any changes in the facts while executing rules, for example insert(xyz)
or modify(xyz)
, is not made aware to the rule engine.
Stateful: The facts/working memory is inserted to Knowledge base session before firing rules and after the rules are fired dispose() has to be called to avoid memory leaks.
Any changes in the facts while executing rules, for example insert(xyz)
or modify(xyz)
, is made aware to the rule engine.
Stateless means a new session is created for each request (so no state is maintained). Stateful means it will continue from whatever state the session was when the previous command ended (for example, all data that was inserted into the session will still be there).
The basic difference the way i see it, is the way the session is auto disposed in stateless. There are no performance gain to be had by choosing one vs. other. Actually, the stateless session uses a stateful session behind it. So go figure!
I want to quote the drools documentation here which cleared my mind.
"StatelessKnowledgeSession provides a convenience API, wrapping StatefulKnowledgeSession. It avoids the need to call dispose(). Stateless sessions do not support iterative invocations, the act of calling execute(...) is a single shot method that will internally instantiate a StatefulKnowledgeSession, add all the user data and execute user commands, call fireAllRules, and then call dispose()."
So basically, stateless session is a stateful session used one time.
This then imply that stateless session can also do inference, unlike many documents and some answers here said! This should only depend on the "then" part of the rule, whether you use "modify" or not.
While I have not verify this myself, this post seems support my reasoning.
https://groups.google.com/forum/#!topic/drools-usage/qYbqiS1ht4g
In stateful sessions, we can modify facts and reinsert them even after having the rules fired before.
In stateless sessions, on the other hand, once all the rules have been fired ( using execute()
), we cannot further modify the facts and reinsert them into the session ( as the session is unusable after execution()
is called ).
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With