Preciso que o Spring MVC e Hibernate gerencie dois DataSources de bancos diferentes conforme o exemplo abaixo:
package br.com.centralizador.config;
import java.util.Properties;
import javax.persistence.EntityManagerFactory; import javax.sql.DataSource;
import org.springframework.context.annotation.Bean; import org.springframework.jdbc.datasource.DriverManagerDataSource; import org.springframework.orm.jpa.JpaTransactionManager; import org.springframework.orm.jpa.JpaVendorAdapter; import org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean; import org.springframework.orm.jpa.vendor.HibernateJpaVendorAdapter; import org.springframework.transaction.annotation.EnableTransactionManagement;
@EnableTransactionManagement public class ConfiguraJPA {
@Bean
public LocalContainerEntityManagerFactoryBean localContainerEntityManagerFactoryBean(DataSource dataSource, Properties properties) {
LocalContainerEntityManagerFactoryBean factoryBean = new LocalContainerEntityManagerFactoryBean();
JpaVendorAdapter adapter = new HibernateJpaVendorAdapter();
factoryBean.setJpaVendorAdapter(adapter);
factoryBean.setDataSource(dataSource);
factoryBean.setJpaProperties(properties);
factoryBean.setPackagesToScan("br.com.centralizador.models");
return factoryBean;
}
@Bean(name="dSourceMySql")
private DataSource dataSourceMySql() {
DriverManagerDataSource dataSource = new DriverManagerDataSource();
dataSource.setDriverClassName("com.mysql.cj.jdbc.Driver");
dataSource.setUrl("jdbc:mysql://localhost:3306/centralizador?useTimezone=true&serverTimezone=UTC");
dataSource.setUsername("user");
dataSource.setPassword("xxxxxxxx");
return dataSource;
}
@Bean(name="dSourceInformix")
private DataSource dataSourceInformix() {
DriverManagerDataSource dataSource = new DriverManagerDataSource();
dataSource.setDriverClassName("com.informix.jdbc.IfxDriver");
dataSource.setUrl("jdbc:informix-sqli://192.168.1.1:1526;InformixServer=call");
dataSource.setUsername("user");
dataSource.setPassword("xxxxxxx");
return dataSource;
}
@Bean
public Properties propertiesMysql() {
Properties properties = new Properties();
properties.setProperty("hibernate.dialect", "org.hibernate.dialect.MySQL5Dialect");
properties.setProperty("hibernate.show_sql", "true");
properties.setProperty("hibernate.hbm2ddl.auto", "update");
return properties;
}
@Bean
public Properties propertiesInformix() {
Properties properties = new Properties();
properties.setProperty("hibernate.dialect", "org.hibernate.dialect.InformixDialect");
properties.setProperty("hibernate.show_sql", "true");
properties.setProperty("hibernate.hbm2ddl.auto", "validate");
return properties;
}
@Bean
public JpaTransactionManager transactionManager(EntityManagerFactory emf) {
return new JpaTransactionManager(emf);
}
}