gpt4 book ai didi

java - Spring Boot - Mysql 驱动程序 - JPA - 很长一段时间后,服务器运行发布请求显示无法打开 JPA EntityManager 进行事务

转载 作者:行者123 更新时间:2023-11-29 18:34:39 25 4
gpt4 key购买 nike

我正在使用具有以下配置的 SPRING BOOT 应用程序:

pom.xml

 <parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>1.5.4.RELEASE</version>
</parent>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<version>6.0.6</version>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-data-jpa</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-mail</artifactId>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-orm</artifactId>
</dependency>
<dependency>
<groupId>org.thymeleaf</groupId>
<artifactId>thymeleaf-spring4</artifactId>
</dependency>
<dependency>
<groupId>com.google.guava</groupId>
<artifactId>guava</artifactId>
<version>22.0</version>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
</plugins>
</build>

我的application.properties具有以下参数:

mysql.url=jdbc:mysql://localhost:3306/database?autoReconnect=true
mysql.username=root
mysql.password=root
mysql.driver-class-name=com.mysql.cj.jdbc.Driver

当我运行服务器时,它工作正常。没有什么问题。但是,在 Spring Boot 服务器运行一段时间后,当我向任何 REST 服务发送 POST 请求消息时,我可以看到此消息:

100   908    0   897  100    11   1741     21 --:--:-- --:--:-- --:--:--  1853{"timestamp":1501411130329,"status":500,"error":"Internal Server Error","exception":"java.lang.IllegalArgumentException","message":"org.springframework.transaction.CannotCreateTransactionException: Could not open JPA EntityManager for transaction; nested exception is javax.persistence.PersistenceException: com.mysql.cj.jdbc.exceptions.CommunicationsException: The last packet successfully received from the server was 295.677.715 milliseconds ago.  The last packet sent successfully to the server was 295.677.715 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.","path":"/get_last_user"} 

我认为 pom.xml 中定义的 MySQL 驱动程序和 MySQL 版本不兼容,但我不确定。

这个应用程序在 DEBIAN 8 中运行。我不知道它是否相关,但我做出了贡献。

我不知道驱动程序是否可以是com.mysql.jdbc.Driver,但它已被弃用。

提前致谢。

最佳答案

应用程序启动很长时间后,您的数据库连接可能会失败。如果您不希望出现此问题,则需要设置配置以便应用程序自动重新启动其连接。

在您的 application.properties 中,添加以下内容:

spring.datasource.validationQuery=SELECT 1
spring.datasource.testOnBorrow=true
spring.datasource.max-active=10
spring.datasource.max-idle=5
spring.datasource.min-idle=5
spring.datasource.initial-size=3
spring.datasource.testWhileIdle=true
spring.datasource.max-wait=10000

关于java - Spring Boot - Mysql 驱动程序 - JPA - 很长一段时间后,服务器运行发布请求显示无法打开 JPA EntityManager 进行事务,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/45399391/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com