MySQL连接池配置——超时异常和处理

前言

MySQL版本:MySQL 8.0.15

数据库连接驱动版本:mysql-connector-java-8.0.15.jar

连接池版本:c3p0-0.9.5.2.jar

连接配置

#数据库驱动
jdbc.driver=com.mysql.cj.jdbc.Driver
#数据库连接
jdbc.url=jdbc:mysql://localhost:3306/miniss?characterEncoding=utf8&amp&serverTimezone=UTC&amp&autoReconnect=true
#用户名
jdbc.username=root
#用户密码
jdbc.password=123456

异常错误

Cause: com.mysql.cj.jdbc.exceptions.CommunicationsException: The last packet successfully received from the server was 68,560,034 milliseconds ago.  The last packet sent successfully to the server was 68,5
60,034 milliseconds ago. is longer than the server configured value of 'wait_timeout'. You should consider either expiring and/or testing connection validity before use in your application, increasing the serv
er configured values for client timeouts, or using the Connector/J connection property 'autoReconnect=true' to avoid this problem.
; SQL []; The last packet successfully received from the server was 68,560,034 milliseconds ago.  The last packet sent successfully to the server was 68,560,034 milliseconds ago. is longer than the server conf
igured value of 'wait_timeout'. You should consider either expiring and/or testing connection validity before use in your application, increasing the server configured values for client timeouts, or using the 
Connector/J connection property 'autoReconnect=true' to avoid this problem.; nested exception is com.mysql.cj.jdbc.exceptions.CommunicationsException: The last packet successfully received from the server was 
68,560,034 milliseconds ago.  The last packet sent successfully to the server was 68,560,034 milliseconds ago. is longer than the server configured value of 'wait_timeout'. You should consider either expiring 
and/or testing connection validity before use in your application, increasing the server configured values for client timeouts, or using the Connector/J connection property 'autoReconnect=true' to avoid this p
roblem.
        at org.springframework.jdbc.support.SQLExceptionSubclassTranslator.doTranslate(SQLExceptionSubclassTranslator.java:98)
        at org.springframework.jdbc.support.AbstractFallbackSQLExceptionTranslator.translate(AbstractFallbackSQLExceptionTranslator.java:73)
        at org.springframework.jdbc.support.AbstractFallbackSQLExceptionTranslator.translate(AbstractFallbackSQLExceptionTranslator.java:81)
        at org.mybatis.spring.MyBatisExceptionTranslator.translateExceptionIfPossible(MyBatisExceptionTranslator.java:73)
        at org.mybatis.spring.SqlSessionTemplate$SqlSessionInterceptor.invoke(SqlSessionTemplate.java:446)
        at com.sun.proxy.$Proxy19.selectOne(Unknown Source)
        at org.mybatis.spring.SqlSessionTemplate.selectOne(SqlSessionTemplate.java:166)
        at org.apache.ibatis.binding.MapperMethod.execute(MapperMethod.java:82)
        at org.apache.ibatis.binding.MapperProxy.invoke(MapperProxy.java:59)
        at com.sun.proxy.$Proxy20.adminLogin(Unknown Source)
        at com
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
下面是一个常用的 MySQL 连接池的推荐配置: ``` # 初始化连接数 initialSize=10 # 最大连接数 maxActive=100 # 最大空闲连接数 maxIdle=50 # 最小空闲连接数 minIdle=10 # 获取连接时的最大等待时间,单位毫秒。超过该时间将抛出异常 maxWait=5000 # 检查连接是否有效的 SQL 语句,用于保证连接池中的连接都是有效的 validationQuery=SELECT 1 # 检查连接是否有效的超时时间,单位秒 validationQueryTimeout=10 # 当连接池中的连接耗尽时,是否阻塞等待 blockWhenExhausted=true # 是否在连接池空闲时检查连接的有效性 testWhileIdle=true # 是否在从连接池中获取连接时检查连接的有效性 testOnBorrow=false # 是否在归还连接到连接池时检查连接的有效性 testOnReturn=false # 连接池中连接空闲时的最大存活时间,单位毫秒 maxIdleTime=60000 # 连接池中连接空闲时的最大存活时间,单位毫秒 maxEvictableIdleTimeMillis=1200000 # 定时清理无效连接的时间周期,单位毫秒。如果小于等于 0,则禁用清理功能 timeBetweenEvictionRunsMillis=60000 # 是否开启连接泄露检测。如果开启,当连接被应用程序泄露时会打印警告信息 removeAbandoned=false # 连接被泄露的超时时间,单位秒。如果 removeAbandoned 为 true,则应用程序获取连接后,超过该时间未归还连接,会被认为是泄露的连接 removeAbandonedTimeout=300 # 是否在发现泄露的连接时打印堆栈跟踪信息 logAbandoned=false ``` 需要注意的是,这个配置并不是适用于所有场景的,实际使用时还需要结合具体的业务场景和系统资源情况进行调整。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值