12 Replies Latest reply on Oct 30, 2013 8:51 AM by girish.bapat

    jbpm hibernate persistence

    joshjdevl

      Hi,

       

      We are using Hibernate as our persistence implementatio and Bitronix. I would like to configure JBPM peristence to use a hibernate entity manager so it can participate in our application transactions and go to our existing databases.

       

      I've done something like this where I inject our jtaTransactionManager.

       

      {code}

      // create the entity manager factory and register it in the environment

              final EntityManagerFactory emf = new EntityManagerFactoryImpl(

                      getSessionFactory(), PersistenceUnitTransactionType.JTA, true, null);

       

       

              final Environment env = KnowledgeBaseFactory.newEnvironment();

              env.set(EnvironmentName.ENTITY_MANAGER_FACTORY, emf);

              // env.set(EnvironmentName.PERSISTENCE_CONTEXT_MANAGER, emf);

              env.set(EnvironmentName.TRANSACTION_MANAGER, jtaTransactionManager);

              // create a new knowledge session that uses JPA to store the runtime

              // state

       

       

              final StatefulKnowledgeSession ksession =

                      JPAKnowledgeService.newStatefulKnowledgeSession(getKnowledgeBase(), null, env);

      {code}

       

       

      However, I receive the following error. Any clues/hints?

       

       

       

      {code}

      Caused by: java.lang.reflect.InvocationTargetException

                at sun.reflect.NativeConstructorAccessorImpl.newInstance0(Native Method)

                at sun.reflect.NativeConstructorAccessorImpl.newInstance(NativeConstructorAccessorImpl.java:57)

                at sun.reflect.DelegatingConstructorAccessorImpl.newInstance(DelegatingConstructorAccessorImpl.java:45)

                at java.lang.reflect.Constructor.newInstance(Constructor.java:532)

                at org.drools.persistence.jpa.KnowledgeStoreServiceImpl.buildCommanService(KnowledgeStoreServiceImpl.java:116)

                ... 34 more

      Caused by: java.lang.RuntimeException: Could not commit session

                at org.drools.persistence.SingleSessionCommandService.<init>(SingleSessionCommandService.java:136)

                ... 39 more

      Caused by: java.lang.NullPointerException

                at org.drools.persistence.SingleSessionCommandService.<init>(SingleSessionCommandService.java:125)

                ... 39 more

      {code}

        • 1. jbpm hibernate persistence
          urdo2
          • 2. Re: jbpm hibernate persistence
            afisboy

            Best I can tell, the above link doesn't point to a solution?!?

             

            I see the same problem when attempting to use JPAKnowledgeService.newStatefulKnowledgeSession()

             

            11:35:06,175 ERROR [STDERR] java.lang.IllegalStateException: java.lang.reflect.InvocationTargetException

            11:35:06,175 ERROR [STDERR]     at org.drools.persistence.jpa.KnowledgeStoreServiceImpl.buildCommanService(KnowledgeStoreServiceImpl.java:130)

            11:35:06,175 ERROR [STDERR]     at org.drools.persistence.jpa.KnowledgeStoreServiceImpl.newStatefulKnowledgeSession(KnowledgeStoreServiceImpl.java:54)

            11:35:06,175 ERROR [STDERR]     at org.drools.persistence.jpa.JPAKnowledgeService.newStatefulKnowledgeSession(JPAKnowledgeService.java:109)

             

            Source code in drools-persistence-jpa/src/main/java/org/drools/persistence/jpa/KnowledgeStoreServiceImpl.java

             

                private CommandExecutor buildCommanService(KnowledgeBase kbase,

                                                          KnowledgeSessionConfiguration conf,

                                                          Environment env) {

             

                    Class< ? extends CommandExecutor> serviceClass = getCommandServiceClass();

                    try {

                        Constructor< ? extends CommandExecutor> constructor = serviceClass.getConstructor( KnowledgeBase.class,

                                                                                                          KnowledgeSessionConfiguration.class,

                                                                                                          Environment.class );

                        return constructor.newInstance( kbase,

                                                        conf,

                                                        env );

                    } catch ( SecurityException e ) {

                        throw new IllegalStateException( e );

                    } catch ( NoSuchMethodException e ) {

                        throw new IllegalStateException( e );

                    } catch ( IllegalArgumentException e ) {

                        throw new IllegalStateException( e );

                    } catch ( InstantiationException e ) {

                        throw new IllegalStateException( e );

                    } catch ( IllegalAccessException e ) {

                        throw new IllegalStateException( e );

                    } catch ( InvocationTargetException e ) {

                        throw new IllegalStateException( e );

                    }

                }

             

            I assume JPAKnowledgeService.newStatefulKnowledgeSession() works for some people?  Anyone have some hints on what I'm doing wrong?

            • 3. jbpm hibernate persistence
              afisboy

              Here is what worked for me.  Messing around with the source code, I found the exception was generated by the lack of a UserTransaction being included in the "env" object passed to JPAKnowledgeService.newStatefulKnowledgeSession().  Once I did that, all appears to be working.

              • 4. Re: jbpm hibernate persistence
                joshjdevl

                Do you have an example to share? Also, we use Hibernate so I create an entity manager factory and inject the session factory.

                 

                I tried

                 

                 

                {code}

                        final EntityManagerFactory emf = new EntityManagerFactoryImpl(

                                getSessionFactory(), PersistenceUnitTransactionType.JTA, true, null);

                 

                 

                        final Environment env = KnowledgeBaseFactory.newEnvironment();

                        env.set(EnvironmentName.ENTITY_MANAGER_FACTORY, emf);

                        env.set(EnvironmentName.TRANSACTION_MANAGER, jtaTransactionManager);

                        env.set(EnvironmentName.TRANSACTION, jtaTransactionManager.getUserTransaction());

                 

                {code}

                 

                 

                 

                Though receive

                 

                 

                {code}

                Caused by: java.lang.reflect.InvocationTargetException

                          at sun.reflect.NativeConstructorAccessorImpl.newInstance0(Native Method)

                          at sun.reflect.NativeConstructorAccessorImpl.newInstance(NativeConstructorAccessorImpl.java:57)

                          at sun.reflect.DelegatingConstructorAccessorImpl.newInstance(DelegatingConstructorAccessorImpl.java:45)

                          at java.lang.reflect.Constructor.newInstance(Constructor.java:532)

                          at org.drools.persistence.jpa.KnowledgeStoreServiceImpl.buildCommanService(KnowledgeStoreServiceImpl.java:116)

                          ... 34 more

                Caused by: java.lang.RuntimeException: Could not commit session

                          at org.drools.persistence.SingleSessionCommandService.<init>(SingleSessionCommandService.java:136)

                          ... 39 more

                Caused by: java.lang.NullPointerException

                          at org.drools.persistence.SingleSessionCommandService.<init>(SingleSessionCommandService.java:125)

                          ... 39 more

                 

                 

                {code}

                • 5. Re: jbpm hibernate persistence
                  joshjdevl

                  Looking at the code, it seems its not implemented. Thus this.jpm will be null

                   

                   

                  {code}

                                     if (tm.getClass().getName().toLowerCase().contains("jpa")) {

                                          // configure spring for JPA and local transactions

                                          cls = Class.forName("org.drools.container.spring.beans.persistence.DroolsSpringJpaManager");

                                          con = cls.getConstructors()[0];

                                          this.jpm = (PersistenceContextManager) con.newInstance(new Object[] { this.env });

                                      } else {

                                          // configure spring for JPA and distributed transactions

                                      }

                   

                  {code}

                  • 6. Re: TaskServer, persistence file and session
                    bpmn2user

                    I have a simple persistence example (http://community.jboss.org/people/bpmn2user/blog/2011/03/25/jbpm5--persistence-hello-process-example) to that would not thow the exception 'Could not commit session' if the task server is run separately.

                     

                    TomG: I am wondering what is the 'UserTransaction' configuration that is required in the 'env' object?

                    • 7. jbpm hibernate persistence
                      afisboy

                      I did a JNDI lookup on the UserTransaction and TransactionManager (I'm working with JBoss).  I added it to the env property

                       

                      env.set(EnvironmentName.TRANSACTION_MANAGER, tm);

                      env.set(EnvironmentName.TRANSACTION, ut);

                       

                      That got things to kind of work the JPAKnowledgeSesion, but it hasn't been stable.  I haven't had much luck getting the JPA versions to work.  I've been trying to get service tasks to work which would get up triggering threads to call the completeWorkItem calls on long running tasks.  Sometimes it works, sometimes it doesn't.  Its been discouraging trying to work within the JBoss container.  I suspect I'm not doing it right.  Has anyone had any luck running within JBoss using JPA versions?

                      • 8. jbpm hibernate persistence
                        bpmn2user

                        I did notice transaction problem with the example (http://community.jboss.org/people/bpmn2user/blog/2011/03/25/jbpm5--persistence-hello-process-example) if I use both the persistence units (process and task) together.

                         

                        I have not tried the example with JBoss, but I presume it is something to do with the UserTransaction configuration as you had mentioned. As the JBPM5 console works on JBoss server (also JBPM console works fine with Tomcat/Oracle http://community.jboss.org/people/bpmn2user/blog/2011/02/26/orcale-integration-with-jbpm5), I think it has to do the UserTrasaction/JTA configuration.

                        • 9. Re: jbpm hibernate persistence
                          bpmn2user

                          I did notice transaction problem with the example (http://community.jboss.org/people/bpmn2user/blog/2011/03/25/jbpm5--persistence-hello-process-example) and Oracle database if I use both the persistence units (process and task) together.

                           

                          I have not tried the example with JBoss, but I presume it is something to do with the UserTransaction configuration as you had mentioned. As the JBPM5 console works on JBoss server (also JBPM console works fine with Tomcat/Oracle http://community.jboss.org/people/bpmn2user/blog/2011/02/26/orcale-integration-with-jbpm5), I think it has to do the UserTrasaction/JTA configuration.

                          • 10. jbpm hibernate persistence
                            afisboy

                            I found out what I was doing wrong.  I am able to get the transaction manager and user transactions to work fine in JBoss.  My screw up was with "Service Tasks".  I was getting process instance ids mixed up with work item ids.  I learned there are 2 ids involved when correctly identifying a workitem to complete when the completion is outside of the listener's callback (a long running task).  The session id, and the work item id.

                            • 11. Re: jbpm hibernate persistence
                              supritchaudhary

                              Check that you have correct version of jbpm-persistence-jpa* in your classpath.

                              If you have "jbpm-persistence-jpa-5.2.0.Final.jar" and other jbpm files of version 5.3.0.Final you are likely to have this problem.

                              • 12. Re: jbpm hibernate persistence
                                girish.bapat

                                This helped me thanks for pointing out