MySql jdbc autoReconnect 的应用[通俗易懂]

MySql jdbc autoReconnect 的应用[通俗易懂]http://dev.mysql.com/doc/connector-j/en/connector-j-reference-configuration-properties.html

大家好,又见面了,我是你们的朋友全栈君。

MySql 的jdbc 配置选项:http://dev.mysql.com/doc/connector-j/en/connector-j-reference-configuration-properties.html

High Availability and Clustering. 这一部分提到,

autoReconnect

Should the driver try to re-establish stale and/or dead connections? If enabled 
the driver will throw an exception for a queries issued on a stale or dead 
connection, which belong to the current transaction, but will attempt reconnect 
before the next query issued on the connection in a new transaction. The use of 
this feature is not recommended, because it has side effects related to session 
state and data consistency when applications don't handle SQLExceptions properly, 
and is only designed to be used when you are unable to configure your application 
to handle SQLExceptions resulting from dead and stale connections properly. 
Alternatively, as a last option, investigate setting the MySQL server variable 
"wait_timeout" to a high value, rather than the default of 8 hours.

Default: false

Since version: 1.1

很明显,官方是不建议使用该配置的,除非你自己不能处理SQLExceptions ,这种情况我倒是还没有遇到。

但是,有种情景下,这个参数是非常有用的:需要不停地查询数据库,没有多线程需求,那么为了效率,最好与数据库保持一个长连接,如果数据库宕机了怎么办?

[1104 17:05:25 854 ERROR] [main] scheduler.service.DatabaseService - com.mysql.jdbc.exceptions.jdbc4.MySQLQueryInterruptedException: Query execution was interrupted
[1104 17:05:27 857 ERROR] [main] scheduler.service.DatabaseService - com.mysql.jdbc.exceptions.jdbc4.CommunicationsException: Communications link failure

The last packet successfully received from the server was 2,003 milliseconds ago.  The last packet sent successfully to the server was 0 milliseconds ago.
[1104 17:05:29 859 ERROR] [main] scheduler.service.DatabaseService - com.mysql.jdbc.exceptions.jdbc4.MySQLNonTransientConnectionException: No operations allowed after statement closed.
[1104 17:05:31 859 ERROR] [main] scheduler.service.DatabaseService - com.mysql.jdbc.exceptions.jdbc4.MySQLNonTransientConnectionException: No operations allowed after statement closed.
[1104 17:05:33 859 ERROR] [main] scheduler.service.DatabaseService - com.mysql.jdbc.exceptions.jdbc4.MySQLNonTransientConnectionException: No operations allowed after statement closed.
[1104 17:05:35 860 ERROR] [main] scheduler.service.DatabaseService - com.mysql.jdbc.exceptions.jdbc4.MySQLNonTransientConnectionException: No operations allowed after statement closed.
[1104 17:05:37 860 ERROR] [main] scheduler.service.DatabaseService - com.mysql.jdbc.exceptions.jdbc4.MySQLNonTransientConnectionException: No operations allowed after statement closed.

可以看到,jdbc 驱动在连接失败后,只会不停地报异常(程序的查询请求都是通过同一个Statement 发出的),当数据库服务重新启动后,仍然没有反应。必须重启应用吗?

这时可以使用这个参数来要求jdbc 驱动在发现数据库连接异常后会自动地重新连接

jdbc:mysql://localhost:3306/scheduler?useUnicode=true&characterEncoding=UTF-8&autoReconnect=true&maxReconnects=2&initialTimeout=5

问题解决

[1105 14:45:52 453 ERROR] [main] scheduler.service.DatabaseService - com.mysql.jdbc.exceptions.jdbc4.MySQLQueryInterruptedException: Query execution was interrupted
[1105 14:46:01 458 ERROR] [main] scheduler.service.DatabaseService - com.mysql.jdbc.exceptions.jdbc4.MySQLNonTransientConnectionException: Could not create connection to database server. Attempted reconnect 2 times. Giving up.
[1105 14:46:10 471 ERROR] [main] scheduler.service.DatabaseService - com.mysql.jdbc.exceptions.jdbc4.MySQLNonTransientConnectionException: Could not create connection to database server. Attempted reconnect 2 times. Giving up.
[1105 14:46:19 475 ERROR] [main] scheduler.service.DatabaseService - com.mysql.jdbc.exceptions.jdbc4.MySQLNonTransientConnectionException: Could not create connection to database server. Attempted reconnect 2 times. Giving up.

