hikari配置max-lifetime解决mysql连接卡顿问题

一枝梅 1月前 ⋅ 81 阅读

mysql部署环境:腾讯云

mysql版本号:5.7

客户端:SpringBoot 2.2.6.RELEASE

数据库连接池:hikari

问题描述:连接空闲一段时间之后再次请求,后端报错,报错日志

2021-01-17 13:20:54 [XNIO-2 task-25] WARN  c.z.hikari.pool.PoolBase:180 HikariPool-1 - Failed to validate connection com.mysql.cj.jdbc.ConnectionImpl@713d175f (No operations allowed after connection closed.). Possibly consider using a shorter maxLifetime value.

问题分析:根据报错信息,程序建议缩小maxLifetime配置,这样就有可能不会报错了,这个错是hikari数据库连接池组件爆出来的,所以应当在hikari配置

解决方案:新增或者修改如下配置

spring:
datasource:
  hikari:
    idle-timeout: 10000
    max-lifetime: 30000

其它:hikari数据库连接池是spring-boot-starter-jdbc组件引入进来的,所以基本上在spring boot程序中,只要不指定专门的数据库连接池,默认就会使用hikari数据库连接池,以上的配置就能生效

spring-boot-starter-jdbc 2.2.6.RELEASE pom文件完整配置

<?xml version="1.0" encoding="UTF-8"?>
<project xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd" xmlns="http://maven.apache.org/POM/4.0.0"
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
  <modelVersion>4.0.0</modelVersion>
  <parent>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starters</artifactId>
    <version>2.2.6.RELEASE</version>
  </parent>
  <groupId>org.springframework.boot</groupId>
  <artifactId>spring-boot-starter-jdbc</artifactId>
  <version>2.2.6.RELEASE</version>
  <name>Spring Boot JDBC Starter</name>
  <description>Starter for using JDBC with the HikariCP connection pool</description>
  <url>https://projects.spring.io/spring-boot/#/spring-boot-parent/spring-boot-starters/spring-boot-starter-jdbc</url>
  <organization>
    <name>Pivotal Software, Inc.</name>
    <url>https://spring.io</url>
  </organization>
  <licenses>
    <license>
      <name>Apache License, Version 2.0</name>
      <url>https://www.apache.org/licenses/LICENSE-2.0</url>
    </license>
  </licenses>
  <developers>
    <developer>
      <name>Pivotal</name>
      <email>info@pivotal.io</email>
      <organization>Pivotal Software, Inc.</organization>
      <organizationUrl>https://www.spring.io</organizationUrl>
    </developer>
  </developers>
  <scm>
    <connection>scm:git:git://github.com/spring-projects/spring-boot.git</connection>
    <developerConnection>scm:git:ssh://git@github.com/spring-projects/spring-boot.git</developerConnection>
    <url>https://github.com/spring-projects/spring-boot</url>
  </scm>
  <issueManagement>
    <system>Github</system>
    <url>https://github.com/spring-projects/spring-boot/issues</url>
  </issueManagement>
  <dependencies>
    <dependency>
      <groupId>org.springframework.boot</groupId>
      <artifactId>spring-boot-starter</artifactId>
      <version>2.2.6.RELEASE</version>
      <scope>compile</scope>
    </dependency>
    <dependency>
      <groupId>com.zaxxer</groupId>
      <artifactId>HikariCP</artifactId>
      <version>3.4.2</version>
      <scope>compile</scope>
    </dependency>
    <dependency>
      <groupId>org.springframework</groupId>
      <artifactId>spring-jdbc</artifactId>
      <version>5.2.5.RELEASE</version>
      <scope>compile</scope>
    </dependency>
  </dependencies>
</project>

#hikari #mysql
我有话说:

全部评论: 0

    文章目录