3 Replies Latest reply on Dec 4, 2006 1:46 PM by cocampo

    invalid reference, performance, db access, etc...

    jstachera

      I am working on web application and I found some problems with JbpmContext. There are some issues that I am not fully aware:

      1. If I do not close the context no changes will be saved to the database.
      2. If for example I will get some reference to jbpm object:

      TaskInstance taskInstance = context.getTaskInstance(wid);
      


      and for some reason I close the context then from that time on the reference to taskInstance is invalid end every attempt to call some method leads to exception -> "context was closed" - or smth like that.

      Therefore, when I create my TaskInstance Bean (actually JbpmWorkItem) in the constructor I initialize all the fields that are used by getters methods of the bean.

      public class JbpmWorkItem implements IWorkItem {
       static Log log = new Log();
       /**
       * session
       */
       JbpmDefSession session;
       /**
       * work item id
       */
       long workItemId;
       /**
       * process instance id
       */
       long processInstanceId;
       /**
       * process definition id
       */
       long processDefinitionId;
       /**
       * CurrentProcessObjectName
       */
       String currentProcessObjectName;
      
      
       /*
       * Task instance - actuall jbpm work item (task to do)
       */
      
       /** Cons
       * @param session - def session
       * @param wid - work item id
       * @throws JbpmAtflowException
       */
       public JbpmWorkItem(DefSession session, long wid) throws Exception {
       this.session = (JbpmDefSession)session;
       this.workItemId = wid;
       /* getContext() creates new context for logged user*/
       JbpmContext context = this.session.getContext();
       try {
       TaskInstance taskInstance = context.getTaskInstance(wid);
       processInstanceId = taskInstance.getToken().getProcessInstance().getId();
       processDefinitionId = taskInstance.getToken().getProcessInstance().getProcessDefinition().getId();
       currentProcessObjectName = taskInstance.getToken().getNode().getName();
       }
       catch (Exception e) {
       log.error("Could not create work item: " + wid + ", actor: " + session.getLoggedOnUser().getName());
       throw new JbpmAtflowException("Could not create work item: " + wid + ", actor: " + session.getLoggedOnUser().getName(), e);
       }
       finally {
       context.close();
       }
       }
      


      and here is the question is it correct way ?
      My purpose of doing that was to minimize the number of accesses to the database. Since If I put the code with creating new context and closing it in every getter method just to access jbpm object it will probably decrease the performance.

      Am I right ? or Is Jbpm doing some caching so not every call to jbpm object method leads to database access to get the data ?

      BR,
      Jurek

        • 1. Re: invalid reference, performance, db access, etc...
          kukeltje

          1: correct (lots of references to this in the forum, docs etc)
          2: Correct, default behaviour of many systems where hibernate is used

          hibernate already takes care of optimizing tings. If the behaviour is not what you want, why not try optimizing the hibernate config instead of wrapping things?

          • 2. Re: invalid reference, performance, db access, etc...
            jstachera

             

            "kukeltje" wrote:
            If the behaviour is not what you want, why not try optimizing the hibernate config instead of wrapping things?


            I am not sure if I can leave the context open or it's better to create new one every time when I want to get some data out of jBPM ?




            • 3. Re: invalid reference, performance, db access, etc...

               

              "jstachera" wrote:


              I am not sure if I can leave the context open or it's better to create new one every time when I want to get some data out of jBPM ?



              Well, it's a matter of choice; do you want to have the objects to be open all the time (including your hbm definition files) or you prefer the burden of your files being loaded every time you need to use jBPM? IMHO, it depends on how often are you gonna access jBPM's data.

              Just my two cents.