I can't commit to database from java delegates

I have a Camunda process engine running inside a spring boot application with a secondary data source. this is the configurations:

@Bean @Primary
    public DataSource dataSource(){
        SimpleDriverDataSource dataSource = new SimpleDriverDataSource();
        dataSource.setDriverClass(org.postgresql.Driver.class);
        dataSource.setUrl("jdbc:postgresql://localhost:5432/smq");
        // username and password
        return dataSource;
    }

in other @Configuration class:

@Bean(name="camundaBpmDataSource")
    public DataSource camundaDataSource() {
        SimpleDriverDataSource dataSource = new SimpleDriverDataSource();
        dataSource.setDriverClass(org.postgresql.Driver.class);
        dataSource.setUrl("jdbc:postgresql://localhost:5432/camunda");
        // username and password
        return dataSource;
    }

The problem is when I run a java delegate I can’t commit to my primary data source. I don’t receive an error just id does not commit to the data base. code of the java dalegete:

package tn.itserv.smq.application.delegete;

import org.camunda.bpm.engine.delegate.DelegateExecution;
import org.camunda.bpm.engine.delegate.JavaDelegate;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import org.springframework.stereotype.Component;
import org.springframework.transaction.annotation.Transactional;
import tn.itserv.smq.application.model.Opportunity;
import tn.itserv.smq.application.model.ROaction;
import tn.itserv.smq.application.model.Risk;
import tn.itserv.smq.application.service.OpportunityService;
import tn.itserv.smq.application.service.ROactionService;
import tn.itserv.smq.application.service.RiskService;

@Component
public class CreateNewROAction implements JavaDelegate {

    private final ROactionService rOactionService;
    private final RiskService riskService;
    private final OpportunityService opportunityService;
    private Logger logger = LoggerFactory.getLogger(CreateNewROAction.class);

    public CreateNewROAction(ROactionService rOactionService, RiskService riskService, OpportunityService opportunityService) {
        this.rOactionService = rOactionService;
        this.riskService = riskService;
        this.opportunityService = opportunityService;
    }

    @Override @Transactional
    public void execute(DelegateExecution delegateExecution) throws Exception {

        String processInstanceId = delegateExecution.getProcessInstanceId();
        String entityType = delegateExecution.getVariable("entityType").toString();
        logger.info("entityType variable value: "+entityType);
        if(entityType.equalsIgnoreCase("risk")){
            String riskId = delegateExecution.getVariable("riskId").toString();
            Risk risk = riskService.findById(Long.parseLong(riskId));
            ROaction rOaction = new ROaction();
            rOaction.setProcessInstanceId(processInstanceId);
            rOaction = saveROAction(rOaction);
            logger.info("RO-Action created: "+rOaction);
            risk.setAction(rOaction);
            risk = updateRisk(risk);
            logger.info("risk updated: "+risk);
            delegateExecution.setVariable("id", rOaction.getId());
        }
        else if (entityType.equalsIgnoreCase("opportunity")){
            String opportunityId = delegateExecution.getVariable("opportunityId").toString();
            Opportunity opportunity = opportunityService.findById(Long.parseLong(opportunityId));
            ROaction rOaction = new ROaction();
            rOaction.setProcessInstanceId(processInstanceId);
            rOaction = saveROAction(rOaction);
            opportunity.setAction(rOaction);
            opportunity = updateOpportunity(opportunity);
            delegateExecution.setVariable("id", rOaction.getId());
        }
    }
    @Transactional(value = "REQUIRES_NEW")
    ROaction saveROAction(ROaction rOaction){
         return rOactionService.save(rOaction);
    }
    @Transactional(value = "REQUIRES_NEW")
    Risk updateRisk(Risk risk){
        return riskService.update(risk, risk.getId());
    }
    @Transactional(value = "REQUIRES_NEW")
    Opportunity updateOpportunity(Opportunity opportunity){
        return opportunityService.save(opportunity);
    }
}

the result of the logs are:


2019-07-26 14:14:19.292  INFO 8688 --- [nio-8080-exec-6] t.i.s.a.delegete.CreateNewROAction       : entityType variable value: risk
2019-07-26 14:14:19.388  INFO 8688 --- [nio-8080-exec-6] t.i.s.a.delegete.CreateNewROAction       : RO-Action created: ROaction(id=null, description=null, status=In progress, responsible=null, deadline=null, AchievementDate=null, effectivenessEvaluationDate=null, effectivenessEvaluation=null, processInstanceId=226, createdAt=Fri Jul 26 14:14:19 CET 2019, updatedAt=Fri Jul 26 14:14:19 CET 2019)
2019-07-26 14:14:19.399  INFO 8688 --- [nio-8080-exec-6] t.i.s.a.delegete.CreateNewROAction       : risk updated: Risk(id=3, description=some description, probability=3, impact=3, cause=cause, consequence=consequence, action=ROaction(id=null, description=null, status=In progress, responsible=null, deadline=null, AchievementDate=null, effectivenessEvaluationDate=null, effectivenessEvaluation=null, processInstanceId=226, createdAt=Fri Jul 26 14:14:19 CET 2019, updatedAt=Fri Jul 26 14:14:19 CET 2019), processes=[Process(id=5, name=Development and integration, type=Realisation, createdAt=2019-07-26 12:35:35.057282, updatedAt=2019-07-26 12:35:35.057282, pilot=null), Process(id=4, name=Consulting, type=Realisation, createdAt=2019-07-26 12:35:35.057282, updatedAt=2019-07-26 12:35:35.057282, pilot=null), Process(id=3, name=Project management, type=Realisation, createdAt=2019-07-26 12:35:35.057282, updatedAt=2019-07-26 12:35:35.057282, pilot=null)], processInstanceId=201, updatedBy=null, createdAt=2019-07-26 13:56:23.915, updatedAt=2019-07-26 13:56:47.79)

when I call the service from controllers I have no problem to commit the data. The problem occure just in java delegate.

I would be grateful for the help :smiley: .

Maybe I wasn’t clear in my question so if you need any clarification please do not hesitate. I’m really stuck with this problem for more than a week now.

@chiheb try removing all the @Transactional annotations in the delegate.

@aravindhrs At first, I had no @Transactional annotation. I added it later as a solution but it didn’t work.

did you checked without @Transactional it worked or not?

yes I just did and it did not work @aravindhrs

from java delegate you are updating process variables and it uses camundaBpmDataSource . From delegate you’re calling services like saveROAction,updateRisk,updateOpportunity which uses primary datasource.

The execute() function is a java delegate implementation and it doesn’t require @Transactional annotation at that method level. Camunda handles transaction management for camundaBpmDataSource

You can use @Transactional annotation only for these functions saveROAction, updateRisk, updateOpportunity.

So I deleted the @Trasactional annotation from execute() and kept it for the other functions but the issue still exists.
I just want to mention that the configuration classes are annotated with @Configaration annotation only.

can you upload your config classes

CamundaEngineConfig.txt (2.8 KB)
Config.txt (1.4 KB)

Is there a solution for this. I have the same exact problem, the transaction is not commited to the database.