Home > Java > javaTutorial > How to Configure and Use Multiple Data Sources in Spring Boot with Spring Data JPA?

How to Configure and Use Multiple Data Sources in Spring Boot with Spring Data JPA?

Patricia Arquette
Release: 2024-10-25 01:18:02
Original
805 people have browsed it

How to Configure and Use Multiple Data Sources in Spring Boot with Spring Data JPA?

Spring Boot, Spring Data JPA with Multiple DataSources

Using @EnableAutoConfiguration and application.properties:

  1. Define multiple data source configurations in application.properties.
  2. Enable @EnableAutoConfiguration for the primary data source.
  3. Manually create DataSource, EntityManagerFactory, and TransactionManager for additional data sources.
  4. Specify the appropriate transaction manager in @Transactional annotations for each data source.

Example application.properties:

# Primary Data Source
spring.datasource.url=jdbc:h2:mem:default
spring.datasource.username=sa
spring.datasource.password=

# Additional Data Source
additional.datasource.url=jdbc:h2:mem:additional
additional.datasource.username=anotheruser
additional.datasource.password=anotherpassword
Copy after login

Example Configuration Class for Primary Data Source:

<code class="java">@Configuration
@EnableTransactionManagement
@EnableAutoConfiguration
@EnableJpaRepositories(
        entityManagerFactoryRef = "entityManagerFactory",
        transactionManagerRef = "transactionManager",
        basePackages = {"com.example.repository.primary"})
public class PrimaryDataSourceConfig {

    @Bean
    public EntityManagerFactory entityManagerFactory() {
        // Create and configure EntityManagerFactory manually
    }

    @Bean
    public PlatformTransactionManager transactionManager() {
        // Create and configure PlatformTransactionManager manually
    }
}</code>
Copy after login

Example Configuration Class for Additional Data Source:

<code class="java">@Configuration
@EnableTransactionManagement
@EnableJpaRepositories(
        entityManagerFactoryRef = "additionalEntityManagerFactory",
        transactionManagerRef = "additionalTransactionManager",
        basePackages = {"com.example.repository.additional"})
public class AdditionalDataSourceConfig {

    @Value("${additional.datasource.url}")
    private String url;

    @Value("${additional.datasource.username}")
    private String username;

    @Value("${additional.datasource.password}")
    private String password;

    @Bean
    public DataSource additionalDataSource() {
        // Create and configure DataSource manually
    }

    @Bean
    public EntityManagerFactory additionalEntityManagerFactory() {
        // Create and configure EntityManagerFactory manually
    }

    @Bean
    public PlatformTransactionManager additionalTransactionManager() {
        // Create and configure PlatformTransactionManager manually
    }
}</code>
Copy after login

Example Service Class with Transactional Annotations:

<code class="java">@Service
@Transactional("transactionManager") // Use primary transaction manager
public class PrimaryService {

    // ...
}

@Service
@Transactional("additionalTransactionManager") // Use additional transaction manager
public class AdditionalService {

    // ...
}</code>
Copy after login

This approach provides greater control and flexibility when working with multiple data sources in Spring Boot applications.

The above is the detailed content of How to Configure and Use Multiple Data Sources in Spring Boot with Spring Data JPA?. For more information, please follow other related articles on the PHP Chinese website!

source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Latest Articles by Author
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template