AnsweredAssumed Answered

Rollback operations on task-complete failure

Question asked by mf on Aug 17, 2015
Latest reply on Aug 18, 2015 by martin.grofcik
I have a completeTask method in TaskManager spring bean, which first it sets some variables, then completes the task. When taskService.completeTask throws an Exception, how can i revert all operations before it?

My current solution is catching exception, removing variables, throw it again, but is there any better solution?


@Named
public class TaskManager {

   @Inject
   private TaskService taskService;

   @Inject
   private RuntimeService runtimeService;

   public void completeTask(CompleteTaskParam param) {
      // set some local variables on execution and task, for example:
      runtimeService.setVariableLocal(param.getExecutionId(), "var1", "1234");
      taskService.setVariableLocal(param.getTaskId(), "var2", "3456");
      try {
         // completing task
         taskService.complete(param.getTaskId());
      } catch(Exception e) {
         // revert variables
         runtimeService.removeVariableLocal(param.getExecutionId(), "var1");
         taskService.removeVariableLocal(param.getTaskId(), "var2");
         throw e;
      }
   }
}

Outcomes