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)
全栈程序员-站长的头像全栈程序员-站长


相关推荐

  • flask框架2_flask框架介绍

    flask框架2_flask框架介绍flask框架2文章目录flask框架2一.状态保持1.Session细节二.高级处理1.上下文2.请求勾子3.路由变量3.1绑定动态URL(重点)3.2正则转换器4.werkzerg库结构(了解)三.参数和配置1.Flask()参数2.Flask应用配置四.脚本启动五.模板1.模板变量的基本使用(重点)2.过滤器一.状态保持1.Session细节…

    2022年8月30日
    3
  • kong 网关教程入门[通俗易懂]

    kong 网关教程入门[通俗易懂]helm安装先创建pvkind:PersistentVolumeapiVersion:v1metadata:name:kong-postgrelabels:release:stablespec:capacity:storage:8GiaccessModes:-ReadWriteOncepersistentVolum…

    2022年6月26日
    78
  • MIPS汇编语言指令分类及寻址模式

    MIPS汇编语言指领分类及寻址模式一、普及一些相关概念指令集:一个给定的计算机体系结构所包含的指令集合。存储程序概念:多种类型的指令和数据均以数字形式存储于存储器的概念,存储程序型计算机即缘于此。1二、MIPS操作数之32个寄存器寄存器名字寄存器编号寄存器功能$zero$0恒等于零$at$1被汇编器保留,用于处理大的常数$v0–$v

    2022年4月17日
    41
  • 【ES6】Promise用法[通俗易懂]

    【ES6】Promise用法[通俗易懂]promise理解及使用Promise解决的问题——异步Promise的基本用法异步操作拒绝及中断调用链ES6对Promise/A+的扩展Promise.all的扩展Promise.race的扩展众所周知的,Javascript是一种单线程的语言,所有的代码必须按照所谓的“自上而下”的顺序来执行。本特性带来的问题就是,一些将来的、未知的操作,必须异步实现(关于异步,我会在另一篇文章里进行讨论)…

    2022年6月18日
    33
  • 光纤交换机划分zone图解

    光纤交换机划分zone图解 HP光纤存储交换机 用户:admin出厂密码:password出厂IP:10.77.77.77 用串口连上交换机,help查出可用的命令IpaddrShow 命令查看IP地址,然后用IE浏览器输入http://10.77.77.77登陆交换机。   划分ZONE点击左下角图标  输入用户admin密码password,进入

    2022年5月21日
    53
  • jquery弹窗插件dialog_jquery进度条插件

    jquery弹窗插件dialog_jquery进度条插件143行js顶部进度条最小插件-nanobar.js源码解析

    2022年4月20日
    69

发表回复

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

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