Connector/J connection property 'autoReconnect=true' to avoid this problem.

最近做的比特宝项目发现每次进入测试服务器时候都报这个错误:Connector/J connection property 'autoReconnect=true' to avoid this problem.

 

错误日志如下:

 

Connector/J connection property 'autoReconnect=true' to avoid this problem.
        at org.springframework.jdbc.support.SQLExceptionSubclassTranslator.doTranslate(SQLExceptionSubclassTranslator.java:98)
        at org.springframework.jdbc.support.AbstractFallbackSQLExceptionTranslator.translate(AbstractFallbackSQLExceptionTranslator.java:72)
        at org.springframework.jdbc.support.AbstractFallbackSQLExceptionTranslator.translate(AbstractFallbackSQLExceptionTranslator.java:80)
        at org.springframework.jdbc.core.JdbcTemplate.execute(JdbcTemplate.java:603)
        at org.springframework.jdbc.core.JdbcTemplate.query(JdbcTemplate.java:637)
        at org.springframework.jdbc.core.JdbcTemplate.query(JdbcTemplate.java:662)
        at org.springframework.jdbc.core.JdbcTemplate.query(JdbcTemplate.java:702)
        at org.springframework.jdbc.core.namedparam.NamedParameterJdbcTemplate.queryForObject(NamedParameterJdbcTemplate.java:178)
        at com.bitbao.cm.dao.UDBUserDao.getUserByUidForAccount(UDBUserDao.java:334)
        at com.bitbao.cm.controller.WebBaseController.listLeftBar(WebBaseController.java:59)
        at com.bitbao.cm.controller.WebBaseController.preHanle(WebBaseController.java:44)
        at sun.reflect.GeneratedMethodAccessor56.invoke(Unknown Source)
        at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:25)
        at java.lang.reflect.Method.invoke(Method.java:597)
        at org.springframework.web.bind.annotation.support.HandlerMethodInvoker.invokeHandlerMethod(HandlerMethodInvoker.java:162)
        at org.springframework.web.servlet.mvc.annotation.AnnotationMethodHandlerAdapter.invokeHandlerMethod(AnnotationMethodHandlerAdapter.java:436)
        at org.springframework.web.servlet.mvc.annotation.AnnotationMethodHandlerAdapter.handle(AnnotationMethodHandlerAdapter.java:424)
        at org.springframework.web.servlet.DispatcherServlet.doDispatch(DispatcherServlet.java:863)
        at org.springframework.web.servlet.DispatcherServlet.doService(DispatcherServlet.java:792)
        at org.springframework.web.servlet.FrameworkServlet.processRequest(FrameworkServlet.java:851)
        at org.springframework.web.servlet.FrameworkServlet.doGet(FrameworkServlet.java:756)
        at javax.servlet.http.HttpServlet.service(HttpServlet.java:617)
        at javax.servlet.http.HttpServlet.service(HttpServlet.java:717)
        at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:290)
        at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:206)
        at org.apache.catalina.core.ApplicationDispatcher.invoke(ApplicationDispatcher.java:646)
        at org.apache.catalina.core.ApplicationDispatcher.processRequest(ApplicationDispatcher.java:438)
        at org.apache.catalina.core.ApplicationDispatcher.doForward(ApplicationDispatcher.java:374)
        at org.apache.catalina.core.ApplicationDispatcher.forward(ApplicationDispatcher.java:302)
        at org.apache.catalina.core.StandardHostValve.custom(StandardHostValve.java:421)
        at org.apache.catalina.core.StandardHostValve.status(StandardHostValve.java:342)
        at org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:143)
        at org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:102)
        at org.apache.catalina.valves.AccessLogValve.invoke(AccessLogValve.java:554)
        at org.apache.catalina.core.StandardEngineValve.invoke(StandardEngineValve.java:109)
        at org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:298)
        at org.apache.coyote.http11.Http11Processor.process(Http11Processor.java:859)
        at org.apache.coyote.http11.Http11Protocol$Http11ConnectionHandler.process(Http11Protocol.java:588)
        at org.apache.tomcat.util.net.JIoEndpoint$Worker.run(JIoEndpoint.java:489)
        at java.lang.Thread.run(Thread.java:662)
