最新文章专题视频专题问答1问答10问答100问答1000问答2000关键字专题1关键字专题50关键字专题500关键字专题1500TAG最新视频文章推荐1 推荐3 推荐5 推荐7 推荐9 推荐11 推荐13 推荐15 推荐17 推荐19 推荐21 推荐23 推荐25 推荐27 推荐29 推荐31 推荐33 推荐35 推荐37视频文章20视频文章30视频文章40视频文章50视频文章60 视频文章70视频文章80视频文章90视频文章100视频文章120视频文章140 视频2关键字专题关键字专题tag2tag3文章专题文章专题2文章索引1文章索引2文章索引3文章索引4文章索引5123456789101112131415文章专题3
问答文章1 问答文章501 问答文章1001 问答文章1501 问答文章2001 问答文章2501 问答文章3001 问答文章3501 问答文章4001 问答文章4501 问答文章5001 问答文章5501 问答文章6001 问答文章6501 问答文章7001 问答文章7501 问答文章8001 问答文章8501 问答文章9001 问答文章9501
当前位置: 首页 - 科技 - 知识百科 - 正文

java调用mysql获取不到连接的问题_MySQL

来源:懂视网 责编:小采 时间:2020-11-09 19:11:45
文档

java调用mysql获取不到连接的问题_MySQL

java调用mysql获取不到连接的问题_MySQL:之前做了一个web项目的时候,好好的网站第二天总是会提示using the Connector/J connection property 'autoReconnect=true' to avoid this problem. 这样的错误com.mysql.jdbc.CommunicationsException: The la
推荐度:
导读java调用mysql获取不到连接的问题_MySQL:之前做了一个web项目的时候,好好的网站第二天总是会提示using the Connector/J connection property 'autoReconnect=true' to avoid this problem. 这样的错误com.mysql.jdbc.CommunicationsException: The la

之前做了一个web项目的时候,好好的网站第二天总是会提示using the Connector/J connection property 'autoReconnect=true' to avoid this problem. 这样的错误

com.mysql.jdbc.CommunicationsException: The last packet successfully received from the server was58129 seconds ago.The last packet sent successfully to the server was 58129 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.

问题的根本原因是是mysql超时设置的问题。如 果连接闲置8小时 (8小时内没有进行数据库操作), mysql就会自动断开连接

第一种解决方案是在 connection url中加参数: autoReconnect=true

jdbc.url=jdbc:mysql://ipaddress:3306/database?autoReconnect=true&autoReconnectForPools=true
第二种如果使用hibernate的时候做如下配置
<property name="connection.autoReconnect">trueproperty>
<property name="connection.autoReconnectForPools">trueproperty>
<property name="connection.is-connection-validation-required">trueproperty>
如果使用了c3p0做如下配置
<property name="hibernate.c3p0.acquire_increment">1property>
<property name="hibernate.c3p0.idle_test_period">0property>
<property name="hibernate.c3p0.timeout">0property>
<property name="hibernate.c3p0.validate">trueproperty>
第三种方法如下:

大部分都是使用连接池方式时才会出现这个问题,短连接应该很难出现这个问题。这个问题的原因:

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中加入:

wait_timeout=31536000
interactive_timeout=31536000

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

参考文档: http://aayy520.blog.163.com/blog/static/23182260201102994534777/

总结一下,这种问题主要是使用了连接池没有识别出过期的链接,解决方案就是数据库连接加一个autoReconnect的参数。或者使用框架的一些参数来控制。

声明:本网页内容旨在传播知识,若有侵权等问题请及时与本网联系,我们将在第一时间删除处理。TEL:177 7030 7066 E-MAIL:11247931@qq.com

文档

java调用mysql获取不到连接的问题_MySQL

java调用mysql获取不到连接的问题_MySQL:之前做了一个web项目的时候,好好的网站第二天总是会提示using the Connector/J connection property 'autoReconnect=true' to avoid this problem. 这样的错误com.mysql.jdbc.CommunicationsException: The la
推荐度:
标签: 网站 连接 问题
  • 热门焦点

最新推荐

猜你喜欢

热门推荐

专题
Top