Cannot change the executortype when there is an existing transaction [How to Solve]

Error content

org.springframework.dao.TransientDataAccessResourceException: Cannot change the ExecutorType when there is an existing transaction
	at org.mybatis.spring.SqlSessionUtils.sessionHolder(SqlSessionUtils.java:157)
	at org.mybatis.spring.SqlSessionUtils.getSqlSession(SqlSessionUtils.java:91)
	at com.baomidou.mybatisplus.core.toolkit.sql.SqlHelper.sqlSessionBatch(SqlHelper.java:55)
	at com.baomidou.mybatisplus.extension.service.impl.ServiceImpl.sqlSessionBatch(ServiceImpl.java:75)
	at com.baomidou.mybatisplus.extension.service.impl.ServiceImpl.saveOrUpdateBatch(ServiceImpl.java:172)
	at com.baomidou.mybatisplus.extension.service.impl.ServiceImpl.saveOrUpdateBatch(ServiceImpl.java:163)
	at com.baomidou.mybatisplus.extension.service.impl.ServiceImpl$$FastClassBySpringCGLIB$$76535273.invoke(<generated>)

Actual use method (error example)

    @Override
    @Transactional(rollbackFor = Exception.class)
    public BaseResp saveOrUpdate(TestPlanParam param) {
        //add, update
============================================================
Omit the codes
============================================================
        //1.New, updated firefighting plans (now with additional master tables)
        this.saveOrUpdate(planEntity);
        Integer testPlanId = planEntity.getId();
        List<PlanTestTeamEntity> teamEntities=new ArrayList<>();
        List<PlanTestResourceEntity> resourceEntities=new ArrayList<>();

        //2.Add update firefighting team (bulk add schedule, need to hook up the id of the main table)
        teams.forEach(team->{
            PlanTestTeamEntity teamEntity=new PlanTestTeamEntity();
            BeanUtil.copyProperties(team,teamEntity);
            teamEntity.setFireFightTestPlanId(testPlanId);
            //planTestTeamService.saveOrUpdate(teamEntity); Solution, using an article-by-article update
            teamEntities.add(teamEntity);
        });
        //3.Add update of fire fighting supplies (bulk add schedule, need to hook up the id of the main table)
        resources.forEach(res->{
            PlanTestResourceEntity resourceEntity=new PlanTestResourceEntity();
            BeanUtil.copyProperties(res,resourceEntity);
            resourceEntity.setFireFightTestPlanId(testPlanId);
            //planTestResourceService.saveOrUpdate(resourceEntity); Solution, using an article-by-article update
            resourceEntities.add(resourceEntity);
        });
        planTestTeamService.saveOrUpdateBatch(teamEntities);
        planTestResourceService.saveOrUpdateBatch(resourceEntities);

        return BaseResp.success();
    }

Error reporting reason

Executortype cannot be changed when a transaction exists

Solution:

When batch updating, first check out all the updated objects, and then update them circularly

Similar Posts: