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

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

    John Lee Newbie

      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                                    <=====