//第一数据源
spring.datasource.default.driver-class-name=com.mysql.jdbc.Driver
spring.datasource.default.url=jdbc:mysql://10.18.100.X:3306/hzz?useUnicode=true&autoReconnect=true&failOverReadOnly=false&characterEncoding=utf8
spring.datasource.default.username=xxx
spring.datasource.default.password=xxxx
//第二数据源,注意 要用 /
spring.datasource.secondary.url=jdbc:oracle:thin:@X.X.X.X:1521/hzzdb
spring.datasource.secondary.username=wrgis
spring.datasource.secondary.password=wrgis_123
spring.datasource.secondary.driver-class-name=oracle.jdbc.driver.OracleDriver
# 下面为连接池的补充设置,应用到上面所有数据源中
#自动提交
spring.datasource.tomcat.default-auto-commit=true
spring.datasource.hikari.maximum-pool-size=100
spring.datasource.tomcat.max-idle=10
spring.datasource.tomcat.min-idle=5
spring.datasource.tomcat.initial-size=5
spring.datasource.tomcat.max-wait=10000
spring.datasource.tomcat.test-on-borrow=true
spring.datasource.tomcat.validation-query=SELECT 1
package com.gsww.hzz;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.boot.autoconfigure.jdbc.DataSourceBuilder;
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.context.annotation.PropertySource;
import org.springframework.jdbc.core.JdbcTemplate;
import javax.sql.DataSource;
/**
* @author 强周亮
* @desc 数据源配置类
* @email [email protected]
* @time 2019-07-09 19:47
* @class DataSourceConfig
* @package com.gsww.hzz
*/
@Configuration
@PropertySource("classpath:application.properties")
public class DataSourceConfig {
private Logger logger = LoggerFactory.getLogger(DataSourceConfig.class);
/**
* @Description: 创建第一个数据库连接池
* @Parameters: DataSourceConfig
*/
@Bean(name = "defaultDataSource")
@Primary
@Qualifier("defaultDataSource")
@ConfigurationProperties(prefix="spring.datasource.default" )
public DataSource detaultDataSource() {
logger.info("第一个数据库连接池创建中.......");
return DataSourceBuilder.create().build();
}
/**
* @Description: 创建第二个数据库连接池
* @Parameters: DataSourceConfig
*/
@Bean(name = "secondaryDataSource")
@Qualifier("secondaryDataSource")
@ConfigurationProperties(prefix="spring.datasource.secondary")
public DataSource secondaryDataSource() {
logger.info("第二个数据库连接池创建中......");
return DataSourceBuilder.create().build();
}
//配置jdbc
@Bean(name = "defaultJdbcTemplate")
public JdbcTemplate defaultJdbcTemplate(
@Qualifier("defaultDataSource") DataSource dataSource) {
return new JdbcTemplate(dataSource);
}
@Bean(name = "secondaryJdbcTemplate")
public JdbcTemplate secondaryJdbcTemplate(
@Qualifier("secondaryDataSource") DataSource dataSource) {
return new JdbcTemplate(dataSource);
}
}
package com.gsww.hzz;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.boot.autoconfigure.orm.jpa.JpaProperties;
import org.springframework.boot.orm.jpa.EntityManagerFactoryBuilder;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.Primary;
import org.springframework.data.jpa.repository.config.EnableJpaRepositories;
import org.springframework.orm.jpa.JpaTransactionManager;
import org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean;
import org.springframework.transaction.PlatformTransactionManager;
import org.springframework.transaction.annotation.EnableTransactionManagement;
import javax.persistence.EntityManager;
import javax.sql.DataSource;
import java.util.Map;
/**
* @author 强周亮
* @desc 第一个数据源的配置类
* @email [email protected]
* @time 2019-07-09 19:48
* @class DefaultDataSouceConfig
* @package com.gsww.hzz
*/
@Configuration
@EnableTransactionManagement
@EnableJpaRepositories(
entityManagerFactoryRef="entityManagerFactoryPrimary",
transactionManagerRef="transactionManagerPrimary",
basePackages= { "com.gsww.hzz.dao" }) //设置Repository所在位置,jpa dao层
public class DefaultDataSouceConfig {
@Autowired @Qualifier("defaultDataSource")
private DataSource primaryDataSource;
@Primary
@Bean(name = "entityManagerPrimary")
public EntityManager entityManager(EntityManagerFactoryBuilder builder) {
return entityManagerFactoryPrimary(builder).getObject().createEntityManager();
}
@Primary
@Bean(name = "entityManagerFactoryPrimary")
public LocalContainerEntityManagerFactoryBean entityManagerFactoryPrimary (EntityManagerFactoryBuilder builder) {
return builder
.dataSource(primaryDataSource)
.properties(getVendorProperties(primaryDataSource))
.packages("com.gsww.hzz.entity") //设置实体类所在位置
.persistenceUnit("primaryPersistenceUnit")
.build();
}
@Autowired
private JpaProperties jpaProperties;
private Map getVendorProperties(DataSource dataSource) {
return jpaProperties.getHibernateProperties(dataSource);
}
@Primary
@Bean(name = "transactionManagerPrimary")
public PlatformTransactionManager transactionManagerPrimary(EntityManagerFactoryBuilder builder) {
return new JpaTransactionManager(entityManagerFactoryPrimary(builder).getObject());
}
}
绑定数据源
@Autowired
@Qualifier("defaultJdbcTemplate")
private JdbcTemplate jdbcTemplate;
c3p0
c3p0
0.9.1.2
com.oracle
ojdbc6
11.2.0.3
mysql
mysql-connector-java
6.0.6
注意:mysql和oracle的连接池配置有差异,得单独设置
#第二数据源
spring.datasource.c3p0.secondary.jdbcUrl=jdbc:oracle:thin:@127.0.0.1:1521/hzzdb
spring.datasource.c3p0.secondary.user=user
spring.datasource.c3p0.secondary.password=pass
spring.datasource.c3p0.secondary.driverClass=oracle.jdbc.driver.OracleDriver
spring.datasource.c3p0.default.driverClass=com.mysql.jdbc.Driver
spring.datasource.c3p0.default.jdbcUrl=jdbc:mysql://127.0.0.1:3306/hzz?useUnicode=true&autoReconnect=true&failOverReadOnly=false&useSSL=false&characterEncoding=utf8
spring.datasource.c3p0.default.user=user
spring.datasource.c3p0.default.password=pass
#默认为3,表示连接池中任何时候可以存放的连接最小数量。
spring.datasource.c3p0.default.minPoolSize=10
#默认为15,表示连接池中任何时候可以存放的连接最大数量。
spring.datasource.c3p0.default.maxPoolSize=50
#默认为0,单位为秒,表示在连接池中未被使用的连接最长存活多久不被移除。
spring.datasource.c3p0.default.maxIdleTime=300
#默认值为3,表示当C3P0数据源中的连接池耗尽时,一次同时获取的连接数。
spring.datasource.c3p0.default.acquireIncrement=5
#最大sql语句
spring.datasource.c3p0.default.maxStatements=200
#默认为3,表示初始化连接池时获取的连接个数。该数值在miniPoolSize和maxPoolSize之间。
spring.datasource.c3p0.default.initialPoolSize=5
#连接池在获得新连接失败时重试的次数,如果小于等于0则无限重试直至连接获得成功
spring.datasource.c3p0.default.acquireRetryAttempts=30
spring.datasource.c3p0.default.acquireRetryDelay=1000
#如果为true,则当连接获取失败时自动关闭数据源,除非重新启动应用程序
spring.datasource.c3p0.default.breakAfterAcquireFailure=false
#默认为false,设置为true,表示从连接池中取出连接时测试连接的有效性,因为此操作会降低性 能,建议使用idleConnectionTestPeriod或者automaticTestTable来提升连接测试的性能。
spring.datasource.c3p0.default.testConnectionOnCheckout=true
spring.datasource.c3p0.default.testConnectionOnCheckin=true
spring.datasource.c3p0.default.preferredTestQuery=SELECT 1
#spring.datasource.c3p0.default.automaticTestTable=app_version
#如果这是一个大于0的数字,c3p0将每隔这几秒测试所有空闲,池化但未经检查的连接
spring.datasource.c3p0.default.idleConnectionTestPeriod=60
#配置当连接池所有连接用完时应用程序getConnection的等待时间。为0则无限等待直至有其他连接释放或者创建新的连接,
spring.datasource.c3p0.default.checkoutTimeout=100
#可同时进行的线程数
spring.datasource.c3p0.default.numHelperThreads=10
#oracle的配置
#默认为3,表示连接池中任何时候可以存放的连接最小数量。
spring.datasource.c3p0.secondary.minPoolSize=2
#默认为15,表示连接池中任何时候可以存放的连接最大数量。
spring.datasource.c3p0.secondary.maxPoolSize=50
#默认为0,单位为秒,表示在连接池中未被使用的连接最长存活多久不被移除。
spring.datasource.c3p0.secondary.maxIdleTime=30
#默认值为3,表示当C3P0数据源中的连接池耗尽时,一次同时获取的连接数。
spring.datasource.c3p0.secondary.acquireIncrement=5
#默认为3,表示初始化连接池时获取的连接个数。该数值在miniPoolSize和maxPoolSize之间。
spring.datasource.c3p0.secondary.initialPoolSize=5
package com.gsww.hzz.config;
import com.mchange.v2.c3p0.ComboPooledDataSource;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.boot.autoconfigure.jdbc.DataSourceBuilder;
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.context.annotation.PropertySource;
import org.springframework.jdbc.core.JdbcTemplate;
import javax.sql.DataSource;
/**
* @author 强周亮
* @desc 数据源配置类
* @email [email protected]
* @time 2019-07-09 19:47
* @class DataSourceConfig
* @package com.gsww.hzz
*/
@Configuration
@PropertySource("classpath:application.properties")
public class DataSourceConfig {
private Logger logger = LoggerFactory.getLogger(DataSourceConfig.class);
/**
* @Description: 创建第一个数据库连接池
* @Parameters: DataSourceConfig
*/
@Bean(name = "defaultDataSource")
@Primary
@Qualifier("defaultDataSource")
@ConfigurationProperties(prefix="spring.datasource.c3p0.default" )
public DataSource detaultDataSource() {
logger.info("第一个数据库连接池创建中.......");
return DataSourceBuilder.create().type(ComboPooledDataSource.class).build();
}
/**
* @Description: 创建第二个数据库连接池
* @Parameters: DataSourceConfig
*/
@Bean(name = "secondaryDataSource")
@Qualifier("secondaryDataSource")
@ConfigurationProperties(prefix="spring.datasource.c3p0.secondary")
public DataSource secondaryDataSource() {
logger.info("第二个数据库连接池创建中......");
return DataSourceBuilder.create().type(ComboPooledDataSource.class).build();
}
//配置jdbc
@Bean(name = "defaultJdbcTemplate")
public JdbcTemplate defaultJdbcTemplate(
@Qualifier("defaultDataSource") DataSource dataSource) {
return new JdbcTemplate(dataSource);
}
@Bean(name = "secondaryJdbcTemplate")
public JdbcTemplate secondaryJdbcTemplate(
@Qualifier("secondaryDataSource") DataSource dataSource) {
return new JdbcTemplate(dataSource);
}
}
配置实体和使用同上2.3,2.4
解决方法:在数据库url上配这两个参数:mysql5.0以前可以
autoReconnect=true&failOverReadOnly=false
spring.datasource.default.url=jdbc:mysql://10.10.10.10:3306/hzz?useUnicode=true&autoReconnect=true&failOverReadOnly=false&characterEncoding=utf8