简体   繁体   中英

Spring Boot, Spring Data JPA with Multiple Hikari DataSources and Single Data source Configuration Java File

How to create a single configuration java file with multiple data sources configuration mentioned in the application.properties such that while adding any 'n' number of datasources. It must handle all the datasources automatically with the same configuration file.

Furthermore, all the datasources must use the same JPARepository to query the data. Also, I want to create a list of entity managers to perform specific operations on specfic datasources.

application.properties

site1.url=jdbc:postgresql://localhost:4567/postgres
site1.username=someUsername
site1.password=somePassword

site2.url=jdbc:postgresql://localhost:5433/postgres
site2.username=someUsername
site2.password=somePassword

SiteConfiguration.java

@Configuration
@EnableTransactionManagement
@EnableJpaRepositories(
        entityManagerFactoryRef = "site1EntityManager",
        transactionManagerRef = "site1TransactionManager",
        basePackages = "com.someProject.repositoryInterface"
)
public class Site1Config extends HikariConfig {

    @Autowired
    private Environment environment;

    @Primary
    @Bean(name = "site1EntityManager")
    public HikariDataSource mysqlDataSource() {
        setJdbcUrl(environment.getProperty("site1.url"));
        setUsername(environment.getProperty("site1.username"));
        setPassword(environment.getProperty("site1.password"));

        return new HikariDataSource(this);
    }

    @Primary
    @Bean(name = "site1EntityManager")
    public LocalContainerEntityManagerFactoryBean mysqlEntityManagerFactory(EntityManagerFactoryBuilder builder) {
        return builder
                .dataSource(mysqlDataSource())
                .packages(ModelClass.class)
                .persistenceUnit("site1PU")
                .build();
    }

    @Primary
    @Bean(name = "site1TransactionManager")
    public PlatformTransactionManager mysqlTransactionManager(@Qualifier("site1EntityManager") EntityManagerFactory entityManagerFactory) {
        return new JpaTransactionManager(entityManagerFactory);
    }

}

This is the basic example of the single datasource configuration file. I want to make SiteConfiguration as generic class in the form of 'site(Number)' and create a separate datasources, entity managers for each of the cases.

AUTOMATICALLY IT SHOULD FETCH THE NUMBER OF DATASOURCES FROM application.properites AND IT SHOULD CONFIGURE ALL THE DATASOURCES MENTIONED IN PROPERTIES FILE.

Also, every datasource must use the same JPARepository.

From what I understand, you need to create an "N" number of datasources depending on how many sites you declared in the application.properties . The only way I see is that you DataSource beans should be created dynamically instead of been statically tied to your application.properties.

In Spring there is a way of doing by combining the use of "ConfigurableBeanFactory" and "BeanFactoryAware".

And there is already an answer to this in StackOverflow: Here

Hope this helps

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM