`
woshixushigang
  • 浏览: 576327 次
  • 性别: Icon_minigender_1
  • 来自: 北京
社区版块
存档分类

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

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


如果不想改数据库文件的话推荐用第一种方式
分享到:
评论

相关推荐

    c3p0-0.9.5.2jar和mysql8.0.11jdbc

    autoReconnect=true&lt;/property&gt; &lt;property name="user"&gt;root&lt;/property&gt; &lt;property name="password"&gt;password&lt;/property&gt; &lt;property name="initialPoolSize"&gt;10&lt;/property&gt; &lt;property name="minPoolSize"&gt;10&lt;/...

    OpenKM配置指南

    autoReconnect=true"/&gt; &lt;param name="schemaObjectPrefix" value="rep_"/&gt; &lt;param name="schema" value="mysql"/&gt; &lt;param name="user" value="openkm"/&gt; &lt;param name="password" value="123456"/&gt; &lt;/FileSystem...

    Spring3.0+Struts2.1+ Hibernate3.5融合步骤

    autoReconnect=true&useUnicode=true&characterEncoding=UTF8&lt;/property&gt; &lt;property name="hibernate.connection.username"&gt;root&lt;/property&gt; &lt;property name="hibernate.connection.password"&gt;root&lt;/property&gt; ...

    quartz在Spring中的配置

    useUnicode=true&characterEncoding=UTF-8&autoReconnect=true jdbc.username=root jdbc.password=kfs cpool.checkoutTimeout=5000 cpool.minPoolSize=10 cpool.maxPoolSize=25 cpool.maxIdleTime=7200 cpool....

    hibernate多数据库配置

    &lt;property name="connection.datasource"&gt;dataSource&lt;/property&gt; &lt;!-- 其他 Hibernate 配置信息 --&gt; &lt;/session-factory&gt; &lt;/hibernate-configuration&gt; ``` 在上面的配置中,我们使用 DynamicDataSource 作为 ...

    hibernate自动重新连接数据库proxool的使用

    &lt;property name="hibernate.connection.provider_class"&gt;org.hibernate.service.jdbc.connections.internal.ProxoolConnectionProvider&lt;/property&gt; &lt;property name="hibernate.proxool.pool_name"&gt;my_pool_name&lt;/...

    Mysql JDBC Url参数说明

    autoReconnect=true ``` 6. **connectTimeout**: 设置连接超时时间(毫秒)。 ```text jdbc:mysql://localhost:3306/mydb?connectTimeout=10000 ``` 7. **useAffectedRows**: 是否返回实际受影响的行数,而...

    bbs论坛源码jsp版

    useUnicode=true&characterEncoding=GB2312&autoReconnect=true jdbc.username.mysql=root jdbc.password.mysql=root 3、 Web容器:tomcat6.x 4、 页面访问路径: 论坛首页: http://localhost:8080/bbs 后台管理...

    基于SpringBoot的学生成绩管理系统.zip-7

    一.开发环境 IDEA + JDK8 + MySQL5.7 + navicate 二.技术栈 Springboot+Mybatis+layui 三....数据库地址url修改为 spring.datasource.url=jdbc:mysql://localhost:3306/score?...autoReconnect=true&use

    创建dbcp连接,dbcp(Spring)

    autoReconnect=true"/&gt; &lt;property name="username" value="root"/&gt; &lt;property name="password" value="123456"/&gt; &lt;property name="initialSize" value="100"/&gt; &lt;property name="maxIdle" value="10"/&gt; ...

    如何在一个WEB程序里同时连接多个数据库

    autoReconnect=true&lt;/prop&gt; &lt;prop key="hibernate.connection.username"&gt;root&lt;/prop&gt; &lt;prop key="hibernate.connection.password"&gt;1234&lt;/prop&gt; &lt;/props&gt; &lt;/property&gt; &lt;/bean&gt; ``` ##### 3. DAO层设计 为了实现...

    关于tomcat和mysql连接池问题的详解.docx

    autoReconnect=true" // 数据库URL maxActive="4"/&gt; // 最大活动连接数 ``` 这种方式的效果与在Tomcat管理界面中设置相同。 2. 或者在$Tomcat/conf/server.xml的`&lt;Context&gt;`标签内添加以下代码: ```xml ...

    nacos插件-mysql-connector-java-8.0.19.zip

    《Nacos插件与MySQL Connector/J 8.0.19的深度理解与应用》 在现代企业级的分布式系统中,Nacos作为一款优秀的动态服务发现、配置管理和服务管理平台,扮演着至关重要的角色。而MySQL作为广泛使用的开源关系型...

    学生选课系统学年设计(jsp,servlet,mysql,tomcat)

    &lt;Resource name="zk" auth="Container" type="javax.sql.DataSource" maxActive="100" maxIdle="30" maxWait="10000" username="(此处填你的数据库用户名)" password="(此处填你的数据库密码)" driverClassName=...

    生产环境高可用Nacos集群

    characterEncoding=utf8&connectTimeout=1000&socketTimeout=3000&autoReconnect=true db.url.1=jdbc:mysql://11.163.152.9:3306/nacos_devtest?characterEncoding=utf8&connectTimeout=1000&socketTimeout=3000&...

    Mybatis课件(基于传智修改)

    useUnicode=true&characterEncoding=utf8&autoReconnect=true&allowMultiQueries=true jdbc.username=root jdbc.password=123456 ``` - **`log4j.properties`**: ```properties log4j.rootLogger=DEBUG,A1 ...

    mysql-connector-java-5.1.5

    6. **配置参数**:MySQL Connector/J支持多种配置参数,如`useUnicode=true`、`characterEncoding=utf8`用于设置字符编码,`autoReconnect=true`用于确保断线后自动重连等。 7. **兼容性与版本**:5.1.5版本的MySQL...

    mysql-connector-java-3.0.12-production.zip

    MySQL Connector/J 3.0.12 是 MySQL 官方提供的用于 Java 应用程序连接 MySQL 数据库的驱动程序。这个压缩包包含了该版本的驱动程序及相关文件,为开发者提供了在 Java 平台上与 MySQL 数据库进行交互的能力。以下是...

Global site tag (gtag.js) - Google Analytics