Created
July 26, 2020 13:59
-
-
Save mmafrar/cd35c857896a738e62dc9db97523417c 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.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 = "secondaryEntityManagerFactory", | |
transactionManagerRef = "secondaryTransactionManager", | |
basePackages = {"com.example.demo.repository.secondary"}) | |
public class SecondaryDataSourceConfiguration { | |
@Bean(name = "secondaryDataSourceProperties") | |
@ConfigurationProperties("spring.datasource-secondary") | |
public DataSourceProperties secondaryDataSourceProperties() { | |
return new DataSourceProperties(); | |
} | |
@Bean(name = "secondaryDataSource") | |
@ConfigurationProperties("spring.datasource-secondary.configuration") | |
public DataSource secondaryDataSource(@Qualifier("secondaryDataSourceProperties") DataSourceProperties secondaryDataSourceProperties) { | |
return secondaryDataSourceProperties.initializeDataSourceBuilder().type(HikariDataSource.class).build(); | |
} | |
@Bean(name = "secondaryEntityManagerFactory") | |
public LocalContainerEntityManagerFactoryBean secondaryEntityManagerFactory( | |
EntityManagerFactoryBuilder secondaryEntityManagerFactoryBuilder, @Qualifier("secondaryDataSource") DataSource secondaryDataSource) { | |
Map<String, String> secondaryJpaProperties = new HashMap<>(); | |
secondaryJpaProperties.put("hibernate.dialect", "org.hibernate.dialect.H2Dialect"); | |
secondaryJpaProperties.put("hibernate.hbm2ddl.auto", "create-drop"); | |
return secondaryEntityManagerFactoryBuilder | |
.dataSource(secondaryDataSource) | |
.packages("com.example.demo.model.secondary") | |
.persistenceUnit("secondaryDataSource") | |
.properties(secondaryJpaProperties) | |
.build(); | |
} | |
@Bean(name = "secondaryTransactionManager") | |
public PlatformTransactionManager secondaryTransactionManager( | |
@Qualifier("secondaryEntityManagerFactory") EntityManagerFactory secondaryEntityManagerFactory) { | |
return new JpaTransactionManager(secondaryEntityManagerFactory); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment