5

我用java从Mysql中查询了一些记录。但是在某些持续时间的查询中,我遇到了一个使查询失败的问题,但在其他查询中,它查询成功。接下来是错误消息:

com.mysql.jdbc.exceptions.jdbc4.CommunicationsException: Communications link failure

The last packet successfully received from the server was 90 milliseconds ago.  The last packet sent successfully to the server was 1,674 milliseconds ago.
    at sun.reflect.NativeConstructorAccessorImpl.newInstance0(Native Method)
    at sun.reflect.NativeConstructorAccessorImpl.newInstance(NativeConstructorAccessorImpl.java:39)
    at sun.reflect.DelegatingConstructorAccessorImpl.newInstance(DelegatingConstructorAccessorImpl.java:27)
    at java.lang.reflect.Constructor.newInstance(Constructor.java:513)
    at com.mysql.jdbc.Util.handleNewInstance(Util.java:411)
    at com.mysql.jdbc.SQLError.createCommunicationsException(SQLError.java:1116)
    at com.mysql.jdbc.MysqlIO.reuseAndReadPacket(MysqlIO.java:3090)
    at com.mysql.jdbc.MysqlIO.reuseAndReadPacket(MysqlIO.java:2979)
    at com.mysql.jdbc.MysqlIO.checkErrorPacket(MysqlIO.java:3520)
    at com.mysql.jdbc.MysqlIO.checkErrorPacket(MysqlIO.java:935)
    at com.mysql.jdbc.MysqlIO.nextRow(MysqlIO.java:1433)
    at com.mysql.jdbc.MysqlIO.readSingleRowSet(MysqlIO.java:2924)
    at com.mysql.jdbc.MysqlIO.getResultSet(MysqlIO.java:477)
    at com.mysql.jdbc.MysqlIO.readResultsForQueryOrUpdate(MysqlIO.java:2619)
    at com.mysql.jdbc.MysqlIO.readAllResults(MysqlIO.java:1788)
    at com.mysql.jdbc.MysqlIO.sqlQueryDirect(MysqlIO.java:2209)
    at com.mysql.jdbc.ConnectionImpl.execSQL(ConnectionImpl.java:2619)
    at com.mysql.jdbc.ConnectionImpl.execSQL(ConnectionImpl.java:2569)
    at com.mysql.jdbc.StatementImpl.executeQuery(StatementImpl.java:1521)
    ......
    at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
    at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:39)
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:25)
    at java.lang.reflect.Method.invoke(Method.java:597)
    at org.eclipse.jdt.internal.jarinjarloader.JarRsrcLoader.main(JarRsrcLoader.java:58)
Caused by: java.io.IOException: Packets received out of order
    at com.mysql.jdbc.MysqlIO.readRemainingMultiPackets(MysqlIO.java:3152)
    at com.mysql.jdbc.MysqlIO.reuseAndReadPacket(MysqlIO.java:3077)
    ... 23 more

我尝试了一些方法,例如:

  • 设置 max_allowed_packet = 128 * 1024 * 1024/etc/my.conf
  • 添加 ?autoReconnect=true&failOverReadOnly=false&maxReconnects=10到我的连接网址

但什么也没发生。

我的环境是:

  • mysql: 5.5.3-m3-log 源码分布
  • 爪哇:1.6.0_16
  • Jdk:HotSpot(TM) 64 位服务器 VM(内部版本 14.2-b01,混合模式)
  • JDBC:mysql-connector-java-5.1.18
4

2 回答 2

6

问题解决了。因为结果太大了。在我的查询中,我使用了默认游标,即客户端游标,这意味着 SELECT 的整个结果记录集都返回给客户端(应用程序)并在那里完成分页。所以总的结果集太大,导致 jdbc 客户端内存不足。解决方案是:

  1. 在 JDBC URL 配置参数中添加“useCursorFetch=true”
  2. 调用 statement.setFetchSize(100)

您可以从以下网址阅读更多详细信息:http ://wiki.gxtechnical.com/commwiki/servlet/hwiki?Client%20and%20server%20cursors%20-%20using%20MySQL

于 2012-10-25T09:26:55.893 回答
0

当您尝试使用陈旧的连接提交时,可能会发生这种情况。在提交之前尝试检查连接状态。

于 2012-10-24T15:09:05.423 回答