<div dir="ltr"><br>&nbsp;&nbsp; A session never was thread safe... it is supposed to be accessed by a single thread. <br>&nbsp;&nbsp; What *is* thread safe is a RuleBase, after its creation. So, if you change your code to have:<br><br>final StatefulSession statefulSession = ruleBase.newStatefulSession();<br>
<br>&nbsp;&nbsp;&nbsp; Inside your thread, it should work. <br>&nbsp;&nbsp;&nbsp; I.e., you share your rulebase, but creates a session for each thread, that seems precisely what you need, since your threads are (correctly) calling session.dispose().<br>
<br>&nbsp;&nbsp; []s<br>&nbsp;&nbsp; Edson<br><br><div class="gmail_quote">2008/8/6 Harsh Jetly <span dir="ltr">&lt;<a href="mailto:harsh.jetly@lntinfotech.com">harsh.jetly@lntinfotech.com</a>&gt;</span><br><blockquote class="gmail_quote" style="border-left: 1px solid rgb(204, 204, 204); margin: 0pt 0pt 0pt 0.8ex; padding-left: 1ex;">
<br>
<br>
<br>
ricky.nfs wrote:<br>
&gt;<br>
&gt; Hi there, I am a newbie on Drools. I tried to create a multi threading<br>
&gt; programs using drools. But in large amount of threads, the result in<br>
&gt; stateful session is not safe anymore. Can anyone help me?<br>
&gt;<br>
&gt; private final static Map&lt;Integer,Double&gt; score = new HashMap&lt;Integer,<br>
&gt; Double&gt;();<br>
&gt;<br>
&gt; &nbsp; &nbsp; &nbsp; static<br>
&gt; &nbsp; &nbsp; &nbsp; {<br>
&gt; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; score.put( 0 , 0.0 );<br>
&gt; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; score.put( 1 , 0.84 );<br>
&gt; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; score.put( 2 , 1.26 );<br>
&gt; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; score.put( 3 , 1.68 );<br>
&gt; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; score.put( 4 , 1.05 ); //Else<br>
&gt; &nbsp; &nbsp; &nbsp; }<br>
&gt;<br>
&gt; &nbsp; &nbsp; &nbsp; public static void main(String[] args) throws Exception{<br>
&gt;<br>
&gt; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; Reader source = new InputStreamReader(<br>
&gt; TestStatefulSimpe.class.getResourceAsStream( &quot;/simple/SimpleCATRule.dslr&quot;<br>
&gt; ) );<br>
&gt; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; Reader dsl = new InputStreamReader(<br>
&gt; TestStatefulSimpe.class.getResourceAsStream( &quot;/simple/SimpleCATRule.dsl&quot; )<br>
&gt; );<br>
&gt; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; PackageBuilder builder = new PackageBuilder();<br>
&gt; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; builder.addPackageFromDrl( source, dsl );<br>
&gt;<br>
&gt; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; //get the compiled package (which is serializable)<br>
&gt; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; Package pkg = builder.getPackage();<br>
&gt;<br>
&gt; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; //add the package to a rulebase (deploy the rule package).<br>
&gt; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; org.drools.RuleBase ruleBase = RuleBaseFactory.newRuleBase();<br>
&gt; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; ruleBase.addPackage( pkg );<br>
&gt;<br>
&gt; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; final StatefulSession statefulSession = ruleBase.newStatefulSession();<br>
&gt;<br>
&gt; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; for( int i = 0 ; i &lt; 200000 ; i++ )<br>
&gt; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; {<br>
&gt; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; final int idx = i;<br>
&gt; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; Thread t = new Thread( new Runnable(){<br>
&gt;<br>
&gt; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; @Override<br>
&gt; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; public void run() {<br>
&gt; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; int amount = (int) Math.round( ( Math.random()*4 ) );<br>
&gt; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; TestSimpleVO vo = new TestSimpleVO();<br>
&gt; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; vo.setDecisionMakersAmount( amount );<br>
&gt; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; FactHandle fact = statefulSession.insert( vo );<br>
&gt; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; statefulSession.fireAllRules();<br>
&gt; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; statefulSession.retract(fact);<br>
&gt; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; if( &nbsp;score.get(amount).doubleValue() != vo.getScoreFactor() )<br>
&gt; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; System.out.println( idx + &quot; - &quot; + &nbsp;amount + &quot; : &quot; +<br>
&gt; score.get(amount).doubleValue() + &quot; - &quot; + vo.getScoreFactor() );<br>
&gt; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; statefulSession.dispose();<br>
&gt; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; }});<br>
&gt; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; t.start();<br>
&gt; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; }<br>
&gt; &nbsp; &nbsp; &nbsp; }<br>
&gt;<br>
&gt; And the dslr and dlr attachment :<br>
&gt; &nbsp;<a href="http://www.nabble.com/file/p18564976/SimpleCATRule.dsl" target="_blank">http://www.nabble.com/file/p18564976/SimpleCATRule.dsl</a> SimpleCATRule.dsl<br>
&gt; &nbsp;<a href="http://www.nabble.com/file/p18564976/SimpleCATRule.dslr" target="_blank">http://www.nabble.com/file/p18564976/SimpleCATRule.dslr</a><br>
&gt; SimpleCATRule.dslr<br>
&gt;<br>
&gt; I really appreciate for any help or answer.<br>
&gt;<br>
&gt; Thanks in advance,<br>
&gt; Ricky<br>
&gt;<br>
<font color="#888888"><br>
--<br>
View this message in context: <a href="http://www.nabble.com/Drools-Stateful-Session-Thread-Safe-tp18564976p18848760.html" target="_blank">http://www.nabble.com/Drools-Stateful-Session-Thread-Safe-tp18564976p18848760.html</a><br>

Sent from the drools - dev mailing list archive at Nabble.com.<br>
<br>
_______________________________________________<br>
rules-dev mailing list<br>
<a href="mailto:rules-dev@lists.jboss.org">rules-dev@lists.jboss.org</a><br>
<a href="https://lists.jboss.org/mailman/listinfo/rules-dev" target="_blank">https://lists.jboss.org/mailman/listinfo/rules-dev</a><br>
</font></blockquote></div><br><br clear="all"><br>-- <br> Edson Tirelli<br> JBoss Drools Core Development<br> JBoss, a division of Red Hat @ <a href="http://www.jboss.com">www.jboss.com</a><br>
</div>