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


相关推荐

  • 1192啥意思_拓扑排序怎么排

    1192啥意思_拓扑排序怎么排由于无敌的凡凡在2005年世界英俊帅气男总决选中胜出,Yali Company总经理Mr.Z心情好,决定给每位员工发奖金。公司决定以每个人本年在公司的贡献为标准来计算他们得到奖金的多少。于是Mr.Z下令召开 m 方会谈。每位参加会谈的代表提出了自己的意见:“我认为员工 a 的奖金应该比 b 高!”Mr.Z决定要找出一种奖金方案,满足各位代表的意见,且同时使得总奖金数最少。每位员工奖金最少为100元,且必须是整数。输入格式第一行包含整数 n,m,分别表示公司内员工数以及参会代表数。接下来 m

    2022年8月10日
    3
  • wxpython入门教程_wxPython 入门教程

    wxpython入门教程_wxPython 入门教程这篇文章是关于wxPython,但wxPython实际是两件事物的组合体:Python脚本语言和GUI功能的wxWindows库(关于wxWindows的介绍,请参阅developerWorks上的“细述wxWindows”)。wxWindows库是为了最大可移植性的C/C++库,而抽取GUI功能。所以wxWindows应用程序与生俱来地可以运行在Win…

    2022年5月21日
    22
  • springmvc 适配器详解[通俗易懂]

    springmvc 适配器详解[通俗易懂]大家知道springmvc是一个非常优良的框架,配置灵活实现简单,只需我们更多的关注我们的业务逻辑。今天我们就通过一个简单的例子模拟适配生成过程。处理器适配器HandlerAdapter1、SimpleControllerHandlerAdapter表示所有实现了org.springframework.web.servlet.mvc.Controller接口的Bean可以作

    2022年5月12日
    40
  • 十大排序算法简单讲解

    十大排序算法简单讲解

    2021年9月28日
    47
  • 分布式缓存redis_rocksdb 分布式缓存

    分布式缓存redis_rocksdb 分布式缓存WindowsServerAppFabric首页http://msdn.microsoft.com/zh-cn/windowsserver/ee695849(en-us).aspx高可用性(WindowsServerAppFabric缓存)http://msdn.microsoft.com/zh-cn/library/ee790974.aspx www.nhibe…

    2022年10月16日
    3
  • Python自动化面试题(自动化测试面试基础问题)

    Python自动化测试面试题目汇总1、super是干嘛用的?在Python2和Python3使用,有什么区别?为什么要使用super?请举例说明。答:super用于继承父类的方法、属性。 super是新式类中才有的,所以Python2中使用时,要在类名的参数中写Object。Python3默认是新式类,不用写,直接可用。 使用super可以提高代码的复用性、可维护性。修改代码时,只需修改一处。 代码举例:classbaseClass:def.

    2022年4月12日
    71

发表回复

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

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