【发布时间】:2019-07-21 12:02:49
【问题描述】:
我有 2 个数据源,ds1,ds2。在@Transactional 的单一服务中,我必须从两个表中获取值并更新它们。
示例 sn-p
@Service
public class MyService {
@Autowired
ds1Repository ds1Repository; // from data source 1 (DB Name - DB1) MYSQL
@Autowired
ds2Repository ds2Repository; // from data source 2 (DB Name - DB2) MYSQL
@Transactional (javax.Transactional)
public void processUpdates() {
// Able to get the result set from both the data sources
List<Data1> ds1Data = ds1Repository.findAll();
List<Data2> ds2Data = ds1Repository.findAll();
// modified the collections ds1Data & ds2Data
// This is getting updated
ds1Repository.saveAll(ds1Data);
// This update is not heppening and no exception thrown
ds2Repository.saveAll(ds2Data);
}
}
我已经尝试了以下 wys:
- 我已经配置了两个工作正常的数据源,能够从两个数据库中读取数据
- 搜索后,尝试了 ChainedTransactionManager,通过为两个数据源定义自定义事务管理器名称并在服务之上使用 @Transactional(value="chainedTransactionManager")。还是不行。
谁能帮我解决一下代码的问题?为什么只有数据源 2 上的数据没有被持久化?
【问题讨论】:
-
你能用
ChainedTransactionManager粘贴你的尝试吗? -
public class MultiDataSourceTransactionManager { @Bean(name = "chainedTransactionManager") public ChainedTransactionManager transactionManager(@Qualifier("transactionManager") PlatformTransactionManager ds1, @Qualifier("tm2") PlatformTransactionManager ds2) { return new ChainedTransactionManager(ds1, ds2); } }
标签: java hibernate spring-data-jpa spring-data