其他分享
首页 > 其他分享> > 使用事务注解导致DS数据源切换失效解决方法

使用事务注解导致DS数据源切换失效解决方法

作者:互联网

先来看一下我的数据源配置,通过不同端口模拟多数据源配置:

 

 目前业务需求是对superdata库里的数据做修改的同时game分库也同步进行修改,虽然只有一次关联还是使用了事务保证原子性,加上注解 @Transactional(rollbackFor = Exception.class)参数加上"rollbackFor = Exception.class"异常时回滚

    @Override
    @Transactional(rollbackFor = Exception.class)
    public int updateInfoById(SuperChannelNotice superChannelNotice) {
        int updateById = getBaseMapper().updateById(superChannelNotice);
        operationChannelNoticeService.updateActivity(superChannelNotice.getGameId(), superChannelNotice);
        return updateById;
    }

加上事务注解后会切换数据源失效导致找不到对应的表:

解决办法:

内层代码加事务注解并加入参数(propagation = Propagation.REQUIRES_NEW)

该参数的作用是使内层方法被调用时,外层方法的事务被挂起。内层事务相对于外层事务是完全独立的,具有独立隔离性互不干扰。

    @Override
    @Transactional(rollbackFor = Exception.class)
    public int updateInfoById(SuperChannelNotice superChannelNotice) {
        int updateById = getBaseMapper().updateById(superChannelNotice);
        operationChannelNoticeService.updateActivity(superChannelNotice.getGameId(), superChannelNotice);
        return updateById;
    }
    @Override
    @Transactional(rollbackFor = Exception.class,propagation = Propagation.REQUIRES_NEW)
    public int updateActivity(Integer gameId, SuperChannelNotice superChannelNotice) {
        OperationChannelNotice operationChannelNotice = new OperationChannelNotice();
        BeanUtils.copyProperties(superChannelNotice,operationChannelNotice);
        return getBaseMapper().updateById(operationChannelNotice);
    }

加入该参数后问题得到解决:

 

标签:Exception,superChannelNotice,updateById,int,数据源,rollbackFor,class,注解,DS
来源: https://blog.csdn.net/weixin_49128211/article/details/122437578