Mysql中JDBC如何实现自动重连机制的实例


本文摘自PHP中文网,作者黄舟,侵删。

最近在工作中发现了一个问题,通过查找相关的资料终于解决了,下面这篇文章主要给大家介绍了关于JDBC实现Mysql自动重连机制的相关资料,文中给出多种解决的方法,需要的朋友可以参考借鉴,下面来一起看看吧。

前言

本文主要给大家介绍的是关于JDBC实现Mysql自动重连机制的相关内容,分享出来供大家参考学习,下面来一起看看详细的介绍:

日志:using the Connector/J connection property 'autoReconnect=true' to avoid this problem


1

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就会自动断开连接, 要重启tomcat.

解决办法:

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


1

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

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


1

2

3

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

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

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

第三种:要是还用c3p0连接池:


1

2

3

4

<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>

第四种:最不好的解决方案

阅读剩余部分

相关阅读 >>

mysql索引下推(icp)的简单理解与示例

mysql中union与union all的用法

分析优化mysql 多表联合查询效率

三分钟带你了解mysql数据类型

介绍高级进阶的mysql数据库sql语句

mysql修改权限后本地无法登录怎么办

root密码忘记该怎么办

我所理解的mysql之二:索引

mysql常见索引类型有哪些

mysql显式类型转换实例分享

更多相关阅读请进入《mysql》频道 >>


数据库系统概念 第6版
书籍

数据库系统概念 第6版

机械工业出版社

本书主要讲述了数据模型、基于对象的数据库和XML、数据存储和查询、事务管理、体系结构等方面的内容。



打赏

取消

感谢您的支持,我会继续努力的!

扫码支持
扫码打赏,您说多少就多少

打开支付宝扫一扫,即可进行扫码打赏哦

分享从这里开始,精彩与您同在

评论

管理员已关闭评论功能...