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


相关推荐

  • FreeWebHostingArea_Freefilesync

    FreeWebHostingArea_Freefilesync http://bbs.et8.net/bbs/showthread.php?t=896519hostingsiteshttp://www.orbitfiles.com/http://filexoom.com/http://www.sendthisfile.com/http://www.albumtown.com/http://app02.bonpoo.com/f

    2022年10月8日
    2
  • 安利一款Python开发的仿Linux树形显示目录tree命令「建议收藏」

    安利一款Python开发的仿Linux树形显示目录tree命令「建议收藏」大家好,我是小小明,今天要带大家通过python来实现仿Linux的tree命令。文章目录Linux与Windows的tree命令Linux的tree命令演示Windows的tree命令Python自制tree命令os模块基础代码Rich库关于tree模块的官方示例调用Tree模块实现仿Linux树形显示目录效果安装自定义tree模块首先看看Linux下的tree命令效果如何:Linux与Windows的tree命令Linux的tree命令演示在CentOS的Linux系统下,我们可以再使用yum

    2022年7月25日
    13
  • 长轮询的使用实现_python 轮询

    长轮询的使用实现_python 轮询轮询(Polling):是指不管服务器端有没有更新,客户端(通常是指浏览器)都定时的发送请求进行查询,轮询的结果可能是服务器端有新的更新过来,也可能什么也没有,只是返回个空的信息。不管结果如何,客户端处理完后到下一个定时时间点将继续下一轮的轮询。长轮询(LongPolling):长轮询的服务其客户端是不做轮询的,客户端在发起一次请求后立即挂起,一直到服务器端有更新的时候,服务器才会主动推送信息到…

    2022年10月14日
    2
  • java集合详解_通俗易懂java集合

    java集合详解_通俗易懂java集合ArrayDequeArrayDeque是Deque接口的一个实现,使用了可变数组,所以没有容量上的限制。同时,ArrayDeque是线程不安全的,在没有外部同步的情况下,不能再多线程环境下使用。ArrayDeque是Deque的实现类,可以作为栈来使用,效率高于Stack;也可以作为队列来使用,效率高于LinkedList。需要注意的是,ArrayDeque不支持null值。ArrayDeque初识说明书和继承关系还是按照国际惯例,先看一下ArrayDeque的说明书,其实往往很多时候你的困

    2022年9月20日
    3
  • hybrid开发_混合app开发用什么技术

    hybrid开发_混合app开发用什么技术转载请标明出处:一片枫叶的专栏上一篇文章中我们介绍了Android开发中经常会涉及到但又常常被忽视掉的开发者模式。主要讲解了包括如何打开手机的开发者模式,开发者模式中各个菜单的意义和作用,如何清除手机App数据,以及清除手机App数据具体清除那些数据等知识点,具体关于Android中开发者模式的知识,可参考我的:Android产品研发(十六)–>开发者选项本文将介绍Android

    2022年9月23日
    3
  • 蒲式耳,磅换算成公斤和吨

    蒲式耳,磅换算成公斤和吨一个小东西,给自己留作备份用的<!DOCTYPEhtml><htmllang="en"><head><metacharset

    2022年8月3日
    4

发表回复

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

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