I am using Spring Java Based configuration for configure multiple database with Spring Data.
In the configuration file, i am creating two data source
for MySQ
I had the same problem and after a lot of headache I stumbled upon this doc that made me feel really dumb :(
All you need is @Primary on one of your DataSources and Spring-Boot won't get confused anymore... Here is one of my configurations... The rest are pretty much identical, pointing to other DBs and with no @Primary on them...
@Configuration
@EnableTransactionManagement
@EntityScan(basePackages = {"somepackage.entities"})
@EnableJpaRepositories(entityManagerFactoryRef = "emfDB1", transactionManagerRef = "tmDB1", basePackages = {"somepackage.repositories"})
class PersistenceDB1 {
@Bean
@Primary
DataSource dsDB1() {
BasicDataSource dataSource = new BasicDataSource();
dataSource.setUrl("jdbc:mysql://someserver:3306/proativo");
dataSource.setUsername("username");
dataSource.setPassword("password");
dataSource.setDriverClassName("com.mysql.jdbc.Driver");
return dataSource;
}
@Bean
@Primary
LocalContainerEntityManagerFactoryBean emfDB1() {
LocalContainerEntityManagerFactoryBean entityManagerFactoryBean = new LocalContainerEntityManagerFactoryBean();
entityManagerFactoryBean.setDataSource(dsDB1());
entityManagerFactoryBean.setJpaVendorAdapter(new EclipseLinkJpaVendorAdapter());
entityManagerFactoryBean.setPersistenceXmlLocation("classpath:META-INF/DB1-persistence.xml");
Properties jpaProperties = new Properties();
jpaProperties.put("eclipselink.weaving", "false");
jpaProperties.put("eclipselink.logging.level", "SEVERE"); // SEVERE / FINEST
entityManagerFactoryBean.setJpaProperties(jpaProperties);
return entityManagerFactoryBean;
}
@Bean
@Primary
JpaTransactionManager tmDB1() {
JpaTransactionManager transactionManager = new JpaTransactionManager();
transactionManager.setEntityManagerFactory(emfDB1().getNativeEntityManagerFactory());
return transactionManager;
}
}
Edit: Forgot to mention: Probably due to the way my configuration classes are done, the method of excluding some classes on @EnableAutoConfiguration didn't work for me...