'Multiple Datasources with single JPA Repository type
I have 2 databases with identical schemas, but for 2 different markets (let's say us-db and eu-db). Since the schemas are identical, I can use the same entity classes - the publisher table is the same in both databases, so I only need 1 Publisher class. It follows, that I only need a single Repo:
@Repository
public interface PublisherRepository extends CrudRepository<Publisher, Long> {
}
However, I would like to set it up so that my Spring Context contains 2 Beans of type PublisherRepository - one where the underlying datasource is us-db, and the other - eu-db.
Is it possible to achieve this? I'm also open to other implementation suggestions that eliminate the need for duplication of entity definitions.
Solution 1:[1]
Spring (Boot) don't support multiple data-source out-of-the-box. You can use trick like this https://www.baeldung.com/spring-data-jpa-multiple-databases . I believe that, If Spring did not support multiple datasources out-of-the-box, your try is not good practice.
I think best practices is, using single datasource, but use 2 micro-services.
Or sometime switching environment application.properties file will be helpful to you.
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|---|
| Solution 1 | James Graham |
