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


相关推荐

  • event.srcElement 用法笔记

    event.srcElement 用法笔记event.srcElement可以捕获当前事件作用的对象,如event.srcElement.tagName可以捕获活动标记名称。event.srcElement.TagName//事件对象的h

    2022年7月2日
    29
  • socketpair函数用法[通俗易懂]

    socketpair函数用法[通俗易懂]socketpair()函数的声明:#include<sys/types.h>#include<sys/socket.h>intsocketpair(intd,inttype,intprotocol,intsv[2]);socketpair()函数用于创建一对无名的、相互连接的套接子。如果函数成功,则返回0,创建好的套接字分别是sv[0]和sv[1];否则返回-1,错误码保存于errno中。基本用法:1.这对套接字可以用于全双工通信,每一个..

    2022年10月14日
    0
  • idea2021.9 激活码[最新免费获取]

    (idea2021.9 激活码)最近有小伙伴私信我,问我这边有没有免费的intellijIdea的激活码,然后我将全栈君台教程分享给他了。激活成功之后他一直表示感谢,哈哈~IntelliJ2021最新激活注册码,破解教程可免费永久激活,亲测有效,下面是详细链接哦~https://javaforall.net/100143.html83…

    2022年3月27日
    67
  • DropDownList绑定数据库「建议收藏」

    DropDownList绑定数据库「建议收藏」//获取文本this.DropDownList1.SelectedItem.Text;本类内使用protectedvoidPage_Load(objectsender,EventArgse)   {       this.DropDownList1.Items.Clear();       this.DropDownList1.DataSource=DbHelperSQL.Query(“select*fromauthors”).Tables[0];       this.Drop

    2022年10月8日
    0
  • Hash Verification哈希值校验工具[通俗易懂]

    Hash Verification哈希值校验工具[通俗易懂]最后更新:2019-2-26HashVerification最新版:HashVerificationV1.0NEW!版本号:V1.0.0.14973大小:21KB/zip格式压缩,63KB/解压后 MD5校验码 SHA1校验码 2ad54536be68bd7446f82da1bd16b113 74610b4cd2fe675d…

    2022年9月13日
    0
  • 使用批处理异地备份数据

    使用批处理异地备份数据

    2021年7月26日
    65

发表回复

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

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