1
votes

My application (spring-boot) need to access multiple databases. For some reason, I can't find a propper example using application.yml

This example: http://smasue.github.io/spring-yml-datasources -> application.yml but not jpa

this example: https://www.baeldung.com/spring-data-jpa-multiple-databases -> jpa but not application.yml

So I created a very simple project based on this gs: https://spring.io/guides/gs/accessing-data-jpa/

You can find my simple example here: https://github.com/Tyvain/JpaMultipleDatabaseAndApplicationYml

    spring:
      datasource:
        db-1:
          url: jdbc:postgresql://10.10.100.100:5432/db1
          username: db1
          password: db1
          driver-class-name: org.postgresql.Driver
        db-2:
          url: jdbc:postgresql://10.10.100.100:5432/db2
          username: db2
          password: db2
          driver-class-name: org.postgresql.Driver

From here, I am not sure how to affect my repositories to each database. This example https://www.baeldung.com/spring-data-jpa-multiple-databases is unclear as it's based on properties... and I am not sure how to adapt all code

@PropertySource({ "classpath:persistence-multiple-db.properties" })
[...]
   properties.put("hibernate.hbm2ddl.auto",
          env.getProperty("hibernate.hbm2ddl.auto"));
        properties.put("hibernate.dialect",
          env.getProperty("hibernate.dialect"));

How would you assign each repo (CustomerRepositoryDB1 and CustomerRepositoryDB2) to their database ?

1
@Tyvain...it will help some how.. medium.com/@joeclever/… - kumar
@kumar It doesn't help: both examples are based on '.properties' (not .yml) and one of them is the same example I gave in the description. - Tyvain
@Tyvain, properties and yaml are two absolutely equal means of configuration. The format is only slgihtly different. Translating properties to yaml format is very simple and straightforward. You just have to make the paths hierarchical and use colons instead of equals signs. - Benjamin Maurer

1 Answers

2
votes

Properties and yaml are two absolutely equal means of configuration. The format is only slgihtly different.

You could just replace foo.properties with foo.yml and

com.foobar.var1=value
com.foobar.var2=value2

simply becomes

com.foobar:
    var1: value
    var2: value2

Plus there is an official Spring Data Repository on Github full of examples. There is even one with two datasources, configured completely in code, no yaml or properties needed:

https://github.com/spring-projects/spring-data-examples/tree/master/jpa/multiple-datasources

In Application.java they exclude the AutoConfig classes and then in each package (Order, Customer), they have a Config class, configuring the datasource. Then there is no need to set the datasource on the repository itself, as that is handled by package scanning with:

factoryBean.setPackagesToScan(OrderConfig.class.getPackage().getName());

in the config. To reiterate: it's datasource per java package, no annotation on the Repository needed.