5 Replies Latest reply on Nov 3, 2012 6:02 PM by jasonjho

    Multiple sessions and thread safety using jBPM persistence

    jasonjho

      For every workflow process I am creating a new separate kbase/ksession.   So every flow is bound to a unique session and not shared.

       

      I have enabled JPA persistence and pass in the following env to each new session:

       

      {code}

      env = KnowledgeBaseFactory.newEnvironment();

      env.set(EnvironmentName.ENTITY_MANAGER_FACTORY, emf);

      env.set(EnvironmentName.TRANSACTION_MANAGER, TransactionManagerServices.getTransactionManager());

      session = createNewSession(env)

      session.startProcess(...)

      {code}

       

       

      I am getting Hibernate StaleObjectExceptions and OptimisticLocking errors when I perform multiple operations on the same process definition, but run on different session IDs.

      I am not sure what specific DB operation is causing this, but since each process is in its own KSession - how is this even possible?

       

      What database update could be in contention between 2 threads in this case?

       

      I am using jBPM5.2.

       

      Any ideas???

        • 1. Re: Multiple sessions and thread safety using jBPM persistence
          swiderski.maciej

          Recently an issue was resolved that looks like you could ran into - when same process instance is manipulated by two sessions optimistic lock exception (StaleObject...) can be seen as persistence context was not cleared on transaction end meaning cached object was used on next command execution. It was fixed in 5.4 so soon should be available as Final, meanwhile you could give a try with CR1.

           

          HTH

          • 2. Re: Multiple sessions and thread safety using jBPM persistence
            jasonjho

            Hi, thanks for the reply/info.

             

            1) Do you know if the 5.4.0CR1 build is compatible with Drools 5.3.1.Final?

            2) Are other JPA providers such as OpenJPA supported with the 5.4.0.CR1 build?  I know from previous JIRA reports that Hibernate was the only one that was supported at the time of 5.2.0.

             

            Thanks!

            • 3. Re: Multiple sessions and thread safety using jBPM persistence
              swiderski.maciej

              Jason J wrote:

               

              Hi, thanks for the reply/info.

               

              1) Do you know if the 5.4.0CR1 build is compatible with Drools 5.3.1.Final?

              as far as I can tell it is not

               

               

              Jason J wrote:

               

              2) Are other JPA providers such as OpenJPA supported with the 5.4.0.CR1 build?  I know from previous JIRA reports that Hibernate was the only one that was supported at the time of 5.2.0.

              not yet, work is in progress, there are jira issue for it so please keep an eye on them and vote to speed up the work

               

              HTH

              • 4. Re: Multiple sessions and thread safety using jBPM persistence
                jasonjho

                Hi Maciej,

                 

                Could you point me to the bug report regarding the 2 sessions and StaleObjectException?  At this point, I'm wondering if it's simple enough to patch this fix into my current environment instead of upgrading to a new version (i am running into other new issues that i'm not seeing in my current build).

                 

                Thanks!

                • 5. Re: Multiple sessions and thread safety using jBPM persistence
                  jasonjho

                  Finally tracked it down.  For anyone that is in a similar situation, the fix is:

                   

                  In SingleSessionCommandService.SynchronizationImpl.afterCompletion(...), add the following line and update the interfaces accordingly

                   

                     this.service.jpm.clearPersistenceContext();  // clears the context after tx complete

                     this.service.jpm.endCommandScopedEntityManager();