原创

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个小时。
正文到此结束