0

I am creating a spring boot application (Spring Boot 2.5). It should work with multiple databases. Unfortunately, it is not known in advance how many bases there will be. In application.properties I have described it as follows.

app.datasource[0].dsp.url = ...
app.datasource[0].dsp.username= ...
app.datasource[0].dsp.password = ...
app.datasource[0].hikari.maximumPoolSize= ...

app.datasource[1].dsp.url = ...
app.datasource[1].dsp.username= ...
app.datasource[1].dsp.password = ...
app.datasource[1].hikari.maximumPoolSize= ...

Config file:

@ConfigurationProperties(prefix = "app")
public class DSProperties {
    private List<DSConfig> datasource;

    public List<DSConfig> getDatasource() {
        return datasource;
    }
}

class DSConfig {
    private DataSourceProperties dsp;
    private HikariConfig hikari;

    public DataSourceProperties getDsp() {
        return dsp;
    }

    public HikariConfig getHikari() {
        return hikari;
    }
}

Create datasource

@Configuration
public class BeanConfigs {

    @Bean
    @Scope(BeanDefinition.SCOPE_PROTOTYPE)
    public DataSource createDataSource(DSProperties dsProperties){
        return dsProperties.getDsConfigs().get(0).getDsp().initializeDataSourceBuilder().build();

        //how to set properties for hikari or how to configure it correctly?
    }
}

I don't quite understand how to apply hikari settings to work with a data source and how to configure datasource

Vadim Kotov
  • 8,084
  • 8
  • 48
  • 62
Violetta
  • 509
  • 4
  • 12
  • All the datasource have the same table ? – Ruokki Feb 14 '22 at 15:18
  • I don't quite understand your issue, really. Is it that you don't know [how to register multiple beans](https://stackoverflow.com/questions/4540713/add-bean-programmatically-to-spring-web-app-context) or you just don't know how to use code to register DataSources? If your case is the latter, [HikariCPs github page has examples](https://github.com/brettwooldridge/HikariCP#rocket-initialization). – Jetto Martínez Feb 14 '22 at 15:21

0 Answers0