2 Replies Latest reply on Jan 3, 2011 2:37 PM by jxli_us

    How to access hibernate session from within drools rule file in JbossESB?

    jxli_us

      Hi There,

       

      I like the beauty of JBossESB, Drools and has been playing with them for awhile. According to the document, I can use setGlobal() in the rules file to setup a hibernate session and use SQL to access data in a relation table. But was not able to get it to work.

       

      The problem seems like the Hibernate session was created in one rule, but can not be accessed from other rules. Here is the test case I played; I'd appreciated your input on this.  -John

       

      - rules:

      ====================

      global com.qfraud.HibernateSessionWrap hibernateSession;

       

      rule "set globals"

      salience 100

      //dialect "mvel"

      when

      eval ( hibernateSession ==null)

      then

      System.out.println("set globals ... ");

      //totally not working// drools.setGlobal("hibernateSession", new HibernateSessionWrap()); 

      //totally not working// kcontext.getKnowledgeRuntime().setGlobal("hibernateSession", new HibernateSessionWrap());    hibernateSession is still null in the rules below

      //drools.getKnowledgeRuntime().setGlobal("hibernateSession", new HibernateSessionWrap());

      //drools.getWorkingMemory().setGlobal("hibernateSession", new HibernateSessionWrap());

      hibernateSession = new HibernateSessionWrap();

      System.out.println(": " + hibernateSession);

      System.out.println(" ... set globals ");

      end

       

      rule "any hibernatesession"

      when

      eval ( hibernateSession == null)

      then

      System.out.println("no hibernateSession" );

      end

      ==========================

       

      - HibernateSessionWrap class is simply configure/initialize hibernate:

      ========================================================

      HibernateSessionWrap {

         <  Hibernate classes >

         HibernateSessionWrap(){

            ....

            System.out.println("HibernateSessionWrap created")

         }

      }

      ======================================================

       

      - and here is wht output on the jboss5.1 console:

      =======================================

      .....

       

      18:24:56,275 INFO  [STDOUT] set globals ...                                <=====

      18:24:56,275 INFO  [STDOUT] setup the global hibernateSession used in drl for working on facts in RDBMS.

      18:24:56,276 INFO  [Configuration] configuring from resource: /hibernate.cfg.xml

      18:24:56,276 INFO  [Configuration] Configuration resource: /hibernate.cfg.xml

      18:24:56,281 INFO  [Configuration] Reading mappings from resource : /com/qfraud/fraud_mappings.hbm.xml

      18:24:56,289 INFO  [HbmBinder] Mapping class: com.qfraud.Account -> ACCOUNT

      ...

      18:24:56,312 INFO  [SessionFactoryObjectFactory] Not binding factory to JNDI, no JNDI name configured

      18:24:56,312 INFO  [NamingHelper] JNDI InitialContext properties:{}

      18:24:56,314 INFO  [STDOUT] HibernateSessionWrap created               <=====

      18:24:56,314 INFO  [STDOUT] : com.qfraud.HibernateSessionWrap@61677b       <=====

      18:24:56,314 INFO  [STDOUT]  ... set globals                                                 <=====

      18:24:56,316 INFO  [STDOUT] in notification1: 1234-65-AFD

      18:24:56,317 INFO  [STDOUT] LostCardEvent: 1234-65-AFD

      18:24:56,317 INFO  [STDOUT] notification3: null

      18:24:56,317 INFO  [STDOUT] no hibernateSession                                    <=====

        • 1. Re: How to access hibernate session from within drools rule file in JbossESB?
          jeffdelong

          I believe that the reason you are seeing hibernateSession is null is because the LHS of the second rule has already been evaluated. If the HibernateSession was being used in the RHS of the rule it would be not null. However, usually a HibernateSession would not be used in a constraint like you are using it. Did you try using it in the from clause of a rule whose constraint would be re-evaluated after the execution of the higher-salience rule?

          • 2. Re: How to access hibernate session from within drools rule file in JbossESB?
            jxli_us

            Hi Jeff,

             

            Thank you for the insightful response. I just got back from vacation and will give it a try.

             

            I'm from DBA side. Having played with Drools/ESB for a while, I wondered how does Drools get facts from a database. This should be useful if a rule depends on large amount of facts. But I could be thinking on the wrong track.