Springboot多数据源自动断开connection连接问题

单数据源配置,可以采用springboot的自动加载mybatis,

application.properties中配置如下:不需要特殊配置即可

@MapperScan("com.nox.dsp.dao")
采用这种自动加载dao层的形式

#spring.datasource.type = org.apache.commons.dbcp2.BasicDataSource
#初始化连接
#spring.datasource.dbcp2.initial-size = 10
#spring.datasource.dbcp2.max-wait-millis = 10000
#最大空闲连接
#spring.datasource.dbcp2.max-idle=20
#最小空闲连接
#spring.datasource.dbcp2.min-idle=5
#spring.datasource.dbcp2.pool-prepared-statements = true
#spring.datasource.dbcp2.max-open-prepared-statements = 20
#检测数据库的查询语句
#spring.datasource.dbcp2.validation-query = select 1 from dual
#(即在获取Connection对象时检测其可用性)  这样会影响性能,但是这个配置是最有效的。
#spring.datasource.dbcp2.test-on-borrow = true
#设置当连接被归还时,是否要提交所有还未完成的事务
#spring.datasource.dbcp2.test-on-return = false
#有些数据库连接的时候有超时限制(mysql连接在8小时后断开),或者由于网络中断等原因,连接池的连接会出现失效的情况,这时候设置一个testWhileIdle参数为true,可以保证连接池内部定时检测连接的可用性,不可用的连接会被抛弃或者重建,最大情况的保证从连接池中得到的Connection对象是可用的
#spring.datasource.dbcp2.test-while-idle = true
#每隔五分钟检测空闲超过10分钟的连接
#spring.datasource.dbcp2.min-evictable-idle-time-millis = 600000
#spring.datasource.dbcp2.time-between-eviction-runs-millis = 300000


当切换到多数据源的时候:

去掉此注解:

@MapperScan("com.nox.dsp.dao")
需要分别转装载两个数据源:具体如何加载可以参考http://blog.csdn.net/u014078154/article/details/78985102这个


#mysql dsp库
spring.datasource.dsp.url = jdbc:mysql://XXXX
spring.datasource.dsp.username = ad
spring.datasource.dsp.password = ad
spring.datasource.dsp.driver-class-name = com.mysql.jdbc.Driver
spring.datasource.dsp.max-idle = 10
spring.datasource.dsp.max-wait = 10000
spring.datasource.dsp.min-idle = 5
spring.datasource.dsp.initial-size = 5
spring.datasource.dsp.validation-query = SELECT 1
spring.datasource.dsp.test-on-borrow = false
spring.datasource.dsp.test-while-idle = true
spring.datasource.dsp.time-between-eviction-runs-millis = 18800

spring.datasource.dsp.type = com.alibaba.druid.pool.DruidDataSource

#mysql operation库
spring.datasource.operation.url = jdbc:mysql://XXX
spring.datasource.operation.username = ad
spring.datasource.operation.password = ad
spring.datasource.operation.driver-class-name = com.mysql.jdbc.Driver
spring.datasource.operation.max-idle = 10
spring.datasource.operation.max-wait = 10000
spring.datasource.operation.min-idle = 5
spring.datasource.operation.initial-size = 5
spring.datasource.operation.validation-query = SELECT 1
spring.datasource.operation.test-on-borrow = false
spring.datasource.operation.test-while-idle = true
spring.datasource.operation.time-between-eviction-runs-millis = 18800

spring.datasource.operation.type = com.alibaba.druid.pool.DruidDataSource

添加为如下数据源配置即可



你可能感兴趣的:(开发常识)