springboot集成activiti工作流service事务不生效问题解决

环境是springboot和mybatis,事务配置用的是spring默认的。
我通过controller层调用service1的方法,然后在service1的方法里面调用service2的方法,当service2的方法正常执行成功后,service1的方法还没有结束,继续往下执行,此时service1方法抛出异常,经过多次尝试,发现service1的事务回滚了,而service2事务没有回滚

service1代码如下:

@Transactional(rollbackFor = Exception.class,propagation = Propagation.REQUIRED)
    public void startWorkflowByKey() {
                //service1调用service2方法
        service2.startWorkflowByKey();

                //service1修改数据
        mapper.updateLeaveAll(leave);
        /*try {
            Thread.sleep(5000);
        } catch (InterruptedException e) {
            e.printStackTrace();
        }*/
                //抛出异常
        if(true){
            throw new RuntimeException();
        }
    }

service2代码如下:

@Service
public class WorkflowServiceImpl implements WorkflowService {
    @Autowired
    private RuntimeService runtimeService;
    @Autowired
    private HistoryService historyService;
    @Autowired
    private TaskService taskService;
    @Autowired
    private IdentityService identityService;
    @Autowired
    private RepositoryService repositoryService;
    @Override
    @Transactional(rollbackFor = Exception.class,propagation = Propagation.REQUIRED)
    public ProcessInstance startWorkflowByKey() {
        runtimeService.startProcessInstanceByKey();
        return instance;
    }
}

我让service1线程休眠5秒钟,service1的事务在5秒内没有提交,此时去页面查询,发现service2方法执行完后,事务已经提交了,service2上我加了Propagation.REQUIRED,应该是加入service1的事务才对,但是service2的事务却自己提交了。

因此这两个service使用的肯定不是同一个事务控制器,于是检查springboot的配置:

@Bean
public SpringProcessEngineConfiguration springProcessEngineConfiguration(MasterDataSource dataSource, DataSourceTransactionManager transactionManager){
    SpringProcessEngineConfiguration spec = new SpringProcessEngineConfiguration();
    spec.setDataSource(dataSource);
    这里将datasource放入transactionManager中,事务就生效了
    transactionManager.setDataSource(dataSource);
    spec.setTransactionManager(transactionManager);
    //解决流程png中文乱码
    spec.setActivityFontName("宋体");
    spec.setLabelFontName("宋体");
    //spec.set
    //若表不存在,自动新建表
    spec.setDatabaseSchemaUpdate("true");
    Resource[] resources = null;
    // 自动部署静态bpmn文件
    try {
        resources = new PathMatchingResourcePatternResolver().getResources("classpath*:processes/*.bpmn");
    } catch (IOException e) {
        e.printStackTrace();
    }
    spec.setDeploymentResources(resources);
    return spec;
}
  • 1
    点赞
  • 6
    收藏
    觉得还不错? 一键收藏
  • 2
    评论
