Skip to content

Instantly share code, notes, and snippets.

@russellhoff
Last active May 31, 2018 10:38
Show Gist options
  • Save russellhoff/e0fe3ec00d11b9af4d58bcdb5322f9db to your computer and use it in GitHub Desktop.
Save russellhoff/e0fe3ec00d11b9af4d58bcdb5322f9db to your computer and use it in GitHub Desktop.
Multiple datasources and their configuration in Spring Boot
package com.ingartek.transporte.estimat.configuration;
import java.util.HashMap;
import java.util.Map;
import javax.sql.DataSource;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.autoconfigure.domain.EntityScan;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.core.env.Environment;
import org.springframework.data.jpa.repository.config.EnableJpaRepositories;
import org.springframework.jdbc.datasource.DriverManagerDataSource;
import org.springframework.orm.jpa.JpaTransactionManager;
import org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean;
import org.springframework.orm.jpa.vendor.HibernateJpaVendorAdapter;
import org.springframework.transaction.PlatformTransactionManager;
import org.springframework.transaction.annotation.EnableTransactionManagement;
import com.ingartek.transporte.estimat.destino_datos.model.MovimientoTarjetaConsolidado;
@Configuration
@EntityScan("com.ingartek.transporte.estimat.destino_datos.model")
@EnableJpaRepositories(
basePackages = {
"com.ingartek.transporte.estimat.destino_datos.dao"
},
entityManagerFactoryRef = "secondaryEntityManagerFactory",
transactionManagerRef = "secondaryTransactionManager"
)
@EnableTransactionManagement
public class DestinoDatosConfig {
/*
* Atributos
*/
private final static Logger logger = LoggerFactory.getLogger(DestinoDatosConfig.class);
@Autowired
private Environment env;
/*
* Métodos
*/
public DestinoDatosConfig(){
logger.debug("new DestinoDatosConfig()");
}
@Bean("secondaryTransactionManager")
public PlatformTransactionManager secondaryTransactionManager() {
return new JpaTransactionManager(secondaryEntityManagerFactory().getObject());
}
@Bean("secondaryEntityManagerFactory")
public LocalContainerEntityManagerFactoryBean secondaryEntityManagerFactory(){
Map<String, String> jpaProperties = new HashMap<String, String>();
jpaProperties.put("hibernate.ddl-auto", env.getProperty("dssecondary.spring.jpa.properties.hibernate.ddl-auto"));
jpaProperties.put("hibernate.dialect", env.getProperty("dssecondary.spring.jpa.properties.hibernate.dialect"));
HibernateJpaVendorAdapter jpaVendorAdapter = new HibernateJpaVendorAdapter();
jpaVendorAdapter.setGenerateDdl(true);
LocalContainerEntityManagerFactoryBean factoryBean = new LocalContainerEntityManagerFactoryBean();
factoryBean.setJpaPropertyMap(jpaProperties);
factoryBean.setDataSource(secondaryDataSource());
factoryBean.setJpaVendorAdapter(jpaVendorAdapter);
factoryBean.setPackagesToScan(MovimientoTarjetaConsolidado.class.getPackage().getName());
return factoryBean;
}
@Bean("secondaryDataSource")
public DataSource secondaryDataSource(){
DriverManagerDataSource ds = new DriverManagerDataSource();
ds.setDriverClassName(env.getProperty("dssecondary.spring.datasource.driver-class-name"));
ds.setUrl(env.getProperty("dssecondary.spring.datasource.url"));
ds.setUsername(env.getProperty("dssecondary.spring.datasource.username"));
ds.setPassword(env.getProperty("dssecondary.spring.datasource.password"));
return ds;
}
}
package com.ingartek.transporte.estimat.configuration;
import java.util.HashMap;
import java.util.Map;
import javax.sql.DataSource;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.autoconfigure.domain.EntityScan;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.Primary;
import org.springframework.core.env.Environment;
import org.springframework.data.jpa.repository.config.EnableJpaRepositories;
import org.springframework.jdbc.datasource.DriverManagerDataSource;
import org.springframework.orm.jpa.JpaTransactionManager;
import org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean;
import org.springframework.orm.jpa.vendor.HibernateJpaVendorAdapter;
import org.springframework.transaction.PlatformTransactionManager;
import org.springframework.transaction.annotation.EnableTransactionManagement;
import com.ingartek.transporte.estimat.origen_datos.model.MovimientoTarjeta;
@Configuration
@EntityScan("com.ingartek.transporte.estimat.origen_datos.model")
@EnableJpaRepositories(
basePackages = { "com.ingartek.transporte.estimat.origen_datos.dao" },
entityManagerFactoryRef = "primaryEntityManagerFactory",
transactionManagerRef = "primaryTransactionManager"
)
@EnableTransactionManagement
public class OrigenDatosConfig {
/*
* Atributos
*/
private final static Logger logger = LoggerFactory.getLogger(OrigenDatosConfig.class);
@Autowired
private Environment env;
/*
* Métodos
*/
public OrigenDatosConfig(){
logger.debug("new OrigenDatosConfig()");
}
@Bean("primaryTransactionManager")
@Primary
public PlatformTransactionManager primaryTransactionManager() {
return new JpaTransactionManager(primaryEntityManagerFactory().getObject());
}
@Bean("primaryEntityManagerFactory")
@Primary
public LocalContainerEntityManagerFactoryBean primaryEntityManagerFactory(){
Map<String, String> jpaProperties = new HashMap<String, String>();
jpaProperties.put("hibernate.ddl-auto", env.getProperty("dsprimary.spring.jpa.properties.hibernate.ddl-auto"));
jpaProperties.put("hibernate.dialect", env.getProperty("dsprimary.spring.jpa.properties.hibernate.dialect"));
HibernateJpaVendorAdapter jpaVendorAdapter = new HibernateJpaVendorAdapter();
jpaVendorAdapter.setGenerateDdl(false);
LocalContainerEntityManagerFactoryBean factoryBean = new LocalContainerEntityManagerFactoryBean();
factoryBean.setJpaPropertyMap(jpaProperties);
factoryBean.setDataSource(primaryDataSource());
factoryBean.setJpaVendorAdapter(jpaVendorAdapter);
factoryBean.setPackagesToScan(MovimientoTarjeta.class.getPackage().getName());
return factoryBean;
}
@Bean("primaryDataSource")
@Primary
public DataSource primaryDataSource(){
DriverManagerDataSource ds = new DriverManagerDataSource();
ds.setDriverClassName(env.getProperty("dsprimary.spring.datasource.driver-class-name"));
ds.setUrl(env.getProperty("dsprimary.spring.datasource.url"));
ds.setUsername(env.getProperty("dsprimary.spring.datasource.username"));
ds.setPassword(env.getProperty("dsprimary.spring.datasource.password"));
return ds;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment