MySql的autoReconnect与autoReconnectForPools属性对比

Posted 勤奋的Coder

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了MySql的autoReconnect与autoReconnectForPools属性对比相关的知识,希望对你有一定的参考价值。

autoReconnect会向客户端抛出一个SQLException,但会尝试重新建立连接。
autoReconnectForPools将在每次执行SQL之前尝试ping服务器。
autoReconnect和autoReconnectForPools并没有什么关系。并且mysql官方并不推荐使用autoReconnect属性。


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
官方已经说明了:如果启用这个属性,则MySQL驱动程序将会对当前事务的已过期或者已被废弃的链接发出的查询请求抛出异常,并会在发起下一次请求之前尝试重新建立连接。官方已经不建议使用该属性,该值只建议在应用程序无法正确处理死链或已被回收的链接所导致的SQLException时使用。
解决该问题的最有方法就是调整MySQL的wait_timeout参数,使之大于8个小时。

以上是关于MySql的autoReconnect与autoReconnectForPools属性对比的主要内容,如果未能解决你的问题,请参考以下文章

mysql5 数据库连接丢失问题,autoReconnect=true不起作用

连接数据库超时设置autoReconnect=true

'autoreconnect=true'设置无效,怎么解决

java c3p0:如何配置 autoreconnect=true?

jdbc autoReconnect=true 参数设置导致 slow log 爆表。

mysql 设置允许重试,批量更新