从MySQL,JDBC中使用大型ResultSet无序接收数据包

时间:2014-06-20 20:55:38

标签: java mysql jdbc

我遇到了一个问题,我现在几天都无法解决这个问题。所以,我试图用解决方案逐行列出一个大型结果集,在互联网上多次引用,包括stackoverflow

Statement stmt = readOnlyConn.createStatement(java.sql.ResultSet.TYPE_FORWARD_ONLY, java.sql.ResultSet.CONCUR_READ_ONLY);

stmt.setFetchSize(Integer.MIN_VALUE);

它似乎有效。至少有一段时间(它将行写入文本文件),直到我得到下面的错误。我搜索了我的眼睛试图找到可能导致这种情况,尝试了一些mysql服务器调整,没有任何作用。谁能帮帮我吗?每个人都在用上面的解决方案报告出色的结果,但它似乎特别讨厌我。我非常感谢任何帮助。

    java.lang.RuntimeException: com.mysql.jdbc.exceptions.jdbc4.CommunicationsException: Communications link failure

The last packet successfully received from the server was 15 milliseconds ago.  The last packet sent successfully to the server was 6,239 milliseconds ago.
    at gblaster.main.lambda$main$1(main.java:134)
    at gblaster.main$$Lambda$3/792791759.accept(Unknown Source)
    at java.util.ArrayList$ArrayListSpliterator.forEachRemaining(ArrayList.java:1359)
    at java.util.stream.ReferencePipeline$Head.forEach(ReferencePipeline.java:580)
    at gblaster.main.main(main.java:124)
    at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
    at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
    at java.lang.reflect.Method.invoke(Method.java:483)
    at com.intellij.rt.execution.application.AppMain.main(AppMain.java:120)
Caused by: com.mysql.jdbc.exceptions.jdbc4.CommunicationsException: Communications link failure

The last packet successfully received from the server was 15 milliseconds ago.  The last packet sent successfully to the server was 6,239 milliseconds ago.
    at sun.reflect.NativeConstructorAccessorImpl.newInstance0(Native Method)
    at sun.reflect.NativeConstructorAccessorImpl.newInstance(NativeConstructorAccessorImpl.java:62)
    at sun.reflect.DelegatingConstructorAccessorImpl.newInstance(DelegatingConstructorAccessorImpl.java:45)
    at java.lang.reflect.Constructor.newInstance(Constructor.java:408)
    at com.mysql.jdbc.Util.handleNewInstance(Util.java:411)
    at com.mysql.jdbc.SQLError.createCommunicationsException(SQLError.java:1129)
    at com.mysql.jdbc.MysqlIO.reuseAndReadPacket(MysqlIO.java:3720)
    at com.mysql.jdbc.MysqlIO.reuseAndReadPacket(MysqlIO.java:3609)
    at com.mysql.jdbc.MysqlIO.checkErrorPacket(MysqlIO.java:4160)
    at com.mysql.jdbc.MysqlIO.checkErrorPacket(MysqlIO.java:928)
    at com.mysql.jdbc.MysqlIO.nextRow(MysqlIO.java:2053)
    at com.mysql.jdbc.MysqlIO.readSingleRowSet(MysqlIO.java:3554)
    at com.mysql.jdbc.MysqlIO.getResultSet(MysqlIO.java:491)
    at com.mysql.jdbc.MysqlIO.readResultsForQueryOrUpdate(MysqlIO.java:3245)
    at com.mysql.jdbc.MysqlIO.readAllResults(MysqlIO.java:2413)
    at com.mysql.jdbc.ServerPreparedStatement.serverExecute(ServerPreparedStatement.java:1500)
    at com.mysql.jdbc.ServerPreparedStatement.executeInternal(ServerPreparedStatement.java:857)
    at com.mysql.jdbc.PreparedStatement.executeQuery(PreparedStatement.java:2313)
    at db.mysql.GMySQLConnector.loadAllOrfsForGenomeId(GMySQLConnector.java:296)
    at gblaster.deploy.Deployer.unloadORFsForGenomeToFile(Deployer.java:124)
    at gblaster.main.lambda$main$1(main.java:128)
    ... 9 more
Caused by: java.io.IOException: Packets received out of order
    at com.mysql.jdbc.MysqlIO.readRemainingMultiPackets(MysqlIO.java:3782)
    at com.mysql.jdbc.MysqlIO.reuseAndReadPacket(MysqlIO.java:3707)
    ... 23 more

1 个答案:

答案 0 :(得分:1)

您的代码很难理解,因为Integer.MIN_VALUE是否定的,

stmt.setFetchSize(Integer.MIN_VALUE);

我认为你会发现

效果更好
stmt.setFetchSize(100); // <-- I also bet this works better then 1. 

最后,Statement.setFetchSize(int) javadoc说(强调添加),

  

<强>抛出

     

SQLException - 如果发生数据库访问错误,则在关闭的Statement 上调用此方法,或者不满足条件行&gt; = 0。

因此,您应该使用代码获得SQLException