update不会事务回滚求问大佬们是哪的问题?

# 用spring boot做数据导出时,插入数据抛异常可以事务回滚,但是update却不会事务回滚求问大佬们是哪的问题?

启动类


@SpringBootApplication(scanBasePackages = {"com.moredata"})


@MapperScan({"com.moredata.dao","com.moredata.dao.master"})

@ComponentScan(basePackages = "com.moredata.*")

@EnableTransactionManagement

@EnableAspectJAutoProxy

@EnableScheduling

public class DemoApplication {

    public static void main(String[] args) {
        SpringApplication.run(DemoApplication.class, args);
    }

    @Bean
    public PlatformTransactionManager txManager(DataSource dataSource){
        return new DataSourceTransactionManager(dataSource);
    }

    @Bean
    public Object testBean(PlatformTransactionManager platformTransactionManager){
        System.out.println(">>>>>>>>>>"+platformTransactionManager.getClass().getName());
        return new Object();
    }
}

mapper不会回滚方法



public interface EmGrZsmxSyncDtoMapper {

    @Update({
            "update EM_GR_ZSMX_SYNC",
            "set SYNC_STATE = '1'",
            "where ID = #{id,jdbcType=DECIMAL}"})
    void updateSyncStateById(BigDecimal id);
}

service



@Service

@Transactional(rollbackFor=Exception.class)

public class TestOneServiceImp implements ITestOneService {

@Resource
    TestOneDtoMapper testOneDtoMapper;

    @Resource
    TestTwoDtoMapper testTwoDtoMapper;

    @Resource
    OperationLogDtoMapper operationLogDtoMapper;

    @Resource
    EmGrZsmxSyncDtoMapper emGrZsmxSyncDtoMapper;

    @Resource
    ImpDataDtoMapper impDataDtoMapper;

    @Resource
    EmGrZsmxSyngLogDtoMapper emGrZsmxSyngLogDtoMapper;
@Override

    @Log(methodDesc = "数据导出")

    @Transactional(rollbackFor=Exception.class)
    public void insertEM(EmGrZsmxSyncDto emGrZsmxSyncDto) {

            System.out.println("定时插入进入");
            impDataDtoMapper.insert(emGrZsmxSyncDto);
            emGrZsmxSyncDtoMapper.updateSyncStateById(emGrZsmxSyncDto.getId());
            EmGrZsmxSyngLogDto emGrZsmxSyngLogDto = new EmGrZsmxSyngLogDto();
            emGrZsmxSyngLogDto.setSyncId(emGrZsmxSyncDto.getId());
            emGrZsmxSyngLogDto.setZzbh(emGrZsmxSyncDto.getZzbh());
            emGrZsmxSyngLogDto.setSyncDate(new Date());
            JSONObject jsonObject = JSONObject.fromObject(emGrZsmxSyncDto);
            String jsonStr = jsonObject.toString();
            emGrZsmxSyngLogDto.setSyncContent(jsonStr);
            emGrZsmxSyngLogDtoMapper.insert(emGrZsmxSyngLogDto);
            System.out.println(emGrZsmxSyncDto.getId());
            throw new RuntimeException("运行异常");

    }


}

2个回答

回滚要有异常产生。你的update有产生异常吗?

he_song_
玩辅助的上单 下面我直接throw了一个运行异常啊
7 个月之前 回复

我的是两个数据源的,然后创建事务的时候只能创建一个数据源的,默认的是主数据源,然后从数据源的不会创建,可以通过@Transactional(value=“”)指定数据源创建事务,但是只能指定单一的数据源事务,有什么办法可以指定两个数据源的事务啊

wl1411956542
wl1411956542 除非自己手动控制,因为一个数据库实例的事务不可能操作另一个数据库实例的事务
7 个月之前 回复
Csdn user default icon
上传中...
上传图片
插入图片
抄袭、复制答案,以达到刷声望分或其他目的的行为,在CSDN问答是严格禁止的,一经发现立刻封号。是时候展现真正的技术了!
立即提问