我需要从一个应用程序连接到两个不同的数据库.问题是我的appEntityManager没有与之关联的事务管理器,我不知道该怎么做. @Primary adminentityManager能够使用spring boot提供的那个,没有任何问题,如here所述.
The configuration above almost works on its own. To complete the
picture you need to configure TransactionManagers for the two
EntityManagers as well. One of them Could be picked up by the default
JpaTransactionManager in Spring Boot if you mark it as @Primary. The
other would have to be explicitly injected into a new instance. Or you
might be able to use a JTA transaction manager spanning both.
我已经注释了配置
@EnableTransactionManagement
这是相关的豆子
@Bean
@ConfigurationProperties(prefix = "datasource.app")
public DataSource appDataSource() {
return DataSourceBuilder.create().build();
}
@Bean
@Primary
@ConfigurationProperties(prefix = "datasource.admin")
public DataSource adminDataSource() {
return DataSourceBuilder.create().build();
}
@Bean
public LocalContainerEntityManagerfactorybean appEntityManagerFactory(
final EntityManagerFactoryBuilder builder) {
return builder
.dataSource(appDataSource())
.packages("au.com.mycompany.app.bomcommon.domain")
.persistenceUnit("appPersistentUnit")
.build();
}
@Bean
@Primary
public LocalContainerEntityManagerfactorybean adminentityManagerFactory(
final EntityManagerFactoryBuilder builder) {
return builder
.dataSource(adminDataSource())
.packages("au.com.mycompany.app.bombatch")
.persistenceUnit("adminPersistentUnit")
.build();
}
//I thought this would do it but I am getting an exception
//No qualifying bean of type [org.springframework.transaction.PlatformTransactionManager] is defined: expected single matching bean but found 2: appTransactionManager,transactionManager
@Bean
public JpaTransactionManager appTransactionManager(@Qualifier("appEntityManagerFactory") final EntityManagerFactory emf) {
JpaTransactionManager transactionManager = new JpaTransactionManager();
transactionManager.setEntityManagerFactory(emf);
return transactionManager;
}
更新
我最后以不同的方式做到了这一点. see here.
最佳答案
看看这是否有效:
@Bean
@Primary
@ConfigurationProperties(prefix = "datasource.admin")
public DataSource adminDS() { ... }
@Bean
@Primary
public LocalContainerEntityManagerfactorybean admineMF(...) { ... }
@Bean
@Primary
public JpaTransactionManager adminTM(...) { ... }
@Bean
public LocalContainerEntityManagerfactorybean appEMF(...) { ... }
@Bean
public JpaTransactionManager appTM(...) { ... }
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 [email protected] 举报,一经查实,本站将立刻删除。