最近工作中碰到一个问题,springboot 配置了mysql多数据源,以前一直单线程运行,没有任务问题。最近需要多线程运行,然后一直出现下面的问题:
HikariPool-1 - Connection is not available, request timed out after 30033ms.
springboot 2.0之后,默认数据库连接池为 HikariPool,其详细属性见下文
网上查了很多资料,但是很多都没有解决。历经千辛万苦,最终找到了解决方案,如下:
修改前:
spring:
datasource:
datasource1:
driver-class-name: com.mysql.cj.jdbc.Driver
jdbc-url: jdbc:mysql://url:端口号/数据库名字?useunicode=true&characterEncoding=utf8&serverTimezone=UTC
username: root
password: root
datasource2:
driver-class-name: com.mysql.cj.jdbc.Driver
jdbc-url: jdbc:mysql://url:端口号/数据库名字?useunicode=true&characterEncoding=utf8&serverTimezone=UTC
username: root
password: root
修改后:
spring:
datasource:
hikari:
maximum-pool-size: 60
data-source-properties:
setIdleTimeout: 60000
setConnectionTimeout: 60000
setValidationTimeout: 3000
setLoginTimeout: 5
setMaxLifetime: 60000
datasource1:
driver-class-name: com.mysql.cj.jdbc.Driver
jdbc-url: jdbc:mysql://url:端口号/数据库名字?useunicode=true&characterEncoding=utf8&serverTimezone=UTC
username: root
password: root
datasource2:
driver-class-name: com.mysql.cj.jdbc.Driver
jdbc-url: jdbc:mysql://url:端口号/数据库名字?useunicode=true&characterEncoding=utf8&serverTimezone=UTC
username: root
password: root