I have the following configuration for the main datasource.
@Configuration
@PropertySource({ "classpath:application.properties" })
@EnableJpaRepositories(
basePackages = "com.my.proj.datastores.authentication",
entityManagerFactoryRef = "userEntityManager",
transactionManagerRef = "userTransactionManager"
)
public class SpringDatabaseConfig {
@Autowired
private Environment env;
@Bean
@Primary
public LocalContainerEntityManagerFactoryBean userEntityManager() {
LocalContainerEntityManagerFactoryBean em
= new LocalContainerEntityManagerFactoryBean();
em.setDataSource(userDataSource());
em.setPackagesToScan(
new String[] { "com.my.proj.datastores.authentication" });
HibernateJpaVendorAdapter vendorAdapter
= new HibernateJpaVendorAdapter();
em.setJpaVendorAdapter(vendorAdapter);
HashMap<String, Object> properties = new HashMap<>();
properties.put("hibernate.hbm2ddl.auto",
env.getProperty("hibernate.hbm2ddl.auto"));
properties.put("hibernate.dialect",
env.getProperty("hibernate.dialect"));
em.setJpaPropertyMap(properties);
return em;
}
@Primary
@Bean
public DataSource userDataSource() {
DriverManagerDataSource dataSource
= new DriverManagerDataSource();
dataSource.setDriverClassName(
env.getProperty("spring.datasource.driver-class-name"));
dataSource.setUrl(env.getProperty("spring.datasource.jdbc-url"));
dataSource.setUsername(env.getProperty("spring.datasource.username"));
dataSource.setPassword(env.getProperty("spring.datasource.password"));
return dataSource;
}
@Primary
@Bean
public PlatformTransactionManager userTransactionManager() {
JpaTransactionManager transactionManager
= new JpaTransactionManager();
transactionManager.setEntityManagerFactory(
userEntityManager().getObject());
return transactionManager;
}
}
I also have this configuration for my second datasource :
@Configuration
@PropertySource({ "classpath:application.properties" })
@EnableJpaRepositories(
basePackages = "com.my.proj.datastores.ngl",
entityManagerFactoryRef = "otherTypeEntityManager",
transactionManagerRef = "otherTypeTransactionManager"
)
public class OracleDatabaseConfig {
@Autowired
private Environment env;
@Bean
public LocalContainerEntityManagerFactoryBean otherTypeEntityManager() {
LocalContainerEntityManagerFactoryBean em
= new LocalContainerEntityManagerFactoryBean();
em.setDataSource(otherTypeDataSource());
em.setPackagesToScan(
new String[] { "com.my.proj.datastores.ngl" });
HibernateJpaVendorAdapter vendorAdapter
= new HibernateJpaVendorAdapter();
em.setJpaVendorAdapter(vendorAdapter);
HashMap<String, Object> properties = new HashMap<>();
properties.put("hibernate.hbm2ddl.auto",
env.getProperty("hibernate.hbm2ddl.auto"));
properties.put("hibernate.dialect",
env.getProperty("hibernate.dialect"));
em.setJpaPropertyMap(properties);
return em;
}
@Bean
public DataSource otherTypeDataSource() {
DriverManagerDataSource dataSource
= new DriverManagerDataSource();
dataSource.setDriverClassName(
env.getProperty("spring.second.datasource.driver-class-name"));
dataSource.setUrl(env.getProperty("spring.second.datasource.jdbc-url"));
dataSource.setUsername(env.getProperty("spring.second.datasource.username"));
dataSource.setPassword(env.getProperty("spring.second.datasource.password"));
return dataSource;
}
@Bean
public PlatformTransactionManager otherTypeTransactionManager() {
JpaTransactionManager transactionManager
= new JpaTransactionManager();
transactionManager.setEntityManagerFactory(
otherTypeEntityManager().getObject());
return transactionManager;
}
}
The primary one is for a database that handles auth, the second one is where I want to get data from. In an ideal world, these would be one database, but this is the way this project is.
I also have this application.properties :
## Spring DATASOURCE (DataSourceAutoConfiguration & DataSourceProperties)
spring.datasource.jdbc-url= ${DATASOURCE_URL}
spring.datasource.username= ${DATASOURCE_USERNAME}
spring.datasource.password= ${DATASOURCE_PASSWORD}
spring.datasource.driver-class-name=com.mysql.jdbc.Driver
spring.second.datasource.jdbc-url=jdbc:oracle:thin:@localhost:1521:XE
spring.second.datasource.username=admin
spring.second.datasource.password=password
spring.second.datasource.driver-class-name=oracle.jdbc.driver.OracleDriver
The application runs, and the @Primary
data source seems to work.
However, I am unsure of how to "hook up" that second one so that a repository class uses it over the primary one.
I do have the repositories in different packages because I read that I need to.
The query I am trying to run in my repository is a simple "select all" on a table, and I get an error saying that the table doesn't exist... But I know it is trying to use the @Primary, because the error is a MySql exception, and the second data source is an Oracle one.
Any ideas?