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中常用的的内置函数

mysql5.7.20 安装配置方法图文教程(mac)

navicat for mysql过期怎么破解

mysql怎么增加int字段?

怎么重启mysql数据库

mysql中 in和not in用法详解

如何更改mysql数据库的编码字符集

mysql中char和varchar的区别

电脑开机出现mysql的出错怎么办

mysql最大连接数怎么设置

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


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

数据库系统概念 第6版

机械工业出版社

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



打赏

取消

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

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

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

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

评论

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