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

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

    jerzy stachera Newbie

      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