Caused by: com.mysql.jdbc.exceptions.jdbc4.CommunicationsException: The last packet successfully received from the server was 153,619,009 milliseconds ago.  The last packet sent successfully to the server was 153,619,010 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.NativeConstructorAccessorImpl.newInstance0(Native Method)
        at sun.reflect.NativeConstructorAccessorImpl.newInstance(NativeConstructorAccessorImpl.java:39)
        at sun.reflect.DelegatingConstructorAccessorImpl.newInstance(DelegatingConstructorAccessorImpl.java:27)
        at java.lang.reflect.Constructor.newInstance(Constructor.java:513)
        at com.mysql.jdbc.Util.handleNewInstance(Util.java:411)
        at com.mysql.jdbc.SQLError.createCommunicationsException(SQLError.java:1116)
        at com.mysql.jdbc.MysqlIO.send(MysqlIO.java:3352)
        at com.mysql.jdbc.MysqlIO.sendCommand(MysqlIO.java:1971)
        at com.mysql.jdbc.MysqlIO.sqlQueryDirect(MysqlIO.java:2151)
        at com.mysql.jdbc.ConnectionImpl.execSQL(ConnectionImpl.java:2625)
        at com.mysql.jdbc.PreparedStatement.executeInternal(PreparedStatement.java:2119)
        at com.mysql.jdbc.PreparedStatement.executeQuery(PreparedStatement.java:2281)
        at com.mchange.v2.c3p0.impl.NewProxyPreparedStatement.executeQuery(NewProxyPreparedStatement.java:76)
        at org.springframework.jdbc.core.JdbcTemplate$1.doInPreparedStatement(JdbcTemplate.java:644)
        at org.springframework.jdbc.core.JdbcTemplate.execute(JdbcTemplate.java:587)
        ... 36 more
Caused by: java.net.SocketException: Broken pipe
        at java.net.SocketOutputStream.socketWrite0(Native Method)
        at java.net.SocketOutputStream.socketWrite(SocketOutputStream.java:92)
        at java.net.SocketOutputStream.write(SocketOutputStream.java:136)
        at java.io.BufferedOutputStream.flushBuffer(BufferedOutputStream.java:65)
        at java.io.BufferedOutputStream.flush(BufferedOutputStream.java:123)
        at com.mysql.jdbc.MysqlIO.send(MysqlIO.java:3333)
        ... 44 more

 

引入几个解决方案:

 

第一种:

