我有一个应用程序使用spring boot,jpa Hibernate和mysql.我收到了这个错误日志(log)

Caused by: com.mysql.jdbc.exceptions.jdbc4.CommunicationsException: The last packet successfully received from the server was 56,006,037 milliseconds ago.  The last packet sent successfully to the server was 56,006,037 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 problem.

这是我的申请表.属性

# DataSource settings: set here configurations for the database connection
spring.datasource.url = jdbc:mysql://localhost:3306/test
spring.datasource.username = test
spring.datasource.password = test
spring.datasource.driverClassName = com.mysql.jdbc.Driver

# Specify the DBMS
spring.jpa.database = MYSQL

# Show or not log for each sql query
spring.jpa.show-sql = true

# Hibernate settings are prefixed with spring.jpa.hibernate.*
spring.jpa.hibernate.ddl-auto = update
spring.jpa.hibernate.dialect = org.hibernate.dialect.MySQL5Dialect
spring.jpa.hibernate.naming_strategy = org.hibernate.cfg.ImprovedNamingStrategy

To solve this issue I can use

spring.datasource.testOnBorrow=true
spring.datasource.validationQuery=SELECT 1

但我查过了不是recommended.那么,有谁能建议我该如何克服这个错误呢

推荐答案

最简单的方法是在JDBCURL中指定autoReconnect属性,尽管这不是推荐的方法.

spring.datasource.url = jdbc:mysql://localhost:3306/test?autoReconnect=true

当你有一个活动的连接,并且在一个事务中发生了一些事情,并且即将发生重新连接时,这可能会产生问题.当在事务开始时验证连接,并在开始时获取新连接时,它不会给出问题.

不过,在应用程序的生命周期内启用连接验证可能更好.为此,可以指定several properties.

首先,指定池允许的最大连接数.(对于确定最大池大小读取this的读取).

spring.datasource.max-active=10

您可能还需要指定初始连接的数量

spring.datasource.initial-size=5

接下来,您需要指定空闲连接的最小和最大数量.

spring.datasource.max-idle=5
spring.datasource.min-idle=1

要验证连接,需要指定验证查询和验证时间.因为您希望定期进行验证,而不是在从池中检索连接时进行验证(这是为了防止池中的连接断开).

spring.datasource.test-while-idle=true
spring.datasource.test-on-borrow=true
spring.datasource.validation-query=SELECT 1

NOTE:实际上不鼓励使用validation-query,因为JDBC4有更好/不同的连接验证方法.HikariCP将在可用时自动调用JDBC验证方法.

既然在连接空闲时也在进行验证,那么您需要指定希望对连接运行此查询的频率以及连接被视为空闲的时间.

spring.datasource.time-between-eviction-runs-millis=5000 (this is the default)
spring.datasource.min-evictable-idle-time-millis=60000 (this is also default)

这一切都将触发对(空闲)连接的验证,当出现异常或空闲时间已过时,您的连接将从池中删除.

假设您使用Tomcat JDBC作为连接池,那么this很好地理解了配置什么以及如何配置.

UPDATE:弹簧靴2.x将默认连接池切换到HikariCP,而不是Tomcat JDBC.

Mysql相关问答推荐

返回50%的随机结果

mysql查询汇总数据

如何获得表中只有最大值行的一个列被另一个列分区?

Mysql如何分行分词

go&mysql&docker 拒绝连接

MySQL:根据条件查找某些用户的行位置

SQL / MySQL:如何在WHERE IN中判断类似于OR的AND逻辑

Django中的MYSQL查询等效

为什么 fetch 方法不能获取任何东西?(feat node.js,restAPI)

MySQL IF() 函数根据指定条件执行代码块

更新和替换未加引号的 JSON 字符串

SQL UPDATE中的str_replace?

动态创建内联 SQL 表(用于排除左连接)

避免重复进入mysql数据库的最佳方法

是否可以在内部连接期间重命名连接列?

是否可以在 macOS 上只安装 mysqldump

如何在特定数据库中创建表?

MYSQL_ROOT_PASSWORD 已设置但在 docker 容器中获取用户'root'@'localhost'的访问被拒绝(使用密码:YES)

在 WHERE 子句中使用 mysql concat()?

什么是全文索引,我应该什么时候使用它?