I have a spring boot app that connects to two databases. Everything works fine.
My package structure looks like the following
- db
- bar
- BarDbConfig.java
- domain
- BarModel.java
- repo
- BarRepo.java
- foo
- FooDbConfig.java
- domain
- FooModel.java
- repo
- FooRepo.java
application.properties
spring.jpa.database=default
spring.jpa.database-platform=org.hibernate.dialect.H2Dialect
spring.jpa.hibernate.ddl-auto=none
spring.jpa.hibernate.naming.physical-strategy=org.hibernate.boot.model.naming.PhysicalNamingStrategyStandardImpl
#first db
bar.datasource.jdbc-url=jdbc:h2:C:\\Project\\com.example\\db3.data
bar.datasource.username=admin
bar.datasource.password=admin
bar.datasource.driver-class-name=org.h2.Driver
#second db
foo.datasource.jdbc-url=jdbc:h2:C:\\Project\\com.example\\db2.data
foo.datasource.username=admin
foo.datasource.password=admin
foo.datasource.driver-class-name=org.h2.Driver
BarDbConfig.java
@Configuration
@EnableJpaRepositories(
entityManagerFactoryRef = "barEntityManagerFactory",
transactionManagerRef = "barTransactionManager"
)
public class BarDbConfig {
@Bean
@Primary
@ConfigurationProperties(prefix = "bar.datasource")
public DataSource barDataSource() {
return DataSourceBuilder.create().build();
}
@Primary
@Bean
public LocalContainerEntityManagerFactoryBean barEntityManagerFactory(
EntityManagerFactoryBuilder builder,
@Qualifier("barDataSource") DataSource dataSource
) {
return builder.dataSource(dataSource)
.packages(BarDbConfig.class.getPackage().getName())
.persistenceUnit("barPersistenceUnit")
.build();
}
@Primary
@Bean
public PlatformTransactionManager barTransactionManager(
@Qualifier("barEntityManagerFactory") EntityManagerFactory entityManagerFactory) {
return new JpaTransactionManager(entityManagerFactory);
}
}
FooDbConfig.java
@Configuration
@EnableJpaRepositories(
entityManagerFactoryRef = "fooEntityManagerFactory",
transactionManagerRef = "fooTransactionManager"
)
public class FooDbConfig {
@Bean
@ConfigurationProperties(prefix = "foo.datasource")
public DataSource fooDataSource() {
return DataSourceBuilder.create().build();
}
@Bean
public LocalContainerEntityManagerFactoryBean fooEntityManagerFactory(
EntityManagerFactoryBuilder builder,
@Qualifier("fooDataSource") DataSource dataSource
) {
return builder.dataSource(dataSource)
.packages(FooDbConfig.class.getPackage().getName())
.persistenceUnit("fooPersistenceUnit")
.build();
}
@Bean
public PlatformTransactionManager fooTransactionManager(
@Qualifier("fooEntityManagerFactory") EntityManagerFactory entityManagerFactory) {
return new JpaTransactionManager(entityManagerFactory);
}
}
However, I was wondering if all this boilerplate code was really necessary?
- Isn't there any way to remove all the
LocalContainerEntityManagerFactoryBeanandPlatformTransactionManagerstuff? - Is there any reason why it is necessary to provide all those explicit
Qualifier-names? - Can't spring boot find and autoconfigure the datasources by it self??
- What would be the absolute minimal code required to connect to two databases utilizing convention over configuration?