AnsweredAssumed Answered

Activiti Error Handling

Question asked by fritz128 on Sep 25, 2013
Latest reply on Oct 4, 2013 by fritz128
What I have:
Non-Activiti processes (saving to DB) in Activiti Service Task. I want to rollback Activiti context on error

What a problem:
Activiti context doesn't rollback.

My code:]
In this service task I create new client entity and persist it. Client entity has Unique constraint, so the try{} section may fail. When it happens, I see in TaskTwo that execution.getVariable("client") != null


@Service
public class AddClientService implements ActivityBehavior {

    @Override
    public void execute(ActivityExecution execution) throws Exception {
        PvmTransition transition = null;

        Client client = new Client();
        client.setFirstName("John");
        client.setLastName("Smith"); // Have unique constraint

        execution.setVariable("client", client); // need to be rollbacked in case of error

        Engine engine = Engine.getInstance();
        SessionFactory sessionFactory = engine.getHibernateSessionFactory();
        Session session = sessionFactory.openSession();
        session.beginTransaction();
        try {
            session.save(client);
            session.getTransaction().commit();
            transition = execution.getActivity().findOutgoingTransition("toTaskOneFlow");
        } catch (Exception ex) {
            transition = execution.getActivity().findOutgoingTransition("toTaskTwoFlow");
        } finally {
            session.close();
        }
        execution.take(transition);
    }
}


Questions:
1. Why execution.getVariable("client") != null in the next task after error happens?
2. Do I need manually remove all variables from Activiti context after error?
3. May be it is better to use compensation end event in my case?

Outcomes