以下是一个简单的 Spring Boot 集成 Activiti 工作流的示例代码: 1. 在 pom.xml 中添加依赖: ```xml <dependency> <groupId>org.activiti</groupId> <artifactId>activiti-spring-boot-starter-basic</artifactId> <version>${activiti.version}</version> </dependency> ``` 2. 创建一个 Activiti 配置类: ```java @Configuration public class ActivitiConfig { @Bean public ProcessEngineConfiguration processEngineConfiguration(DataSource dataSource, PlatformTransactionManager transactionManager) { SpringProcessEngineConfiguration configuration = new SpringProcessEngineConfiguration(); configuration.setDataSource(dataSource); configuration.setTransactionManager(transactionManager); configuration.setDatabaseSchemaUpdate(ProcessEngineConfiguration.DB_SCHEMA_UPDATE_TRUE); configuration.setAsyncExecutorActivate(false); return configuration; } @Bean public ProcessEngineFactoryBean processEngineFactoryBean(ProcessEngineConfiguration processEngineConfiguration) { ProcessEngineFactoryBean factoryBean = new ProcessEngineFactoryBean(); factoryBean.setProcessEngineConfiguration(processEngineConfiguration); return factoryBean; } @Bean public RepositoryService repositoryService(ProcessEngine processEngine) { return processEngine.getRepositoryService(); } @Bean public RuntimeService runtimeService(ProcessEngine processEngine) { return processEngine.getRuntimeService(); } @Bean public TaskService taskService(ProcessEngine processEngine) { return processEngine.getTaskService(); } @Bean public HistoryService historyService(ProcessEngine processEngine) { return processEngine.getHistoryService(); } @Bean public ManagementService managementService(ProcessEngine processEngine) { return processEngine.getManagementService(); } @Bean public IdentityService identityService(ProcessEngine processEngine) { return processEngine.getIdentityService(); } @Bean public FormService formService(ProcessEngine processEngine) { return processEngine.getFormService(); } } ``` 3. 创建一个简单的工作流程: ```xml <?xml version="1.0" encoding="UTF-8"?> <definitions id="definitions" targetNamespace="http://www.activiti.org/test" xmlns="http://www.omg.org/spec/BPMN/20100524/MODEL" xmlns:bpmndi="http://www.omg.org/spec/BPMN/20100524/DI" xmlns:omgdc="http://www.omg.org/spec/DD/20100524/DC" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://www.omg.org/spec/BPMN/20100524/MODEL BPMN20.xsd"> <process id="testProcess" name="Test Process"> <startEvent id="start" name="Start"></startEvent> <userTask id="task1" name="Task 1" assignee="${user}"></userTask> <exclusiveGateway id="gateway1"></exclusiveGateway> <userTask id="task2" name="Task 2" assignee="${user}" /> <endEvent id="end" name="End"></endEvent> <sequenceFlow id="flow1" sourceRef="start" targetRef="task1"></sequenceFlow> <sequenceFlow id="flow2" sourceRef="task1" targetRef="gateway1"></sequenceFlow> <sequenceFlow id="flow3" sourceRef="gateway1" targetRef="task2"> <conditionExpression xsi:type="tFormalExpression">${approved == true}</conditionExpression> </sequenceFlow> <sequenceFlow id="flow4" sourceRef="gateway1" targetRef="end"> <conditionExpression xsi:type="tFormalExpression">${approved == false}</conditionExpression> </sequenceFlow> </process> </definitions> ``` 4. 创建一个处理器来启动和完成工作流程: ```java @Service public class WorkflowService { private final TaskService taskService; private final RuntimeService runtimeService; @Autowired public WorkflowService(TaskService taskService, RuntimeService runtimeService) { this.taskService = taskService; this.runtimeService = runtimeService; } public void startWorkflow(String processDefinitionKey, Map<String, Object> variables) { ProcessInstance processInstance = runtimeService.startProcessInstanceByKey(processDefinitionKey, variables); } public void completeTask(String taskId, Map<String, Object> variables) { taskService.complete(taskId, variables); } } ``` 5. 在控制器中使用处理器来启动和完成工作流程: ```java @RestController @RequestMapping("/workflow") public class WorkflowController { private final WorkflowService workflowService; @Autowired public WorkflowController(WorkflowService workflowService) { this.workflowService = workflowService; } @PostMapping("/start") public void startWorkflow(@RequestParam String processDefinitionKey, @RequestParam String user) { Map<String, Object> variables = new HashMap<>(); variables.put("user", user); workflowService.startWorkflow(processDefinitionKey, variables); } @PostMapping("/complete") public void completeTask(@RequestParam String taskId, @RequestParam boolean approved) { Map<String, Object> variables = new HashMap<>(); variables.put("approved", approved); workflowService.completeTask(taskId, variables); } } ``` 上述代码中,我们创建了一个 Activiti 配置类来配置 Activiti 引擎,包括数据库配置、事务管理器等。我们还创建了一个简单的工作流程,其中包括一个开始事件、两个用户任务、一个排他网关和一个结束事件。最后,我们创建了一个处理器来启动和完成工作流程,并在控制器中使用处理器来处理具体的请求。
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值