可以看到,在尝试重试建立连接失败后,放弃,再重试……

数据库服务一旦恢复正常,就可以自动建立连接,程序可以继续跑了。

版权声明:本文内容由互联网用户自发贡献,该文观点仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请联系我们举报,一经查实,本站将立刻删除。

发布者:全栈程序员-站长,转载请注明出处:https://javaforall.net/160517.html原文链接:https://javaforall.net

(0)
全栈程序员-站长的头像全栈程序员-站长


相关推荐

  • Ubuntu rootfs_怎么制作ubuntu启动U盘

    Ubuntu rootfs_怎么制作ubuntu启动U盘制作ubunturootfs下载base镜像:http://cdimage.ubuntu.com/ubuntu-base/releases/16.04.1/release/解压:tar-zxfubuntu-xx-base-amd64.tar.gz-C./ubuntu-rootfs/apt-getinstallqemu-user-staticcp/usr/bin/qemu-amd64…

    2022年10月7日
    0
  • TCP协议和UDP协议

    TCP协议和UDP协议1.传输控制协议TCP1.1TCP的主要特点:1.1.1面向连接的运输层协议socket部分讲述tcp连接的建立tcp连接的释放tcp的有限状态机1.1.2每一条TCP连接只能有两个端点,每一条TCP链接只能是点对点的(一对一)1.1.3TCP提供可靠交付的服务可靠传输的工作原理可靠传输的实现流量控制拥塞控制1.1.4TCP提供全双工通信1.1.5面向字节流流式服务的特点1.2与TCP有关的面试问题2.用户数据报协议UDP2.1UDP协

    2022年6月7日
    39
  • windows常用端口号,以及服务对应的端口号「建议收藏」

    windows常用端口号,以及服务对应的端口号「建议收藏」查看端口在windows2000/xp/server2003中要查看端口,可以使用netstat命令:依次点击“开始→运行”,键入“cmd”并回车,打开命令提示符窗口。在命令提示符状态下键入“netstat-a-n”,按下回车键后就可以看到以数字形式显示的tcp和udp连接的端口号及状态。小知识:netstat命令用法命令格式:netstat-a-e…

    2022年9月3日
    3
  • 查看 android打包证书签名

    查看 android打包证书签名查看android打包证书签名用javakeytool查看证书签名keytool-list-v-keystore证书路径输入证书密码之后会看到MD5\SHA1\SHA256签名。这里插一句题外话,有的平台需要的也是签名例如微信系列的直接把签名大写改成小写去掉冒号即可。…

    2022年6月7日
    31
  • yolov5启用数据增强、tensorboard可视化及cutout增强

    yolov5启用数据增强、tensorboard可视化及cutout增强yolov5启用数据增强与tensorboard可视化一,yolov5启用数据增强1.data目录下,有两个hyp的文件:data/hyp.scratch.yaml和data/hyp.finetune.yaml具体内容如下:#HyperparametersforVOCfine-tuning#pythontrain.py–batch64–cfg”–weightsyolov5m.pt–datavoc.yaml–img512–epochs50#See

    2022年9月24日
    0
  • jboss安装与配置_下载qq和安装

    jboss安装与配置_下载qq和安装JBoss下载与JBoss安装一.              下载与安装JBoss在本文中,我们下载的JBoss版本为:4.2.1.GA。下载地址:http://www.jboss.org/jbossas/downloads/在如上的下载页中下载JBoss-4.2.1.GA.zip文件。下载完成后,将其解压缩后即可完成安装,解压缩后将其放置到

    2022年10月3日
    0

发表回复

您的邮箱地址不会被公开。 必填项已用 * 标注

关注全栈程序员社区公众号