849fca2a939dac61e95fa6f2295b43d8.png

**业务需求:**需要从其他数据库中同步业务数据,以支持当前系统的数据需要

**技术栈:**spring boot2.0框架 jpa mysql

**问题描述:**第一次尝试多数据源的开发,在做定时任务同步数据时发现经常报错,错误信息在上面图片中,有的人说是连接池不够用了,有的说是配置问题,有的说是MySQL中有个wait time_out的配置8小时时间太短了

于是我就一个一个去尝试

1、连接池的问题,同步时需要处理一万多数据,我在service层加了事务控制,@Transactional,他 们说可能是这个问题导致的,我将注解去掉,在运行发现还是不行

2、有人说是我的配置文件写的不对,我按照他们的说法,将配置文件修改,发现问题还是存在

原来的配置:

spring:

datasource:

#本地数据库

primary:

driver-class-name: com.mysql.jdbc.Driver

jdbc-url: jdbc:mysql://###/###?characterEncoding=utf8&useSSL=false

username: ###

password: ###

#datasource:

#外部数据库

secondary:

driver-class-name: com.mysql.jdbc.Driver

jdbc-url: jdbc:mysql://###/###?characterEncoding=utf8&useSSL=false

username: ###

password: ###

jpa:

show-sql: true

hibernate:

ddl-auto: update

#strategy: org.hibernate.cfg.DefaultNamingStrategy

naming:

implicit-strategy: org.springframework.boot.orm.jpa.hibernate.SpringImplicitNamingStrategy

properties:

hibernate:

dialect: org.hibernate.dialect.MySQL5Dialect

修改后的配置

jpa:

primary-dialect: org.hibernate.dialect.MySQL5Dialect

secondary-dialect: org.hibernate.dialect.MySQL5Dialect

show-sql: true

hibernate:

ddl-auto: update

#strategy: org.hibernate.cfg.DefaultNamingStrategy

naming:

implicit-strategy: org.springframework.boot.orm.jpa.hibernate.SpringImplicitNamingStrategy

其他未变

运行后发现依然没解决问题

3、对于time_out的方案,我们一开始并没有按照网上说的改为最大值,而是改为一分钟,验证一下是不是长时间不连接导致的断开,修改一分半钟后发现问题没有显现(说明一下,报错不是每时每刻发生,是系统重启后一段时间没事,过上一段时间开始报错,并且不会变好),说明不是wait time_out的问题,

有意思的是我改短以后就没在报过错,于是我又改回28800,过上一段时间后就报错了,于是现在将其改为了300,好几天了,没在报错

至今还未找到错误原因,找到问题以及解决方案会持续更新

Logo

一站式 AI 云服务平台

更多推荐