Reputation: 758
I'm trying to configure two H2 datasource. I tried dozen of tutorials but it still does not works. Now I have two configuration classes
First, default configuration:
@Configuration
@EnableJpaRepositories(
basePackages = "com.yyy.xxx.repository",
entityManagerFactoryRef = "defaultEntityManagerFactory",
transactionManagerRef = "defaultTransactionManager")
@EnableTransactionManagement
public class DefaultJpaConfiguration {
private final JpaVendorAdapter jpaVendorAdapter;
public DefaultJpaConfiguration(JpaVendorAdapter jpaVendorAdapter) {
this.jpaVendorAdapter = jpaVendorAdapter;
}
@Bean(name = "defaultDataSource")
@ConfigurationProperties(prefix = "datasource.default")
public DataSource defaultDataSource() {
return DataSourceBuilder.create().build();
}
@Bean(name = "defaultEntityManagerFactory")
public EntityManagerFactory defaultEntityManagerFactory() {
LocalContainerEntityManagerFactoryBean lef = new LocalContainerEntityManagerFactoryBean();
lef.setDataSource(defaultDataSource());
lef.setJpaVendorAdapter(jpaVendorAdapter);
lef.setPackagesToScan("com.yyy.xxx.entity");
lef.setPersistenceUnitName("defaultPersistenceUnit");
lef.afterPropertiesSet();
return lef.getObject();
}
@Bean(name = "defaultTransactionManager")
public PlatformTransactionManager defaultTransactionManager() {
return new JpaTransactionManager(defaultEntityManagerFactory());
}
@Bean(name = "defaultEntityManager")
public EntityManager defaultEntityManager() {
return defaultEntityManagerFactory().createEntityManager();
}
}
And the second one:
@Configuration
@EnableTransactionManagement
@EnableJpaRepositories(
basePackages = "com.yyy.xxx.adminPassword.repository",
entityManagerFactoryRef = "passwordEntityManagerFactory",
transactionManagerRef = "passwordTransactionManager")
public class PlkPasswordsDatabaseConfiguration {
private final JpaVendorAdapter jpaVendorAdapter;
public PlkPasswordsDatabaseConfiguration(JpaVendorAdapter jpaVendorAdapter) {
this.jpaVendorAdapter = jpaVendorAdapter;
}
@Bean(name = "passwordDataSource")
@ConfigurationProperties(prefix = "datasource.adminPasswords")
public DataSource passwordDataSource() {
return DataSourceBuilder.create().build();
}
@Bean(name = "passwordEntityManagerFactory")
public EntityManagerFactory passwordEntityManagerFactory() {
LocalContainerEntityManagerFactoryBean lef = new LocalContainerEntityManagerFactoryBean();
lef.setDataSource(passwordDataSource());
lef.setJpaVendorAdapter(jpaVendorAdapter);
lef.setPackagesToScan("com.yyy.xxx.adminPassword.entity");
lef.setPersistenceUnitName("passwordPersistenceUnit");
lef.afterPropertiesSet();
return lef.getObject();
}
@Bean(name = "passwordEntityManager")
public EntityManager passwordEntityManager() {
return passwordEntityManagerFactory().createEntityManager();
}
@Bean(name = "passwordTransactionManager")
public PlatformTransactionManager passwordTransactionManager() {
return new JpaTransactionManager(passwordEntityManagerFactory());
}
}
Properties file:
datasource.default.url=jdbc:h2:mem:xxx_db;DB_CLOSE_DELAY=-1;DATABASE_TO_UPPER=false
datasource.default.username=sa
datasource.default.password=
datasource.adminPasswords.url=jdbc:h2:mem:xxx_plk_passwords_db;DB_CLOSE_DELAY=-1;DATABASE_TO_UPPER=false
datasource.adminPasswords.username=sa
datasource.adminPasswords.password=
The error I got say:
Caused by: org.springframework.beans.factory.NoSuchBeanDefinitionException: No bean named 'entityManagerFactory' available
But as you can see in configuration classes entityManagerFactoryRef
is set.
Upvotes: 0
Views: 1795
Reputation: 27078
when you have multiple datasources, EntityManager and PlatformTransactionManager, spring expects you to specify which one of it is the primary one. Add @Primary
on one of the datasource, entitymanager and transactionmanager(doesn't matter which) bean
Upvotes: 2