简体   繁体   中英

Spring Boot - Mysql Driver - JPA - After a long time that server is running a post request shows Could not open JPA EntityManager for transaction

I'm using SPRING BOOT application with this configuration:

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>

My application.properties has these parameters:

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

When I run the server it works fine. Without problems. But after a time that the Spring Boot server is running, I can see this message when I do a POST request message to any REST service:

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"} 

I think that there is an incompatibility with the MySQL driver and the MySQL version defined in pom.xml but I'm not sure.

This application is running in DEBIAN 8. I don't know if it is relevant, but I contribute.

I don't know if the driver could be com.mysql.jdbc.Driver but it is deprecated.

Thanks in advance.

Your db connexions may fail after a long time your application started. If you don't want this issue you need to set configuration so that the application restart its connexion automatically.

In your application.properties, add this :

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM