1 Reply Latest reply on Sep 20, 2013 1:35 AM by Maciej Swiderski

    How to join a JBPM5 transaction in a standalone web design?

    mvermand Newbie



      I'm using the JBPM 5.2 jars inside a webapp that runs on a JBoss AS 7.

      jbpmEmf = Persistence.createEntityManagerFactory("org.jbpm.persistence.jpa"));


      JBPM works fine.

      Now I want to create custom JPA entities and manipulate these together with the JBPM entities.

      Example: when a requests comes to start an instance of a Process, I want to store the external Id that is provided in the request because in that way I can reload the specific kbase and ksession when later on a sequential request is made with the same external Id as argument.


      My problem is that I do not know how to join the transaction that will be used by JBPM to create the process instance entity and workitem entities...


      I extended the persistence.xml:


      <persistence-unit name="org.jbpm.persistence.jpa"
              <!-- START OF WMS ENTITIES -->
              <!-- END OF WMS ENTITIES -->
                  <property name="hibernate.max_fetch_depth" value="3" />
                  <property name="hibernate.hbm2ddl.auto" value="none" />
                  <property name="hibernate.show_sql" value="false" />
                  <property name="hibernate.transaction.manager_lookup_class"
                      value="org.hibernate.transaction.JBossTransactionManagerLookup" />
                  <property name="hibernate.dialect" value="org.hibernate.dialect.Oracle10gDialect" />


      I have created a EntityManagerFactory:

      jbpmEmf = Persistence.createEntityManagerFactory("org.jbpm.persistence.jpa");


      Now, before I start a new process instance, I want to store the external Id in the same JTA transaction as will be used by JBPM.

      I have a REST resource in my app that is called to start a process instance:


      (simplified code:)

      public ScenarioInstance startProcessInstance(@PathParam("name") String name, @PathParam("version") String version,
              Map<String, Object> params) {
          String externalId = params.get("externalid").toString();
          KnowledgeBase kbase = buildKnowledgeBase(name, version); // support for different versions.
          // jbpmEmf is a EntityMananagerFactory created from "org.jbpm.persistence.jpa"
          StatefulKnowledgeSession ksession = JPAKnowledgeService.newStatefulKnowledgeSession(kbase, null, createEnvironment(jbpmEmf));
          int ksessionId = ksession.getId();
          WMSSession wsession = new WMSSession(externalId, ksessionId);
          //=> how to get the EntityManager used by JBPM to persist wsession?
          EntityManager em = ??? <===
          ProcessInstance processInstance = ksession.startProcess(processId, params);
      * Create a new environment
      * @param emf the EntityManagerFactory
      * @return new Environment
      * @throws NamingException
      protected static Environment createEnvironment(EntityManagerFactory emf) throws NamingException {
          Environment env = KnowledgeBaseFactory.newEnvironment();
          env.set(EnvironmentName.ENTITY_MANAGER_FACTORY, emf);
          env.set(EnvironmentName.TRANSACTION_MANAGER, getTransactionManager());
          return env;


      So my question is: (line 17): how to get the EntityManager uwed by JBPM to persist the wsession object?