2 Replies Latest reply on Feb 3, 2010 11:56 AM by kaiweing

    Accessing jBPM Hibernate Session in Activity

    kaiweing

      Hello,

       

      I'm looking for a best practice for the following use case:

       

      (I use jBPM 4.3 and I'm running in a web container, so no JTA, etc.)

       

      At one stage in my process, I want to grab a variable (transfer object, serializable) from the process context, convert it to an entity and write it to the database. I want to do this in the same transaction that is used by jBPM, so my writing to the db and advancing the process are atomar.

       

      Since no JTA is available my activity/dao needs access to the hibernate session used by jBPM. I currently handle this by passing the SessionFactory I get from the environment to the process as a process variable. The activity/dao can then access this session factory to retrieve the current session.

      (I added my entity mapping to the jbpm.hibernate.cfg.xml)

       

              SessionFactory sessionFactory = processEngine.get(SessionFactory.class);
              sessionFactory.getCurrentSession().beginTransaction();
              
              Map<String, Object> vars = new HashMap<String, Object>();
              AuftragTO auftrag = new AuftragTO();
              auftrag.setName("myname");
              
              vars.put("auftrag", auftrag );
              vars.put("sessionFactory", sessionFactory);
              
              try {
                  ProcessInstance processInstance = executionService.startProcessInstanceByKey("simple", vars );
                  sessionFactory.getCurrentSession().getTransaction().commit();
              } catch (RuntimeException e) {
                  sessionFactory.getCurrentSession().getTransaction().rollback();
              }
      
      

       

      This stops working as soon as anything is executed asynchronously (the current session no longer has an open tx, as it is closed when the sync part finishes, the async activity seems to use another session factory).

       

      Is there a better way to access the jBPM session factory from an activity? EventHandler or ActivityBehaviour don't seem to offer that.

       

      Is there another way to participate in the jBPM transaction?

       

      Thanks for your help!

       

      Kai