Java 为什么 autoReconnect=true 似乎不起作用?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/667289/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Why does autoReconnect=true not seem to work?
提问by Pyrolistical
I am using JDBC to connect to a MySQL server (no connection pooling I think). In the connection URL I have autoReconnect=true
我正在使用 JDBC 连接到 MySQL 服务器(我认为没有连接池)。在连接 URL 我有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.
但是我的连接仍然超时。我什至检查过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 ...
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?
采纳答案by Eric Petroelje
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. Alternatively, investigate setting the MySQL server variable "wait_timeout" to some high value rather than the default of 8 hours.
驱动程序是否应该尝试重新建立陈旧和/或死连接?如果启用,驱动程序将对在旧或死连接上发出的查询抛出异常,这些查询属于当前事务,但将在新事务中的连接上发出下一个查询之前尝试重新连接。不推荐使用此功能,因为当应用程序不能正确处理 SQLExceptions 时,它会产生与会话状态和数据一致性相关的副作用,并且仅在您无法配置应用程序以处理因死机导致的 SQLExceptions 时使用它和陈旧的连接正确。或者,调查将 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 linkprovides a bit more information, and indicates that autoReconnect will probably be removed in the future anyways.
ETA:此链接提供了更多信息,并表明将来可能会删除 autoReconnect。
回答by chaos
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.)
autoReconnect 仍然会抛出异常,因此您可以根据需要选择对此情况进行处理。如果你抓住它,你应该会发现连接又在那里。(如果您在进行交易,则会更加复杂——您当前的交易几乎已经死了。)