> ## Documentation Index
> Fetch the complete documentation index at: https://docs.activeviam.com/llms.txt
> Use this file to discover all available pages before exploring further.

# Use multiple databases

> How to connect Atoti Limits to multiple external databases by configuring additional datasources, mapping JPA repositories to datatables, and exposing custom transaction managers

### 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.

```java theme={"languages":{"custom":["/engine/python-sdk/0.9/languages/pycon.tmLanguage.json"]}}
@EnableJpaRepositories(
    entityManagerFactoryRef = "yourEntityManagerFactory",
    transactionManagerRef = "yourTransactionManager",
    basePackageClasses = {
        ILimitStructuresJpaRepository.class
    })
```

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

```java theme={"languages":{"custom":["/engine/python-sdk/0.9/languages/pycon.tmLanguage.json"]}}
@Bean
public LocalContainerEntityManagerFactoryBean yourEntityManagerFactory() {
  final LocalContainerEntityManagerFactoryBean em = new LocalContainerEntityManagerFactoryBean();
  // configure me with your datasource...
  return em;
}

@Bean
public PlatformTransactionManager yourTransactionManager() {
  return new JpaTransactionManager(entityManagerFactory);
}
```

3. [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.

```java theme={"languages":{"custom":["/engine/python-sdk/0.9/languages/pycon.tmLanguage.json"]}}
@Transactional(LIMITS_TRANSACTION_MANAGER)
public class DefaultLimitsAppCrudService {
  //...
}
```
