In this project we will configure and use multiple datasource
FIRST db config
- spring.first.datasource.jdbcUrl=jdbc:mysql://url-to-mysql-db
- spring.first.datasource.username={username}
- spring.first.datasource.password={password}
- spring.first.datasource.driverClassName=com.mysql.cj.jdbc.Driver
SECOND db config
- spring.second.datasource.jdbcUrl=jdbc:postgresql://url-to-postgresql-db
- spring.second.datasource.username={username}
- spring.second.datasource.password={password}
- spring.second.datasource.driverClassName=org.postgresql.Driver
- Beans
@Primary
@Bean(name = "primaryDataSource")
@ConfigurationProperties(prefix = "spring.first.datasource")
public DataSource dataSource() {
return DataSourceBuilder
.create()
.build();
}
@Primary
@Bean(name = "primaryEntityManagerFactory")
public LocalContainerEntityManagerFactoryBean
entityManagerFactory(EntityManagerFactoryBuilder builder,
@Qualifier("primaryDataSource") DataSource dataSource) {
return builder
.dataSource(dataSource)
.packages(PrimaryEntity.class)
.persistenceUnit("primaryPU")
.build();
}
@Primary
@Bean(name = "primaryTransactionManager")
public PlatformTransactionManager transactionManager(
@Qualifier("primaryEntityManagerFactory") EntityManagerFactory
entityManagerFactory) {
return new JpaTransactionManager(entityManagerFactory);
}
- Config annotations : reference the entity manager and the transaction manager as bellow. the basePackages should point to the repository location.
@Configuration
@EnableTransactionManagement
@EnableJpaRepositories(
entityManagerFactoryRef = "primaryEntityManagerFactory",
transactionManagerRef = "primaryTransactionManager",
basePackages = {"com.tutorials.multipledatasource.primary.repo"}
)
Primary annotation can not be used more than one time
You should respect the specified basePackages for each datasource