为什么 autoReconnect=true 似乎不起作用?

发布于 2024-07-15 05:23:19 字数 616 浏览 1 评论 0原文

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

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

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
...

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

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

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?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

谈情不如逗狗 2024-07-22 05:23:19

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

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

根据我的经验,“在下一个查询时重新连接”功能似乎也不起作用,但是我使用的是 MySQL 4.0,这可能就是原因。

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

预计到达时间此链接提供了更多信息,并表明 autoReconnect 将来可能会被删除。

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.

長街聽風 2024-07-22 05:23:19

autoReconnect 仍然会抛出异常,因此您可以根据需要选择对这种情况采取一些措施。 如果你抓住了它,你应该会发现随后又恢复了连接。 (如果你正在进行交易,那么情况会更加复杂——你当前的交易几乎已经死了。)

autoReconnect still throws the exception so you can choose to do something about the situation if you like. If you catch it, you should find that the connection is there again afterward. (There's some more complexity if you're in a transaction -- your current transaction is pretty much dead.)

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文