属性文件
c3p0.driverClass=com.mysql.jdbc.Driver
c3p0.user=root
c3p0.password=edwin
c3p0.jdbcUrl=jdbc\:mysql\://192.168.1.123\:3306/edwin?useUnicode\=true&characterEncoding\=utf-8
c3p0.minPoolSize = 1
c3p0.maxPoolSize = 50
c3p0.initialPoolSize = 1
c3p0.maxIdleTime = 25000
c3p0.acquireIncrement = 1
c3p0.acquireRetryAttempts = 30
c3p0.acquireRetryDelay = 1000
c3p0.testConnectionOnCheckin = true
c3p0.automaticTestTable = t_c3p0
c3p0.idleConnectionTestPeriod = 18000
c3p0.checkoutTimeout=5000
---------------------------------------------------------
spring配置

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
      xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
      xmlns:aop="http://www.springframework.org/schema/aop"
      xmlns:tx="http://www.springframework.org/schema/tx"
      xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
        http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.0.xsd
           http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-3.0.xsd
           http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-3.0.xsd" default-autowire="byName" default-lazy-init="false"> 
 <bean id="propertyConfigurer" class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer"> 
    <property name="location"> 
     <value>classpath:com/edwin/db/config/c3p0.properties</value> 
    </property>
 </bean>
 <bean id="dataSource" class="com.mchange.v2.c3p0.ComboPooledDataSource" destroy-method="close"> 
    <property name="driverClass"> 
     <value>${c3p0.driverClass}</value> 
    </property> 
    <property name="jdbcUrl"> 
     <value>${c3p0.jdbcUrl}</value> 
    </property> 
    <property name="user"> 
     <value>${c3p0.user}</value> 
    </property> 
    <property name="password"> 
     <value>${c3p0.password}</value> 
    </property> 
    <property name="minPoolSize"> 
     <value>${c3p0.minPoolSize}</value>
    </property>
    <property name="maxPoolSize"> 
     <value>${c3p0.maxPoolSize}</value> 
    </property> 
    <property name="initialPoolSize"> 
     <value>${c3p0.initialPoolSize}</value> 
    </property> 
    <property name="maxIdleTime"> 
     <value>${c3p0.maxIdleTime}</value> 
    </property> 
    <property name="acquireIncrement"> 
     <value>${c3p0.acquireIncrement}</value> 
    </property> 
    <property name="acquireRetryAttempts"> 
     <value>${c3p0.acquireRetryAttempts}</value> 
    </property> 
    <property name="acquireRetryDelay"> 
     <value>${c3p0.acquireRetryDelay}</value> 
    </property> 
    <property name="testConnectionOnCheckin"> 
     <value>${c3p0.testConnectionOnCheckin}</value> 
    </property>
        <property name="automaticTestTable"> 
     <value>${c3p0.automaticTestTable}</value> 
    </property> 
    <property name="idleConnectionTestPeriod"> 
     <value>${c3p0.idleConnectionTestPeriod}</value> 
    </property>
   <property name="checkoutTimeout"> 
     <value>${c3p0.checkoutTimeout}</value> 
    </property> 
 </bean>
 
</beans> 
 

二种. 如果不用hibernate的话, 则在 connection url中加参数: autoReconnect=true

jdbc.url=jdbc:mysql://ipaddress:3306/database?autoReconnect=true&amp;autoReconnectForPools=true

 


三种。用hibernate的话, 加如下属性:
<property name="connection.autoReconnect">true</property>
<property name="connection.autoReconnectForPools">true</property>
<property name="connection.is-connection-validation-required">true</property>

 


四。要是还用c3p0连接池:
<property name="hibernate.c3p0.acquire_increment">1</property>
<property name="hibernate.c3p0.idle_test_period">0</property>
<property name="hibernate.c3p0.timeout">0</property>
<property name="hibernate. c3p0.validate">true</property>

 

 

 

最不好的解决方案

 

使用Connector/J连接MySQL数据库,程序运行较长时间后就会报以下错误:

Communications link failure,The last packet successfully received from the server was *** millisecond ago.The last packet successfully sent to the server was *** millisecond ago。

其中错误还会提示你修改wait_timeout或是使用Connector/J的autoReconnect属性避免该错误。

后来查了一些资料,才发现遇到这个问题的人还真不少,大部分都是使用连接池方式时才会出现这个问题,短连接应该很难出现这个问题。这个问题的原因:

MySQL服务器默认的“wait_timeout”是28800秒即8小时,意味着如果一个连接的空闲时间超过8个小时,MySQL将自动断开该连接,而连接池却认为该连接还是有效的(因为并未校验连接的有效性),当应用申请使用该连接时,就会导致上面的报错。

1.按照错误的提示,可以在JDBC URL中使用autoReconnect属性,实际测试时使用了autoReconnect=true& failOverReadOnly=false,不过并未起作用,使用的是5.1版本,可能真像网上所说的只对4之前的版本有效。

2.没办法,只能修改MySQL的参数了,wait_timeout最大为31536000即1年,在my.cnf中加入:

 

[mysqld]

wait_timeout=31536000

interactive_timeout=31536000

重启生效,需要同时修改这两个参数。


如果不想改数据库文件的话推荐用第一种方式

你可能感兴趣的:(Connection)