Using Multiple Databases

Overview

We do not recommend connecting to multiple external databases, but we understand that your limits data may not exist in the same location. It is technically possible to connect Atoti Limits to multiple external databases. This is not a feature we intend to support out-of-the-box, however if you would like to do so, the general steps are:

  1. Create a new datasource configuration in your application.yml similar to the limits.application.datasource configuration
  2. Map the repositories to the datatables you would like to use with this datasource. You will need to add a Spring @Configuration class similar to LimitsJpaConfig.java. The important parts are:

Enabling the JPA Repositories: the repositories specified by basePackageClasses will be mapped to YOUR_TRANSACTION_MANAGER_FACTORY. The example below will only map the ILimitStructuresJpaRepository repository to its equivalent datatable in the datasource.

@EnableJpaRepositories(
    entityManagerFactoryRef = "yourEntityManagerFactory",
    transactionManagerRef = "yourTransactionManager",
    basePackageClasses = {
        ILimitStructuresJpaRepository.class
    })

Exposing your transaction manager: you need to expose your transaction manager as a Spring bean.


@Bean
public LocalContainerEntityManagerFactoryBean yourEntityManagerFactory() {
  LocalContainerEntityManagerFactoryBean em = new LocalContainerEntityManagerFactoryBean();
  // configure me with your datasource...
  return em;
}

@Bean
public PlatformTransactionManager yourTransactionManager() {
  return new JpaTransactionManager(entityManagerFactory);
}
  1. Configure your custom persistence service to use the correct transaction manager. For an example of how this is done see the @Transactional annotation in the DefaultLimitsAppCrudService class.

@Transactional(LIMITS_TRANSACTION_MANAGER)
public class DefaultLimitsAppCrudService {
  //...
}