我在同一个应用程序中配置了带有2个MySQL DB的SpringBoot。 来自不同包的两个DB。
com.appscook.keycloak.usermanagement.sdk.entity
com.appscook.library.domain
属性文件
#SDK DB CONFIG
keycloaksdk.datasource.driver-class-name = com.mysql.jdbc.Driver
keycloaksdk.datasource.url = jdbc:mysql://localhost:3307/organization?autoReconnect=true
keycloaksdk.datasource.username=root
keycloaksdk.datasource.password=password
#LIBRARY DB CONFIG
library.datasource.driver-class-name = com.mysql.jdbc.Driver
library.datasource.url = jdbc:mysql://192.168.0.188:3306/library_keycloak?autoReconnect=true
library.datasource.username = jijesh
library.datasource.password = jijesh
# Keep the connection alive if idle for a long time (needed in production)
spring.datasource.testWhileIdle = true
spring.datasource.timeBetweenEvictionRunsMillis = 60000
spring.datasource.validationQuery = SELECT 1
# Number of ms to wait before throwing an exception if no connection is available.
spring.datasource.max-wait=10000
# Maximum number of active connections that can be allocated from this pool at the same time.
spring.datasource.max-active=50
# Validate the connection before borrowing it from the pool.
spring.datasource.test-on-borrow=true
# Show or not log for each sql query
spring.jpa.show-sql = true
spring.jpa.generate-ddl=true
# Hibernate ddl auto (create, create-drop, update)
spring.jpa.hibernate.ddl-auto=
spring.jpa.database-platform=org.hibernate.dialect.MySQL5Dialect
hibernate.temp.use_jdbc_metadata_defaults = false
# Naming strategy
spring.jpa.hibernate.naming-strategy = org.hibernate.cfg.ImprovedNamingStrategy
# The SQL dialect makes Hibernate generate better SQL for the chosen database
spring.jpa.properties.hibernate.dialect = org.hibernate.dialect.MySQL5Dialect
DB1配置
@Configuration
@ComponentScan("com.appscook.library.service")
@EnableTransactionManagement
@EnableJpaRepositories(
entityManagerFactoryRef = "libraryEntityManagerFactory",
transactionManagerRef = "libraryTransactionManager",
basePackages = "com.appscook.library.repository")
public class LibraryDBConfig {
@Bean
@Primary
@ConfigurationProperties(prefix = "library.datasource")
public DataSource hrmsDataSource() {
return DataSourceBuilder.create().build();
}
@Bean
public LocalContainerEntityManagerFactoryBean libraryEntityManagerFactory(
final EntityManagerFactoryBuilder builder) {
return builder.dataSource(hrmsDataSource())
.packages("com.appscook.library.domain")
.persistenceUnit("libraryPersistenceUnit").build();
}
@Bean
public JpaTransactionManager hrmsTransactionManager(
@Qualifier("libraryEntityManagerFactory") final EntityManagerFactory factory) {
return new JpaTransactionManager(factory);
}
}
Db2 config
@Configuration
@ComponentScan("com.appscook.keycloak.usermanagement.sdk.services")
@EnableTransactionManagement
@EnableJpaRepositories(
entityManagerFactoryRef = "keycloakEntityManagerFactory",
transactionManagerRef = "keycloakTransactionManager",
basePackages = "com.appscook.keycloak.usermanagement.sdk.repository")
public class KeycloakSDKDBConfig {
@Bean
@ConfigurationProperties(prefix = "keycloaksdk.datasource")
public DataSource keycloakDataSource() {
return DataSourceBuilder.create().build();
}
@Bean
public LocalContainerEntityManagerFactoryBean keycloakEntityManagerFactory(
final EntityManagerFactoryBuilder builder) {
return builder.dataSource(keycloakDataSource())
.packages("com.appscook.keycloak.usermanagement.sdk.entity")
.persistenceUnit("keycloakPersistenceUnit").build();
}
@Bean
public JpaTransactionManager keycloakTransactionManager(
@Qualifier("keycloakEntityManagerFactory") final EntityManagerFactory factory) {
return new JpaTransactionManager(factory);
}
}
当我在app上运行显示NoUniqueBeanDefinitionException的控制台时。
Caused by: org.springframework.beans.factory.NoUniqueBeanDefinitionException: No qualifying bean of type [javax.persistence.EntityManagerFactory] is defined: expected single matching bean but found 2: libraryEntityManagerFactory,keycloakEntityManagerFactory
答案 0 :(得分:0)
如果您遇到openEntityManagerInViewInterceptor
bean问题,请设置
应用程序属性文件中的spring.jpa.open-in-view=false
。如documentation中所述,此bean将默认的EntityManager(它从默认的EntityManagerFactory获取)绑定到请求线程,并且由于某种原因,默认情况下启用它。
您还可以考虑将其中一个工厂作为主要工厂:
@Bean
@Primary
public LocalContainerEntityManagerFactoryBean libraryEntityManagerFactory(
final EntityManagerFactoryBuilder builder) {
return builder.dataSource(hrmsDataSource())
.packages("com.appscook.library.domain")
.persistenceUnit("libraryPersistenceUnit").build();
}