MySQL Hibernate Spring

Java 无法执行 MySQL

could not execute query; 

近日进行 java web项目优化 MySQL 数据查询,把数据进行缓存。通过使用Observer模式,监控数据、当数据没有发生更新时

直接使用缓存中的数据而不进行实际的数据查询。

一切看上去很好!但是项目运行一段时间后,奇怪的问题来了。

错误日志片段:

org.springframework.dao.DataAccessResourceFailureException: could not execute query; nested exception is org.hibernate.exception.JDBCConnectionException: could not execute query

Caused by: com.mysql.jdbc.exceptions.jdbc4.CommunicationsException: The last packet successfully received from the server was 90,091,266 milliseconds ago.  The last packet sent successfully to the server was 90,091,282 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.


问题根源:

MySQL 服务器默认的“wait_timeout”的时间,超过此时间后该session.connection 就失效出现错误但是Spring 还有 Hibernate还认为其连接为可用状态,当调用此connection进行操作时问题就出现了。

而错误需要重启Tomcat。

 

解决的思路2类:

1.     数据库连接URL设置支持当发生错误时自动进重新连接:解决方法1-3

2.     修改MySQL服务器默认的“wait_timeout”的时间:这个方法是个不好的解决方法,其只是延长了出错的的时间,当程序超过该 设置时间问题依旧。解决方法4


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

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

    二种。用hibernate的话, 加如下属性:

        <propertyname="connection.autoReconnect">true</property>

        <propertyname="connection.autoReconnectForPools">true</property>

        <propertyname="connection.is-connection-validation-required">true</property>

    三。要是还用c3p0连接池:

        <propertyname="hibernate.c3p0.acquire_increment">1</property>

        <propertyname="hibernate.c3p0.idle_test_period">0</property>

        <propertyname="hibernate.c3p0.timeout">0</property>

        <propertyname="hibernate.c3p0.validate">true</property>

 四。最不好的解决方案

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

 

Communicationslink failure,The last packet successfully received from the serverwas *** 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

 

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


转载至
http://aayy520.blog.163.com/blog/static/23182260201102994534777/
点击打开链接




  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值