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


相关推荐

  • python精彩编程200例-python趣味编程100例(99个)

    【实例简介】python如今很流行,AI的首选工具;python趣味编程100例(99个),学习编程不枯燥。【实例截图】【核心代码】python趣味编程100例(99个)└──python趣味编程100例(99个)├──JCP001.py├──JCP002.py├──JCP003.py├──JCP004.py├──JCP005.py├──JCP006.py├──JCP007.py├…

    2022年4月7日
    54
  • Linux进程和计划任务管理

    Linux进程和计划任务管理

    2021年9月6日
    57
  • python实现Lasso回归

    python实现Lasso回归Lasso原理Lasso与弹性拟合比较python实现importnumpyasnpimportmatplotlib.pyplotaspltfromsklearn.metricsimportr2_score#defmain():#产生一些稀疏数据np.random.seed(42)n_samples,n_features=50,200X=np…

    2022年6月8日
    127
  • 深度学习——SPPNet原理[通俗易懂]

    深度学习——SPPNet原理[通俗易懂]从R-CNN到FastR-CNN,有必要了解下SPPNet,其全称为SpatialPyramidPoolingConvolutionalNetworks(空间金字塔池化卷积网络)。它将CNN的输入从固定尺寸改进为任意尺寸,例如在CNN结构中,输入图像的尺寸往往固定的(如224×224像素),输出可看做固定维数的向量。SPPNet在普通的CNN结构中加入了ROI池化层(ROIPo…

    2022年6月11日
    41
  • Ubuntu输入法的使用

    Ubuntu输入法的使用更换输入方式系统 1 进入系统设置 ubuntu 桌面的右上角按钮菜单中 nbsp 系统设置 2 进入语言支持 nbsp 点击系统设置中的 nbsp 语言支持 3 更换输入方式系统在语言支持中的 nbsp 输入方式系统 nbsp 中 nbsp 进行选择 前提是你安装了其他的输入平台 例如我安装了 fcitx 所以可以选择 fcitx 添加删除输入法 nbsp 1 进入文本输入设置 nbsp nbsp nbsp nbsp 点击桌面右上角面板上的 nbsp 输入法菜单 nbsp 中的 nbsp 文本输入设置 2 添加输

    2025年8月21日
    6
  • mac双系统选择启动_mac装双系统好不好

    mac双系统选择启动_mac装双系统好不好解决方案1:开机时长按option键,进入系统选择界面:用左右方向键选择到你要设置为默认启动的盘,然后同时按下ctrl+enter键,即可将其设置为默认启动的系统。解决方案2:选择mac系统进入后,点击系统偏好设置—–>启动磁盘—–>进入如下图界面:首先点击最下面的锁图片,然后再进行更改,选择你要设置为默认启动的系统后,点击

    2022年10月6日
    3

发表回复

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

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