2 Replies Latest reply on Aug 13, 2013 3:24 AM by Thomas Setiabudi

    DroolsSpringJpaManager breaks when Database Server is restarted, how to handle it?

    Thomas Setiabudi Expert



      I use JBPM5.4.Final, and Drools-Spring 5.4.Final in a Web Application.

      I create Stateful Knowledge Session and LocalTaskService in the Spring config file, they are created once for the rest of the web application life.


      Everything works fine, until the Database Server, MS SQL Server 2008, is restarted.

      Which then break the Stateful Knowledge Session and LocalTaskService.


      After looking into the code, I found that when we declare Stateful Knowledge Session like this:


      <drools-spring:ksession id="ksession1" type="stateful"
                                                        ref="transactionManager" />
                                                        ref="entityManagerFactory" />


      Eventhough we give it EntityManagerFactory, the code inside DroolsSpringJpaManager will try to get EntityManager just once

      this is how the code looks inside org.drools.container.spring.beans.persistence.DroolsSpringJpaManager:


      Look at the implementation of getApplicationScopedPersistenceContext starting from line 64

      The problem with this implementation is EntityManagerFactory used exactly just one time to get an EntityManager, even when the database connection is no longer valid, it will still use the same EntityManager which will of course throw exception.


      My question is, is it a bug? or I use these Stateful Knowledge Session Wrongly?



      Currently, to keep my application running, I modified DroolsSpringJpaManager at getApplicationScopedPersistenceContext():
      If the appScopedEntityManager is not null, then execute simple select query, when that query throws exception, that means its time to get new EntityManager from the EntityManagerFactory.

      It may be an ugly fix, but thats why I need to clarify this issue.

      here is the code:


      public PersistenceContext getApplicationScopedPersistenceContext() {
                          boolean refreshEntityManager = false;
                          if (this.appScopedEntityManager != null) {
                                    try {
                                              Query query = this.appScopedEntityManager
                                                                  .createQuery("SELECT 1 FROM ProcessInstanceInfo WHERE id = 0 ");
      //                                        System.out.println("Query Result: " + String.valueOf(a));
                                    } catch (Throwable e) {
      //                                        System.out.println(e.getMessage());
                                              refreshEntityManager = true;
                          if (refreshEntityManager == true) {
      //                              System.out.println("Refreshing EM....");
                                    EntityManagerHolder emHolder = (EntityManagerHolder) TransactionSynchronizationManager
                                    this.appScopedEntityManager = this.emf.createEntityManager();
                                    emHolder = new EntityManagerHolder(this.appScopedEntityManager);
                                    internalAppScopedEntityManager = true;
      //                              System.out.println("Done Refreshing EM....");
                          } else {
                                    if (this.appScopedEntityManager == null) {
                                              // Use the App scoped EntityManager if the user has provided it,
                                              // and
                                              // it is open.
                                              this.appScopedEntityManager = (EntityManager) this.env
                                              if (this.appScopedEntityManager != null
                                                                  && !this.appScopedEntityManager.isOpen()) {
                                                        throw new RuntimeException(
                                                                            "Provided APP_SCOPED_ENTITY_MANAGER is not open");
                                              if (this.appScopedEntityManager == null) {
                                                        EntityManagerHolder emHolder = (EntityManagerHolder) TransactionSynchronizationManager
                                                        if (emHolder == null) {
                                                                  this.appScopedEntityManager = this.emf
                                                                  emHolder = new EntityManagerHolder(
                                                                                      this.emf, emHolder);
                                                                  internalAppScopedEntityManager = true;
                                                        } else {
                                                                  this.appScopedEntityManager = emHolder
                                    if (TransactionSynchronizationManager.isActualTransactionActive()
                                                        && isJTA) {
                          return new JpaPersistenceContext(this.appScopedEntityManager, isJTA);


      Is it the correct way to do this?

      Will this issue fixed in JBPM 6 ?


      Any help is appreciated.



      Thomas Setiabudi




      Attached server log that shows:
      1. the error after database is restarted and I try to call start process on the ksession.

      2. the error after the first error happen and I try to call start process on the ksession again.