cancel
Showing results for 
Search instead for 
Did you mean: 

Behavior of process when has throw exceptions

nandoztx
Champ in-the-making
Champ in-the-making
Is it correct to drop all process data (historic, runtime data), on exception occurrency, it's agreed with the BPM model?
If a process not have a userTask or boundaryEvent, the last commit have not exists, so when rolled back, the process would never have existed.

   Example:

startEvent->serviceTask1->serviceTask2->endEvent

   If has an exception in serviceTask2 is right wich all data must be rollback?

serviceTask1 has a webservice communication or RPC call.
A exception will throw in serviceTask2, so all data in serviceTask2 and serviceTask1will be rollback.
All business rule in serviceTask1 will not rolled back, cause its not a database transaction, but a external service.
When this process will rollback, any exception will be catched cause all process data has rolled back to last commit in userTask or boundaryEvent or not have a last commit if it's a new process (in this sample, no process historic or register will be persisted);

Thank's in advance.
7 REPLIES 7

frederikherema1
Star Contributor
Star Contributor
What you are describing is correct. When an exception occurs when executing a process, it's rolled back to the initial state it was in, prior to the API-call (or timer firing) that caused the process to "flow". In case of starting a process-instance, there is indeed NO process created when an exception occurs between process-start and the first wait-state or end-event.

So when you're using external non-transactional resources, you'll have to roll your own solution to overcome this. This is a generic (not activiti-specific) problem, communication with external services…

nandoztx
Champ in-the-making
Champ in-the-making
Thank's frederikheremans!

I had this doubt cause this behavior is a little bit weird, but if it is an official bahavior… no problem Smiley Wink

Actually, in my workflows, I use a scriptTask to call serviceTasks or logical procedures to execute something. Protecting the code with try{}catch blocks I can handle the sequence flow, in exceptions or not, according with my business rule. It's a good way to avoid java code modifications and decouple the business rule from the server code. Example:


<scriptTask id="changeCurrentUserPassword" name="Alterar atual senha de usuário" scriptFormat="groovy">
      <script> <![CDATA[
       def userToChangePassword = userService.getUserByLogin(userNameChangePassword);
     userToChangePassword.setPassword(newPassword);
     userEmailChangePassword = userToChangePassword.getPersonEmail();
  try{
   userService.updateUser(userToChangePassword);
   successOnSendRequest = true;
  }catch(Exception e){
   successOnSendRequest = false;
   exceptionOnChangePassword = e.getMessage();
  }
        ]]> </script>
    </scriptTask>
    <sequenceFlow sourceRef="changeCurrentUserPassword" targetRef="successOnRequestOrNot"/>
and in my sequenceFlow:

<exclusiveGateway id="successOnRequestOrNot" name="Verificar sucesso ao solicitar nova senha" />
<sequenceFlow sourceRef="successOnRequestOrNot" targetRef="successGateway">
    <conditionExpression xsi:type="tFormalExpression">${successOnSendRequest == true}</conditionExpression>
</sequenceFlow>
<sequenceFlow sourceRef="successOnRequestOrNot" targetRef="errorGateway">
    <conditionExpression xsi:type="tFormalExpression">${successOnSendRequest == false}</conditionExpression>
</sequenceFlow>

It's a good way to treat exceptions and model the business sequence flow.
It's a pretty good solution?

jbarrez
Star Contributor
Star Contributor
Yes, it is. Also, check the user guide, more specifically the service task. It has a section on exception strategy.

Eg, this is another approach:


Throwing BPMN Errors
As of Activiti 5.9, it is possible to throw BPMN Errors from user code inside Service Tasks or Script Tasks. In order to do this, a special ActivitiException called BpmnError can be thrown in JavaDelegates or scripts and since Activiti 5.10 also in expressions and delegate expressions. The engine will catch this exception and forward it to an appropriate error handler, e.g., a Boundary Error Event or an Error Event Sub-Process.

public class ThrowBpmnErrorDelegate implements JavaDelegate {

  public void execute(DelegateExecution execution) throws Exception {
    try {
      executeBusinessLogic();
    } catch (BusinessExeption e) {
      throw new BpmnError("BusinessExeptionOccured");
    }
  }

}
The constructor argument is an error code, which will be used to determine the error handler that is responsible for the error. See Boundary Error Event for information on how to catch a BPMN Error.

This mechanism should be used only for business faults that shall be handled by a Boundary Error Event or Error Event Sub-Process modeled in the process definition. Technical errors should be represented by other exception types and are usually not handled inside a process.


yogesh1454
Champ in-the-making
Champ in-the-making
Can any one tell me like how to get the error message from inside the boundary Error Event or Error Event Sub-Process modeled

jbarrez
Star Contributor
Star Contributor
You mean the java exception message? If so, then that's not possible : a typed java exception is mapped to an error. Maybe the BpmnError can accept a custom message, but haven't tried it.

ciarancorson1
Champ in-the-making
Champ in-the-making
Is it possible to extract a ActivitiObjectNotFound exception programmatically from within a subprocess, without the use of an error boundary event?

jbarrez
Star Contributor
Star Contributor
No. When would that exception happen?
I can only think of that happening in a custom service task? if so, why not try-catch it?