1、application.properties中配置多数据源
# qss数据库
datasource.qss.driver-class-name=com.mysql.jdbc.Driver
datasource.qss.url=jdbc:mysql://ip:3306/qssdb?autoReconnectForPools=true&useUnicode=true&characterEncoding=utf8&autoReconnect=true&failOverReadOnly=false
datasource.qss.username=***
datasource.qss.password=***
# foc测试数据库
datasource.foc.driver-class-name=oracle.jdbc.driver.OracleDriver
datasource.foc.url=jdbc:oracle:thin:@ip:ora817
datasource.foc.username=***
datasource.foc.password=***
2.application.properties中 配置主数据源MySQL连接池(一定要注意连接池配置的前缀,此处用于配置qss数据源)
#参考:http://blog.csdn.net/catoop/article/details/50507516
#我们使用Tomcat数据源连接池,需要依赖tomcat-jdbc,只要应用中添加了spring-boot-starter-jdbc 或 spring-boot-starter-data-jpa依赖,则无需担心这点,因为将会自动添加 tomcat-jdbc 依赖。
#假如我们想用其他方式的连接池技术,只要配置自己的DataSource bean,即可覆盖Spring Boot的自动配置。
datasource.qss.max-idle=10
# Number of ms to wait before throwing an exception if no connection is available.
datasource.qss.max-wait=10000
datasource.qss.min-idle=5
datasource.qss.initial-size=5
# Maximum number of active connections that can be allocated from this pool at the same time.
datasource.qss.max-active=100
datasource.qss.validation-query=SELECT 1
#使用testOnBorrow为true(即在获取Connection对象时检测其可用性),不过这样会影响性能
# Validate the connection before borrowing it from the pool.
datasource.qss.test-on-borrow=true
#有些数据库连接的时候有超时限制(mysql连接在8小时后断开),或者由于网络中断等原因,连接池的连接会出现失效的情况,这时候设置一个testWhileIdle参数为true,可以保证连接池内部定时检测连接的可用性,不可用的连接会被抛弃或者重建,最大情况的保证从连接池中得到的Connection对象是可用的。
datasource.qss.test-while-idle=true
#设置当连接被归还时,是否要提交所有还未完成的事务
datasource.qss.test-on-return=false
#如果当前连接池中某个连接在空闲了timeBetweenEvictionRunsMillis时间后任然没有使用,则被物理性的关闭掉。
datasource.qss.time-between-eviction-runs-millis=60000
# 配置一个连接在池中最小生存的时间,单位是毫秒
datasource.qss.min-evictable-idle-time-millis=300000
datasource.qss.jdbc-interceptors=ConnectionState;SlowQueryReport(threshold=0)
3. 配置其他数据源连接池(有多少个配置多少个)
datasource.foc.max-idle=10
datasource.foc.max-wait=10000
datasource.foc.min-idle=5
datasource.foc.initial-size=5
datasource.foc.max-active=100
#SELECT 1(MySQL) select 1 from dual(Oracle) SELECT 1(MySQL Server)。
datasource.foc.validation-query=select 1 from dual
datasource.foc.test-on-borrow=true
datasource.foc.test-while-idle=true
datasource.foc.test-on-return=false
datasource.foc.time-between-eviction-runs-millis=60000
datasource.foc.min-evictable-idle-time-millis=300000
datasource.foc.jdbc-interceptors=ConnectionState;SlowQueryReport(threshold=0)
4. DataSource Bean 主数据源(主数据源添加@Primary注解)
@Configuration
@EnableTransactionManagement
@EnableJpaRepositories(basePackages = {"cn.com.scal.qss.dao"}, entityManagerFactoryRef = "qssEntityManagerFactory", transactionManagerRef="qssTransactionManager") //
// basePackages设置Repository所在位置
@ComponentScan
@PropertySource("classpath:application.properties")
public class QssRepositoryConfig {
@Bean(name = "qssDataSource")
@Qualifier("qssDataSource")
@Primary
@ConfigurationProperties(prefix="datasource.qss")
public DataSource qssDataSource() {
return DataSourceBuilder.create().build();
}
@Bean(name = "qssEntityManagerFactory")
@Primary
public EntityManagerFactory qssEntityManagerFactory(DataSource qssDataSource) {
LocalContainerEntityManagerFactoryBean factory = new LocalContainerEntityManagerFactoryBean();
factory.setDataSource(qssDataSource);
factory.setJpaVendorAdapter(new HibernateJpaVendorAdapter());
factory.setJpaProperties(additionalJpaProperties());
factory.setPersistenceUnitName("qssPersistent");
factory.setPackagesToScan("cn.com.scal.qss.domain"); //设置实体类所在位置
factory.afterPropertiesSet();
return factory.getObject();
}
@Bean(name = "qssTransactionManager")
@Primary
public PlatformTransactionManager qssTransactionManager(EntityManagerFactory qssEntityManagerFactory) {
JpaTransactionManager transactionManager = new JpaTransactionManager();
transactionManager.setEntityManagerFactory(qssEntityManagerFactory);
return transactionManager;
}
public Properties additionalJpaProperties(){
Properties properties = new Properties();
properties.setProperty("hibernate.hbm2ddl.auto", "update");
properties.setProperty("hibernate.dialect", "org.hibernate.dialect.MySQL5Dialect");
properties.setProperty("hibernate.show_sql", "true");
properties.setProperty("hibernate.format_sql", "true");
return properties;
}
}
5. DataSource Bean 其他数据源
@Configuration
@EnableTransactionManagement // 只涉及查询,暂不需要事务管理
@EnableJpaRepositories(basePackages = {"cn.com.scal.qss.foc.dao"}, entityManagerFactoryRef = "focEntityManagerFactory" , transactionManagerRef="focTransactionManager")
@ComponentScan
@PropertySource("classpath:application.properties")
public class FocRepositoryConfig {
@Bean(name = "focDataSource")
@Qualifier("focDataSource")
@ConfigurationProperties(prefix="datasource.foc")
public DataSource focDataSource() {
return DataSourceBuilder.create().build();
}
@Bean(name = "focEntityManagerFactory")
@Qualifier("focEntityManagerFactory")
public EntityManagerFactory focEntityManagerFactory(@Qualifier("focDataSource") DataSource focDataSource) {
LocalContainerEntityManagerFactoryBean factory = new LocalContainerEntityManagerFactoryBean();
factory.setDataSource(focDataSource);
factory.setPersistenceProviderClass(HibernatePersistenceProvider.class);
factory.setJpaVendorAdapter(new HibernateJpaVendorAdapter());
factory.setJpaProperties(additionalJpaProperties());
factory.setPersistenceUnitName("focPersistent");
factory.setPackagesToScan("cn.com.scal.qss.foc.domain");
factory.afterPropertiesSet();
return factory.getObject();
}
@Bean(name = "focTransactionManager")
@Qualifier("focTransactionManager")
public PlatformTransactionManager focTransactionManager(@Qualifier("focEntityManagerFactory") EntityManagerFactory focEntityManagerFactory) {
JpaTransactionManager transactionManager = new JpaTransactionManager();
transactionManager.setEntityManagerFactory(focEntityManagerFactory);
// JpaTransactionManager transactionManager = new JpaTransactionManager(focEntityManagerFactory);
return transactionManager;
}
public Properties additionalJpaProperties(){
Properties properties = new Properties();
// properties.setProperty("hibernate.hbm2ddl.auto", "update");
properties.setProperty("hibernate.dialect", "org.hibernate.dialect.Oracle10gDialect");
properties.setProperty("hibernate.show_sql", "true");
properties.setProperty("hibernate.format_sql", "true");
return properties;
}
}
6. finally it works!!!
注意事项:
1、每个数据源都必须要有自己的数据库连接池配置,使用前缀区分;
2、数据库连接验证sql查询语句validation-query区分数据库
#SELECT 1(MySQL) select 1 from dual(Oracle) SELECT 1(MySQL Server)。
3、EntityManager不能配置在配置文件中(单例),不许要在使用的时候才创建,使用完成后关闭。