spring boot jdbc 链接超时

本文解决了一个关于Spring Boot结合jdbcTemplate使用过程中遇到的数据库连接超时问题。通过配置tomcat连接池参数如最大等待时间、最大活跃连接数及借连接前验证等,确保了应用稳定运行。

使用springboot +jdbcTemplate 时运行一段时间后会报错!

com.mysql.jdbc.exceptions.jdbc4.CommunicationsException: The last packet successfully received from the server was 79,870,633 milliseconds ago.

配置一般的链接pool是无效的,使用官网文档中的jdbc-tomcat 连接pool配置

file table (DataSourceAutoConfiguration & DataSourceProperties) 数据源1

spring.datasource.primary.url=jdbc:mysql://192.168.0.26:3306/yliyun?autoReconnect=true spring.datasource.primary.username=root spring.datasource.primary.password=yliyun123@ spring.datasource.primary.driver-class-name=com.mysql.jdbc.Driver spring.datasource.primary.continue-on-error=false

log table (for log to index); 数据源2

spring.datasource.secondary.url=jdbc:mysql://192.168.0.26:3306/log?autoReconnect=true spring.datasource.secondary.username=root spring.datasource.secondary.password=yliyun123@ spring.datasource.secondary.driver-class-name=com.mysql.jdbc.Driver spring.datasource.secondary.continue-on-error=false

connect pool;

Number of ms to wait before throwing an exception if no connection is available.

spring.datasource.tomcat.max-wait=10000

Maximum number of active connections that can be allocated from this pool at the same time.

spring.datasource.tomcat.max-active=50

Validate the connection before borrowing it from the pool.

spring.datasource.tomcat.test-on-borrow=true

运行后即可

转载于:https://my.oschina.net/jiuyuehe/blog/779002

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值