Created
July 26, 2020 13:59
-
-
Save mmafrar/5021f9adbcf2afde90d4e513e8af2e6f to your computer and use it in GitHub Desktop.
Configuring multiple data sources with Spring Boot 2 and Spring Data JPA
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package com.example.demo; | |
import com.zaxxer.hikari.HikariDataSource; | |
import org.springframework.beans.factory.annotation.Qualifier; | |
import org.springframework.boot.autoconfigure.jdbc.DataSourceProperties; | |
import org.springframework.boot.context.properties.ConfigurationProperties; | |
import org.springframework.boot.orm.jpa.EntityManagerFactoryBuilder; | |
import org.springframework.context.annotation.Bean; | |
import org.springframework.context.annotation.Configuration; | |
import org.springframework.context.annotation.Primary; | |
import org.springframework.data.jpa.repository.config.EnableJpaRepositories; | |
import org.springframework.orm.jpa.JpaTransactionManager; | |
import org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean; | |
import org.springframework.transaction.PlatformTransactionManager; | |
import org.springframework.transaction.annotation.EnableTransactionManagement; | |
import javax.persistence.EntityManagerFactory; | |
import javax.sql.DataSource; | |
import java.util.HashMap; | |
import java.util.Map; | |
@Configuration | |
@EnableTransactionManagement | |
@EnableJpaRepositories( | |
entityManagerFactoryRef = "primaryEntityManagerFactory", | |
transactionManagerRef = "primaryTransactionManager", | |
basePackages = {"com.example.demo.repository.primary"}) | |
public class PrimaryDataSourceConfiguration { | |
@Primary | |
@Bean(name = "primaryDataSourceProperties") | |
@ConfigurationProperties("spring.datasource-primary") | |
public DataSourceProperties primaryDataSourceProperties() { | |
return new DataSourceProperties(); | |
} | |
@Primary | |
@Bean(name = "primaryDataSource") | |
@ConfigurationProperties("spring.datasource-primary.configuration") | |
public DataSource primaryDataSource(@Qualifier("primaryDataSourceProperties") DataSourceProperties primaryDataSourceProperties) { | |
return primaryDataSourceProperties.initializeDataSourceBuilder().type(HikariDataSource.class).build(); | |
} | |
@Primary | |
@Bean(name = "primaryEntityManagerFactory") | |
public LocalContainerEntityManagerFactoryBean primaryEntityManagerFactory( | |
EntityManagerFactoryBuilder primaryEntityManagerFactoryBuilder, @Qualifier("primaryDataSource") DataSource primaryDataSource) { | |
Map<String, String> primaryJpaProperties = new HashMap<>(); | |
primaryJpaProperties.put("hibernate.dialect", "org.hibernate.dialect.MySQL5Dialect"); | |
primaryJpaProperties.put("hibernate.hbm2ddl.auto", "create-drop"); | |
return primaryEntityManagerFactoryBuilder | |
.dataSource(primaryDataSource) | |
.packages("com.example.demo.model.primary") | |
.persistenceUnit("primaryDataSource") | |
.properties(primaryJpaProperties) | |
.build(); | |
} | |
@Primary | |
@Bean(name = "primaryTransactionManager") | |
public PlatformTransactionManager primaryTransactionManager( | |
@Qualifier("primaryEntityManagerFactory") EntityManagerFactory primaryEntityManagerFactory) { | |
return new JpaTransactionManager(primaryEntityManagerFactory); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment