Skip to content

Instantly share code, notes, and snippets.

@pascaldimassimo
Last active November 26, 2020 15:41
Show Gist options
  • Save pascaldimassimo/cacad7ae4d41354f7337 to your computer and use it in GitHub Desktop.
Save pascaldimassimo/cacad7ae4d41354f7337 to your computer and use it in GitHub Desktop.
Spring Boot config for multiple datasources and EntityManagerFactories
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;
}
}
@asif31iqbal
Copy link

@pascaldimassimo How do you configure multiple data sources using entity scan? And how do you make the secondary data source work with the appropriate repository?

@kopax
Copy link

kopax commented Nov 3, 2017

@pascaldimassimo same question here

@24kpure
Copy link

24kpure commented Jan 10, 2018

@pascaldimassimo same question here

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment