mySql数据库连接超时错误

来源:互联网 发布:思科 ip绑定mac地址 编辑:程序博客网 时间:2024/04/29 01:38

tomcat运行了一晚上,早上起来发现项目访问不了了,提示如下错误:

Struts Problem Report

Struts has detected an unhandled exception:

Messages:

  1. Software caused connection abort: socket write error
  2. The last packet successfully received from the server was34258 seconds ago.The last packet sent successfully to the server was 34258 seconds ago, which 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.
  3. could not execute query
  4. could not execute query; nested exception is org.hibernate.exception.JDBCConnectionException: could not execute query

Stacktraces

org.springframework.dao.DataAccessResourceFailureException: could not execute query; nested exception is org.hibernate.exception.JDBCConnectionException: could not execute query
    org.springframework.orm.hibernate3.SessionFactoryUtils.convertHibernateAccessException(SessionFactoryUtils.java:625)    org.springframework.orm.hibernate3.HibernateAccessor.convertHibernateAccessException(HibernateAccessor.java:412)    org.springframework.orm.hibernate3.HibernateTemplate.doExecute(HibernateTemplate.java:411)    org.springframework.orm.hibernate3.HibernateTemplate.executeWithNativeSession(HibernateTemplate.java:374)    org.springframework.orm.hibernate3.HibernateTemplate.find(HibernateTemplate.java:912)    org.springframework.orm.hibernate3.HibernateTemplate.find(HibernateTemplate.java:904)    business.tstas.dao.impl.PatentAchieveDaoImpl.findAll(PatentAchieveDaoImpl.java:90)    business.tstas.service.impl.PatentAchieveServiceImpl.findAll(PatentAchieveServiceImpl.java:60)    business.tstas.action.IndexAction.getIndexData(IndexAction.java:128)    sun.reflect.GeneratedMethodAccessor64.invoke(Unknown Source)    sun.reflect.DelegatingMethodAccessorImpl.invoke(Unknown Source)    java.lang.reflect.Method.invoke(Unknown Source)

提示信息很明显,链接超时,并且提示可以设置wait_timeout的大小和配置属性autoReconnect=true来避免这个错误,MySQL服务器默认的“wait_timeout”是28800秒即8小时,意味着如果一个连接的空闲时间超过8个小时,MySQL自动断开该 连接,而连接池却认为该连接还是有效的(因为并未校验连接的有效性),当应用申请使用该连接时,就会导致上面的报错。有以下几种解决方案:

1、不使用hibernate,在connection url中加参数: autoReconnect=true

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

2、使用hibernate配置如下属性:

<property name="connection.autoReconnect">true</property>
        <property name="connection.autoReconnectForPools">true</property>
        <property name="connection.is-connection-validation-required">true</property>

注意:不能配置为如下形式

<property name="hibernate.autoReconnect">true</property>

3、如果还用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>



有网友说(http://blog.csdn.net/zljjava/article/details/7996091),还有一种情况:

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

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


0 0
原创粉丝点击