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();
}