@Transactional事务无效的一个原因

Most Spring applications only need a single transaction manager, but there may be situations where you want multiple independent transaction managers in a single application. The value attribute of the @Transactional annotation can be used to optionally specify the identity of the PlatformTransactionManager to be used. This can either be the bean name or the qualifier value of the transaction manager bean。

大致意思是在Spring2.5之前,@Transactional注解确实不支持多数据源,但是Spring3为其增加了value属性配置,可以指定当前事务通知对应的事务管理器。
 

由于是本地存在多个mysql数据源,事务没指定value则是默认的那个数据源 导致事务不生效,加了value就行,value 是adminv2TransactionManager的bean id

先声明一个事务,指定datasource
@Bean
    public DataSourceTransactionManager adminv2TransactionManager(@Qualifier("adminv2DataSource") DataSource adminv2DataSource){
        DataSourceTransactionManager adminv2TransactionManager = new DataSourceTransactionManager();
        adminv2TransactionManager.setDataSource(adminv2DataSource);
        log.info("init adminv2TransactionManager...");
        return adminv2TransactionManager;
    }




@Transactional(value ="adminv2TransactionManager",rollbackFor = Exception.class)
    public JSONObject enable(String ids, String status) {
        try {
            ///
        } catch (Exception e) {
            //手动标志事务回滚
            TransactionAspectSupport.currentTransactionStatus().setRollbackOnly();
            log.info("事务回滚");
            return SuccessVo.errors();
        }
        return SuccessVo.defaults();
    }

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值