true似乎不起作用

true似乎不起作用

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

问题描述

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

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

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

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

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

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?

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

推荐答案

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

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

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

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.

根据我的经验,下一个查询重新连接"功能似乎也不起作用,但是我使用的是MySQL 4.0.

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 :此链接提供了一些信息更多信息,并表示将来仍然可能会删除autoReconnect.

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

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

07-25 17:06