1

I am having issues creating a spring boot application that can dynamically connect to multiple databases, depending on user input. Basically the application runs the same sql query on different databases. modeling my attempt after this, i have received the following error:

2018-04-10 16:18:50.678 ERROR 15716 --- [  restartedMain] com.zaxxer.hikari.HikariConfig           : HikariPool-1 - dataSource or dataSourceClassName or jdbcUrl is required.

--

Caused by: java.lang.IllegalArgumentException: dataSource or  dataSourceClassName or jdbcUrl is required.

config class:

@Configuration
public class ReconDBConfig {


@ConfigurationProperties(prefix = "spring.datasource.foo")
@Bean
@Primary
public DataSource fooDataSource() {
    return DataSourceBuilder
            .create()
            .build();
}

@ConfigurationProperties(prefix = "spring.datasource.bar")
@Bean
public DataSource barDataSource() {
    return DataSourceBuilder
            .create()
            .build();
}
}

Properties file:

spring.datasource.foo.url        =     jdbc:postgresql://localhost:5432/fooDB
spring.datasource.foo.user       =  admin
spring.datasource.foo.password   =  admin
spring.datasource.foo.driver     =  org.postgresql.Driver
spring.datasource.foo.maxconn    =  5
spring.datasource.foo.expiry     =  180
spring.datasource.foo.cache      =  true
spring.datasource.foo.retry      =  3
spring.datasource.foo.retrydelay =  30

spring.datasource.bar.url        =     jdbc:postgresql://not_localhost:5432/fooDB
spring.datasource.bar.user       =  definitely_not_admin
spring.datasource.bar.password   =  definitely_not_admin
spring.datasource.bar.driver     =  org.postgresql.Driver
spring.datasource.bar.maxconn    =  5
spring.datasource.bar.expiry     =  180
spring.datasource.bar.cache      =  true
spring.datasource.bar.retry      =  3
spring.datasource.bar.retrydelay =  30

any ideas on how i can accomplish this? i am, as you can tell, not super savvy with this multiple db configuration yet.

StillLearningToCode
  • 2,271
  • 4
  • 27
  • 46
  • Does that error show up at every attempt or just after the first connection is succesful? – Matheus Mohr Apr 10 '18 at 22:33
  • every time, well ive only tried building the project less than 10 times... but each one of those times.. – StillLearningToCode Apr 10 '18 at 22:35
  • Comment out one of the beans and see if you get the same error. Having two, the exception is not telling you which one is causing the problem – bichito Apr 10 '18 at 22:40
  • that has been done, same error. i am sure it has something to do with the ' dataSource or dataSourceClassName or jdbcUrl is required' but im not sure how to add that in – StillLearningToCode Apr 10 '18 at 22:42
  • Have you tried configuring it as pointed out [here](https://stackoverflow.com/questions/1921865/how-to-connect-to-multiple-databases-in-hibernate)? – Matheus Mohr Apr 10 '18 at 22:52

1 Answers1

2

DataSourceBuilder.create().build() is going to instantiate HikariDataSource as it is the default DataSource as of SpringBoot 2.0. If you look into HikariDataSource source code the properties are jdbcUrl, username NOT url, user. So, you need to change property keys in application.properties file as follows:

spring.datasource.foo.jdbcUrl=jdbc:postgresql://localhost:5432/fooDB
spring.datasource.foo.username=admin
...
...
spring.datasource.bar.jdbcUrl=jdbc:postgresql://not_localhost:5432/fooDB
spring.datasource.bar.username=definitely_not_admin
...
...

In order to use different dataSource based on some request parameters, you might have to use Spring's AbstractRoutingDataSource as explained here https://spring.io/blog/2007/01/23/dynamic-datasource-routing/.

K. Siva Prasad Reddy
  • 11,786
  • 12
  • 68
  • 95