1

I have 4 databases with similar schema on PostgreSQL

My current code is like this

ressources

spring.datasource.url=jdbc:postgresql://localhost:5432/postgres
spring.datasource.username=postgres
spring.datasource.password=postgres

DAO

public interface AccountRepository extends JpaRepository<Account, Long>{}

Configuration

@Configuration
public class AccountServiceConfiguration {
    @Autowired
    private AccountRepository accountRepository;

    @Bean
    public AccountService accountService() {
        return new AccountService(accountRepository);
    }
}

Controller

@RestController
@RequestMapping("/accounts")
public class AccountController {
    @Autowired
    private AccountService accountService;

    @RequestMapping(name = "/", method = RequestMethod.GET)
    public Page<Account> getAccounts(Integer page, Integer size) {
        return accountService.getAll(page, size);
    }
}

Service

public class AccounttService {
    public AccounttService(AccountRepository accountRepository) {
        this.accountRepository = accountRepository;
    }
    public Page<Account> getAll(Integer page, Integer size) {
        PageRequest pageRequest = new PageRequest(page, size);
        return accountRepository.findAll(pageRequest);
    }
}

I want to change like this

ressources

spring.db1.url=jdbc:postgresql://db1:5432/postgres
spring.db1.username=postgres1
spring.db1.password=postgres1

spring.db2.url=jdbc:postgresql://db2:5432/postgres
spring.db2.username=postgres2
spring.db2.password=postgres2

spring.db3.url=jdbc:postgresql://db3:5432/postgres
spring.db3.username=postgres3
spring.db3.password=postgres3

spring.db4.url=jdbc:postgresql://db4:5432/postgres
spring.db4.username=postgres4
spring.db4.password=postgres4

Controller

...
public Page<Account> getAccounts(Integer page, Integer size, string env) {
    return accountService.getAll(page, size, env);
}
...

Service

public class AccounttService {
    public AccounttService(Map<AccountRepository> mapAccountRepository) {
        this.mapAccountRepository = mapAccountRepository;
    }
    public Page<Account> getAll(Integer page, Integer size, String env) {
        PageRequest pageRequest = new PageRequest(page, size);
        // search in specific env
    }
}

How to load 4 data sources (may be on map) and search by environnement ! If i send env=db1 i want to run my request on db1

If you have other solution, i take it, but must use one repository and one entity to search in all databases.

Thank you :)

Aberwag
  • 11
  • 5
  • this will help.. https://github.com/spring-projects/spring-data-examples/tree/master/jpa/multiple-datasources – J J Dec 19 '16 at 11:22
  • I try this example, but i need to use sigle repository (not multiple repository and multiple configuration) – Aberwag Dec 19 '16 at 11:49
  • What is your question? What do you want to achieve? if you want to change your resources, just do it. But I doubt, that you really want that. – Jens Schauder Dec 19 '16 at 13:36
  • My question is how to load 4 data sources (may be on map) and search by environnement ! If i send env=db1 and run my request on db1 – Aberwag Dec 19 '16 at 13:42
  • 1
    Would a multitenant approach work on this? http://anakiou.blogspot.co.uk/2015/08/multi-tenant-application-with-spring.html – farrellmr Dec 20 '16 at 09:58

2 Answers2

0

According to your comments you want a single Repository instance to switch between different schemata.

This won't work.

What you can do is provide a Facade for multiple Repository instance that delegates each call to on of the many instances according to some parameter/field/property.

But one way or the other you have to create a separate Repository instance with a different database connection for each.

Jens Schauder
  • 65,795
  • 24
  • 148
  • 294
0

What you are describing is called multi-tenancy using multiple databases. To accomplish so you would need to manually configure the persistence layer and not to rely completely in Spring Boot auto-configuration capabilities.

The persistence layer configuration involves:

  • Hibernate, JPA and datasources properties
  • Datasources beans
  • Entity manager factory bean (in the case of Hibernate, with properties specifying this is a mult-tenant entity manager factory bean and tenant connection provider as well as tenant resolver)
  • Transaction manager bean
  • Spring Data JPA and transaction support configuration

In a blog post I recently published: Multi-tenant applications using Spring Boot, JPA, Hibernate and Postgres I cover in this exact problem with a detailed implementation.

ootero
  • 2,641
  • 2
  • 10
  • 18