我有两个数据库(第一个和第二个)的Spring启动。 我有这个工作配置:
@Bean
public DataSourceTransactionManager firstDbTransactionManager(@Qualifier ("primaryDataSource") DataSource datasource) {
return new DataSourceTransactionManager(datasource);
}
@Bean
public DataSourceTransactionManager secondDbTransactionManager(@Qualifier("secondaryDataSource") DataSource datasource) {
return new DataSourceTransactionManager(datasource);
}
我的方法需要为两个db都是@Transactional。 我一直这么做,而且工作正常:
@Autowired
private SubscriptionFeeService thisBeanSelfProxy;
@Transactional("firstDbTransactionManager")
public void withdrawalOfMonthlyFee(Project project) throws Exception {
thisBeanSelfProxy.intermediateMethod(project);
}
@Transactional("secondDbTransactionManager")
public void intermediateMethod(Project project) throws Exception {
thisBeanSelfProxy.withdrawalOfMonthlyFeeInner(project);
}
public void withdrawalOfMonthlyFeeInner(Project project) throws Exception {
firstDbRepository.update(project);
secondDbRepostory.addNewOperationToProjectTable(project.generateOperations());
throw new RuntimeException(); //i want rollback both db
}
我正在为@Transactional注释创建intermediateMethod,因为它不支持@Transactional({" firstDbTransactionManager"," secondDbTransactionManager"})。如何通过一个注释打开两个事务?