为什么 autoReconnect=true 似乎不起作用? [英] Why does autoReconnect=true not seem to work?

查看:77
本文介绍了为什么 autoReconnect=true 似乎不起作用?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我使用 JDBC 连接到 MySQL 服务器(我认为没有连接池).在连接 URL 我有 autoReconnect=true

但我的连接仍然超时.我什至检查了 conn.isClosed() 和它的错误.但是当我尝试使用连接时,出现以下异常.

<前>com.mysql.jdbc.CommunicationsException:由于底层异常导致通信链接失败:** 开始嵌套异常 **java.net.SocketException消息:软件导致连接中止:套接字写入错误堆栈跟踪:java.net.SocketException:软件导致连接中止:套接字写入错误...

我知道在 Java 1.6 中您可以使用 conn.isValid(0) 来检查连接,但我使用的是 Java 1.5

有没有办法确保它不会超时?还是我必须升级到 Java 1.6?

解决方案

我遇到了同样的问题,这绝对令人抓狂.这是 MySQL 网站上的文档所说的内容(重点是我的)

驱动程序是否应该尝试重新建立陈旧和/或失效的连接?如果启用,驱动程序将对在陈旧或死连接上发出的查询抛出异常,这些查询属于当前事务,但会在新事务中的连接上发出下一个查询之前尝试重新连接.不推荐使用此功能,因为当应用程序没有正确处理 SQLExceptions 时,它会产生与会话状态和数据一致性相关的副作用,并且仅在您无法配置应用程序以处理因死机导致的 SQLExceptions 时使用它和陈旧的连接正确.或者,调查将 MySQL 服务器变量wait_timeout"设置为某个较高的值,而不是默认值 8 小时.

根据我的经验,在下一个查询时重新连接"功能似乎也不起作用,但我使用的是 MySQL 4.0,这可能是造成这种情况的原因.

我最终编写了一个微型框架来捕获异常,检查该特定错误,并在可能的情况下尝试重新连接并重试查询.

预计到达时间:这个链接提供了一点更多信息,并表明将来可能会删除 autoReconnect.

I am using JDBC to connect to a MySQL server (no connection pooling I think). In the connection URL I have autoReconnect=true

But my connection still times out. I've even checked conn.isClosed() and its false. But when I try to use the connection I get the following exception.

com.mysql.jdbc.CommunicationsException: Communications link failure due to underlying exception: 

** BEGIN NESTED EXCEPTION ** 

java.net.SocketException
MESSAGE: Software caused connection abort: socket write error

STACKTRACE:

java.net.SocketException: Software caused connection abort: socket write error
...

I know in Java 1.6 you can use conn.isValid(0) to check the connection, but I am using Java 1.5

Is there a way to either ensure it doesn't time out? Or am I going to have to upgrade to Java 1.6?

解决方案

I had the same issue and it was absolutely maddening. Here's what the docs say on the MySQL website (emphasis mine)

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 do not 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, investigate setting the MySQL server variable "wait_timeout" to some high value rather than the default of 8 hours.

In my experience, it doesn't seem like the "reconnect on the next query" functionality worked either, but I was using MySQL 4.0, which may have been the reason for that.

I ended up writing a mini-framework that catches the exceptions, checks for that specific error, and attempts to reconnect and retry the query if possible.

ETA: This link provides a bit more information, and indicates that autoReconnect will probably be removed in the future anyways.

这篇关于为什么 autoReconnect=true 似乎不起作用?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