i am trying to configure two jpa datasource , i did same as this example , every thing is good but is it possible to keep the auto configuration done automatically and just add a new one without having to create LocalContainerEntityManagerFactoryBean
manually.
@Primary
@Bean(name = "dataSource")
@ConfigurationProperties(prefix="spring.datasource")
public DataSource dataSource() {
return DataSourceBuilder.create().build();
}
@Bean
PlatformTransactionManager transactionManager() {
return new JpaTransactionManager(entityManagerFactory().getObject());
}
@Bean
LocalContainerEntityManagerFactoryBean entityManagerFactory() {
HibernateJpaVendorAdapter jpaVendorAdapter = new HibernateJpaVendorAdapter();
jpaVendorAdapter.setGenerateDdl(false);
LocalContainerEntityManagerFactoryBean factoryBean = new LocalContainerEntityManagerFactoryBean();
factoryBean.setDataSource(dataSource());
factoryBean.setJpaVendorAdapter(jpaVendorAdapter);
factoryBean.setPackagesToScan("com.xxxxxxxx.common.domain","com.xxxxxxx.tekram.cdrserver.domain");
return factoryBean;
}
I'm not really sure about what's your concrete problem or what you wish to acomplish, but I'll show you how I've used two dataSources with Spring Boot using the auto configuration features:
Create config for every data source (in separate classes only to better reading):
PrimaryDbConfig.java
SecondaryDbConfig.java
Anotate every DAO with it's respective
PersistenceContext
unitName
:OneDaoImpl.java
AnotherDaoImpl.java
Then in your
src/main/resources/application.properties
As you probably know if you are using the recommended Spring Boot packages naming with these classes and anotations you should be able to use that two dataSources at the same time.