2017-03-16 44 views
1

我真的试图找到解决这个问题的方法 - 但我似乎无法弄清楚。我真的希望你们知道该怎么做。MySQL和Spring MVC应用程序之间的连接失败

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

春DB设置:

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的=真”,因为我找到了答案,那建议 - 但它并没有解决我的问题。

错误日志:

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 

感谢。

回答

0

我正面临着类似的问题,尝试两两件事:

  1. 变化spring.datasource.url=jdbc:mysql://ip/database-namespring.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 ms,所以validationInterval看起来比这少,但仍尝试将它减少到100,并查看错误是否再次出现。如果这不起作用,请尝试配置c3p0并使用我提到的配置。

+0

谢谢!我会试一试,看看能否解决它:-) – doolylol

相关问题