java - MySQL 和 Spring MVC 应用程序之间连接失败

标签 java spring-mvc jdbc

我确实尝试过寻找解决这个问题的方法 - 但我似乎无法弄清楚。我真的希望你们知道该怎么做。

我的 Spring MVC 应用程序已经开始失去与数据库的连接,我不知道为什么。这让我发疯。

Spring 数据库设置:

spring.datasource.driverClassName=com.mysql.jdbc.Driver
spring.datasource.url=jdbc:mysql://ip/database-name
spring.jpa.database-platform=org.hibernate.dialect.MySQL5Dialect
spring.jpa.show-sql=false

spring.datasource.tomcat.initialSize=5
spring.datasource.tomcat.maxActive=55
spring.datasource.tomcat.maxIdle=21
spring.datasource.tomcat.minIdle=13
spring.datasource.tomcat.testWhileIdle=true
spring.datasource.tomcat.timeBetweenEvictionRunsMillis=34000
spring.datasource.tomcat.minEvictableIdleTimeMillis=55000
spring.datasource.tomcat.validationInterval=34000
spring.datasource.tomcat.testOnBorrow=true
spring.datasource.tomcat.validationQuery=SELECT 1
spring.datasource.tomcat.removeAbandoned=true
spring.datasource.tomcat.removeAbandonedTimeout=233

spring.jpa.hibernate.ddl-auto=update

我尝试指定“autoReconnect=true”,因为我找到了答案,表明这一点 - 但它并没有解决我的问题。

错误日志:

WARN 5220 --- [-nio-443-exec-6] o.h.engine.jdbc.spi.SqlExceptionHelper   : SQL Error: 0, SQLState: 08S01
ERROR 5220 --- [-nio-443-exec-6] o.h.engine.jdbc.spi.SqlExceptionHelper   : The last packet successfully received from the server was 60,401,089 milliseconds ago.  The last packet sent successfully to the server was 60,401,089 milliseconds ago. is longer than the server configured value of 'wait_timeout'. You should consider either expiring and/or testing connection val
idity 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.
ERROR 5220 --- [-nio-443-exec-6] w.a.UsernamePasswordAuthenticationFilter : An internal error occurred while trying to authenticate the user.

Caused by: com.mysql.jdbc.exceptions.jdbc4.CommunicationsException: The last packet successfully received from the server was 60,401,089 milliseconds ago.  The last packet sent successfully to the server was 60,401,089 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.
at sun.reflect.GeneratedConstructorAccessor277.newInstance(Unknown Source) ~[na:na]
at sun.reflect.DelegatingConstructorAccessorImpl.newInstance(DelegatingConstructorAccessorImpl.java:45) ~[na:1.8.0_91]
at java.lang.reflect.Constructor.newInstance(Constructor.java:423) ~[na:1.8.0_91]
at com.mysql.jdbc.Util.handleNewInstance(Util.java:404) ~[mysql-connector-java-5.1.38.jar:5.1.38]
at com.mysql.jdbc.SQLError.createCommunicationsException(SQLError.java:981) ~[mysql-connector-java-5.1.38.jar:5.1.38]
at com.mysql.jdbc.MysqlIO.send(MysqlIO.java:3652) ~[mysql-connector-java-5.1.38.jar:5.1.38]
at com.mysql.jdbc.MysqlIO.sendCommand(MysqlIO.java:2460) ~[mysql-connector-java-5.1.38.jar:5.1.38]
at com.mysql.jdbc.MysqlIO.sqlQueryDirect(MysqlIO.java:2625) ~[mysql-connector-java-5.1.38.jar:5.1.38]
at com.mysql.jdbc.ConnectionImpl.execSQL(ConnectionImpl.java:2551) ~[mysql-connector-java-5.1.38.jar:5.1.38]
at com.mysql.jdbc.PreparedStatement.executeInternal(PreparedStatement.java:1861) ~[mysql-connector-java-5.1.38.jar:5.1.38]
at com.mysql.jdbc.PreparedStatement.executeQuery(PreparedStatement.java:1962) ~[mysql-connector-java-5.1.38.jar:5.1.38]
at org.hibernate.engine.jdbc.internal.ResultSetReturnImpl.extract(ResultSetReturnImpl.java:82) ~[hibernate-core-4.3.11.Final.jar:4.3.11.Final]
... 111 common frames omitted
Caused by: java.net.SocketException: Broken pipe
at java.net.SocketOutputStream.socketWrite0(Native Method) ~[na:1.8.0_91]
at java.net.SocketOutputStream.socketWrite(SocketOutputStream.java:109) ~[na:1.8.0_91]
at java.net.SocketOutputStream.write(SocketOutputStream.java:153) ~[na:1.8.0_91]
at java.io.BufferedOutputStream.flushBuffer(BufferedOutputStream.java:82) ~[na:1.8.0_91]
at java.io.BufferedOutputStream.write(BufferedOutputStream.java:126) ~[na:1.8.0_91]
at com.mysql.jdbc.MysqlIO.send(MysqlIO.java:3633) ~[mysql-connector-java-5.1.38.jar:5.1.38]
... 117 common frames omitted

谢谢。

最佳答案

我遇到了类似的问题,请尝试两件事:

  1. spring.datasource.url=jdbc:mysql://ip/database-name 更改为 spring.datasource.url=jdbc:mysql://ip/database-name?autoReconnect=true
  2. 我在应用程序中使用c3p0,配置idleConnectionTestPeriod 和preferredTestQuery 解决了我的问题

    <!-- DB Configuration -->
    <bean id="dataSource" class="com.mchange.v2.c3p0.ComboPooledDataSource">
      <property name="jdbcUrl" value="${jdbc_url}" />
      <property name="user" value="${jdbc_username}" />
      <property name="password" value="${jdbc_password}" />
      <property name="driverClass" value="${jdbc_driver_class}" />
    
      <!-- these are C3P0 properties -->
      <property name="acquireIncrement" value="1" /> <!-- Determines how many connections at a time c3p0 will try to acquire when the pool is exhausted. -->
      <property name="minPoolSize" value="${jdbc_min_pool_size}" />
      <property name="maxPoolSize" value="${jdbc_max_pool_size}" />
      <property name="maxIdleTime" value="100" /> <!-- Seconds a Connection can remain pooled but unused before being discarded. Zero means idle connections never expire. -->
    
      <!--set this value less then mysql wait_timeout -->
      <property name="idleConnectionTestPeriod" value="100"/>   <!-- If this is a number greater than 0, C3P0 will test all idle, pooled but unchecked-out connections, every this number of seconds-->
      <property name="preferredTestQuery" value="select 1"/>   <!--a query used to test connections-->
    </bean>
    

你的属性spring.datasource.tomcat.validationInterval似乎做了同样的事情。从错误来看,您的情况下 MySQL wait_timeout 似乎是 60,401,089 毫秒,因此validationInterval 似乎小于该值,但仍尝试将其减少到 100 并查看错误是否再次发生。如果这不起作用,请尝试配置 c3p0 并使用我提到的配置。

关于java - MySQL 和 Spring MVC 应用程序之间连接失败,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/42835224/

相关文章:

java - Tomcat 网络应用程序 : resource bundle variable not working

java - 如何更改Jasper报告打印名称

java - Tomcat 7 war 文件部署

java - 从图像中提取文本

java - linux下如何获取下载文件路径?

java - 为什么我收到错误 SQLException : [Microsoft][ODBC Excel Driver] Too few parameters. Expected 1

java - Spring MVC Ajax : Passing Empty Array to Ajax Controller

java - Spring Boot 拦截所有异常处理程序

java - 尝试使用 H2 数据库更新 JDBC 结果集时出现异常

java - JDBC 和 MySQL 的编码问题