Last active
November 26, 2020 15:41
-
-
Save pascaldimassimo/cacad7ae4d41354f7337 to your computer and use it in GitHub Desktop.
Spring Boot config for multiple datasources and EntityManagerFactories
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.pascaldimassimo.xyz; | |
import java.util.HashMap; | |
import java.util.Map; | |
import javax.persistence.EntityManagerFactory; | |
import javax.sql.DataSource; | |
import org.hibernate.cfg.ImprovedNamingStrategy; | |
import org.springframework.beans.factory.annotation.Autowired; | |
import org.springframework.beans.factory.annotation.Qualifier; | |
import org.springframework.boot.autoconfigure.jdbc.DataSourceBuilder; | |
import org.springframework.boot.autoconfigure.orm.jpa.EntityManagerFactoryBuilder; | |
import org.springframework.boot.context.properties.ConfigurationProperties; | |
import org.springframework.context.annotation.Bean; | |
import org.springframework.context.annotation.Configuration; | |
import org.springframework.context.annotation.Primary; | |
import org.springframework.orm.jpa.JpaTransactionManager; | |
import org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean; | |
import org.springframework.transaction.PlatformTransactionManager; | |
@Configuration | |
public class DataSourceConfig { | |
@Bean | |
@Primary | |
@ConfigurationProperties(prefix = "spring.datasource.prod") | |
public DataSource prodDataSource() { | |
return DataSourceBuilder.create().build(); | |
} | |
@Bean | |
@ConfigurationProperties(prefix = "spring.datasource.secondary") | |
public DataSource secondaryDataSource() { | |
return DataSourceBuilder.create().build(); | |
} | |
@Bean(name = "entityManagerFactory") | |
@Primary | |
public LocalContainerEntityManagerFactoryBean prodEntityManagerFactory( | |
EntityManagerFactoryBuilder builder) { | |
return builder | |
.dataSource(prodDataSource()) | |
.packages("com.pascaldimassimo.xyz") | |
.persistenceUnit("prod") | |
.properties(buildProperties()) | |
.build(); | |
} | |
@Bean(name = "secondaryEntityManagerFactory") | |
public LocalContainerEntityManagerFactoryBean secondaryEntityManagerFactory( | |
EntityManagerFactoryBuilder builder) { | |
return builder | |
.dataSource(secondaryDataSource()) | |
.packages("com.pascaldimassimo.xyz") | |
.persistenceUnit("secondary") | |
.properties(buildProperties()) | |
.build(); | |
} | |
private Map<String, Object> buildProperties() { | |
// This is usually set by HibernateJpaAutoConfiguration | |
Map<String, Object> properties = new HashMap<String, Object>(); | |
properties.put("hibernate.ejb.naming_strategy", | |
ImprovedNamingStrategy.class.getName()); | |
return properties; | |
} | |
@Bean | |
@Autowired | |
@Qualifier("secondaryEntityManagerFactory") | |
public PlatformTransactionManager transactionManager(EntityManagerFactory emf) { | |
JpaTransactionManager txManager = new JpaTransactionManager(); | |
txManager.setEntityManagerFactory(emf); | |
return txManager; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
@pascaldimassimo same question here