Spring Transactional and Delegates

Hello!

My project is camunda, spring boot and JPA.

I have 2 java delegates via delegates expression.
Both delegates in one camunda transact borders.

First:

@Log4j2
@Transactional
@Component
public class First implements JavaDelegate {
    final SomeRepository someRepository;

    public First(SomeRepository someRepository) {
        this.someRepository = someRepository;
    }

    @Override
    public void execute(DelegateExecution execution) throws Exception {
        try {
            //successful do something with someRepository
        } catch (Exception e) {
            TransactionAspectSupport.currentTransactionStatus().setRollbackOnly();
            throw new BpmnError(this.getClass().getSimpleName(), e.getMessage());
        } 
    }
}

Second:

@Log4j2
@Transactional
@Component
public class Second implements JavaDelegate {
    final SomeRepository someRepository;

    public Second(SomeRepository someRepository) {
        this.someRepository = someRepository;
    }

    @Override
    public void execute(DelegateExecution execution) throws Exception {
        try {
            //do something with someRepository and catch Exception
        } catch (Exception e) {
            TransactionAspectSupport.currentTransactionStatus().setRollbackOnly();
            throw new BpmnError(this.getClass().getSimpleName(), e.getMessage());
        } 
    }
}

If I catch rollback in second delegate, how can I rollback first delegate?
Is it possible in this case?

Hi @Vasily
Please do not mix up the two different concepts of transactions there are.
Transactions in the process model are business transactions and have nothing to do with the technical transactions you’re rolling back. Actually rolling back a technical transaction and then throwing a BPMN error will probably fail, because the Camunda engine cannot save the information about the BPMN error in the database.
Regarding technical transactions, Camunda will perform all steps in one transaction, as long as there are no wait states (user task, timer, etc). You can also configure your own transaction boundaries by using async continuation.
On the other hand there are business transactions you can model in your process model. To “rollback” those you need to define compensation activities for all activities for which the results should be compensated.
See the following example for details: