1 Reply Latest reply on Jun 13, 2011 12:13 PM by byungwoojun

    Does the ksession.dispose() work safely?

    byungwoojun Newbie

      I am creating a ksession per startProcess, and after use of the ksession, I dispose it (using the processEventListener afterProcessCompleted event). If I test my process one by one, there was no problem with the ksession.dispose(). However, I noticed that if I started multiple processes at a similar time, the ksession.dispose() causes the NullPointerException. When multiple processes are invoked, there are multiple active independent ksession objects, in my case. Then, each process starts calling the ksesion.dispose() when it finishes its job. The first ksession.dispose() function code sets NULL to the applicationScopeEntityManger object after use. As the name indicates, the applicationScopeEntityManager seems to be application-level. Once it is set to NULL, the subsequent ksession.dispose() has a problem using the applicationScopeEntityManager (which is already set to NULL).

       

      In enterprise application, it is normal there always many processes are invoked concurrently.

       

      So, I started wondering how the ksession.dispose() works???

       

      Any suggestion will be appreciated.

       

      bwj

        • 1. Re: Does the ksession.dispose() work safely?
          byungwoojun Newbie

          Since we don't change environment per knowledge session, we cache the environment and use it when we create a new stateful knowledge session. It is questionable if we need to create environment per knowledge session (e.g., Environment env = KnowledgeBaseFactory.newEnvironment();)

           

          We chose to cache the environment, and we experienced the problems that was described in my previous section. As a resolution, my team set an additional environment property by using putting the emf.createEntityManager() into EnvironmentName.APP_SCOPED_ENTITY_MANAGER as follows:

           

          private static Environment env = null;

          ...

           

          env = KnowledgeBaseFactory.newEnvironment();

          ...

          env.set(EnvironmentName.ENTITY_MANAGER_FACTORY, emf);

          ...

          env.set(EnvironmentName.APP_SCOPED_ENTITY_MANAGER,

                         emf.createEntityManager());

          ...

           

          StatefulKnowledgeSession ksession = JPAKnowledgeService.newStatefulKnowledgeSession(kbase, null, env);

          ...

           

          After the above setting, the ksession.dispose() no longer created a problem. Here is my question, what is the best practice on the enviroment creation? Should we cache it or should we create it per knowledge session?

           

          thanks,

          bwj