Uploaded image for project: 'jBPM'
  1. jBPM
  2. JBPM-2133

Error handling in CommandServiceBean.execute

XMLWordPrintable

    • Icon: Patch Patch
    • Resolution: Won't Do
    • Icon: Minor Minor
    • jBPM 3.2.x
    • jBPM 3.2.6.SP1
    • None
    • None

      regards CommndServiceBean.execute(Command command):

        public Object execute(Command command)
        {
          JbpmContext jbpmContext = jbpmConfiguration.createJbpmContext();
          try
          {
            log.debug("executing " + command);
            return command.execute(jbpmContext);
          }
          catch (RuntimeException e)
          {
            sessionContext.setRollbackOnly();
            throw e;
          }
          catch (Exception e)
          {
            sessionContext.setRollbackOnly();
            throw new JbpmException("failed to execute " + command, e);
          }
          finally
          {
            jbpmContext.close();
          }
        }
      

      If an exception occurres in Command.excute() it's important that this exception is propagated to the client to allow for proper exception handling to differentiate e.g. between a concurrency issue ("job is locked by...") and a database error.

      These exceptions are not propagated to the client if the jbpmContext.close() in the finally block throws an exceptions. This might very well happen since it performs non-trivial operations, e.g. access the database. (Actually it it seems to me as if the failure of jbpmContext.close() always happen, maybe since after the setRollbackOnly() no new connections are possible.)

      The following change will fix the problem:

        public Object execute(Command command)
        {
          JbpmContext jbpmContext = jbpmConfiguration.createJbpmContext();
          boolean exceptionThrown = false;
          try
          {
            log.debug("executing " + command);
            return command.execute(jbpmContext);
          }
          catch (RuntimeException e)
          {
            sessionContext.setRollbackOnly();
            exceptionThrown = true;
            throw e;
          }
          catch (Exception e)
          {
            sessionContext.setRollbackOnly();
            exceptionThrown = true;
            throw new JbpmException("failed to execute " + command, e);
          }
          finally
          {
              try
              {
                jbpmContext.close();
              }
              catch (RuntimeException e2) {
                if (exceptionThrown)
                {
                  // JIRA XX: do not hide thrown exception
                  log.error("exception on context close", e2);
                }
                else
                {
                  throw e2;
                }
              }
          }
        }
      

            marco.rietveld Marco Rietveld (Inactive)
            boercher Volker Börchers (Inactive)
            Votes:
            0 Vote for this issue
            Watchers:
            0 Start watching this issue

              Created:
              Updated:
              Resolved: