简体   繁体   English

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

[英]Why does autoReconnect=true not seem to work?

I am using JDBC to connect to a MySQL server (no connection pooling I think).我正在使用 JDBC 连接到 MySQL 服务器(我认为没有连接池)。 In the connection URL I have autoReconnect=true在连接 URL 我有autoReconnect=true

But my connection still times out.但我的连接仍然超时。 I've even checked conn.isClosed() and its false.我什至检查conn.isClosed()及其错误。 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我知道在 Java 1.6 中您可以使用conn.isValid(0)来检查连接,但我使用的是 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?还是我必须升级到 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)这是 MySQL 网站上的文档所说的(重点是我的)

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.不建议使用此功能,因为它具有与 session state 相关的副作用以及应用程序无法正确处理 SQLExceptions 时的数据一致性,并且仅设计用于当您无法配置应用程序以处理因 dead 导致的 SQLExceptions 时并正确地过时的连接。 Alternatively, investigate setting the MySQL server variable "wait_timeout" to some high value rather than the default of 8 hours.或者,研究将 MySQL 服务器变量“wait_timeout”设置为某个较高的值,而不是默认值 8 小时。

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

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. ETA此链接提供了更多信息,并表明将来可能会删除自动重新连接。

autoReconnect still throws the exception so you can choose to do something about the situation if you like. autoReconnect 仍然会引发异常,因此您可以根据需要选择对这种情况做些什么。 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.) (如果你在一个事务中,会有更多的复杂性——你当前的事务几乎已经死了。)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM