MySQL java jdbc MySQLNonTransientConnectionException:得到的数据包乱序

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了MySQL java jdbc MySQLNonTransientConnectionException:得到的数据包乱序相关的知识,希望对你有一定的参考价值。

我目前无法通过java将多查询发送到mysql数据库。代码是这样的:

String query = "Query 1; Query 2; Query 3;... Query 59" 
stmt = connection.createStatement();
System.out.println(query);
stmt.execute(query);

然后我收到以下错误:

com.mysql.jdbc.exceptions.jdbc4.MySQLNonTransientConnectionException: Got packets out of order
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:423)
at com.mysql.jdbc.Util.handleNewInstance(Util.java:411)
at com.mysql.jdbc.Util.getInstance(Util.java:386)
at com.mysql.jdbc.SQLError.createSQLException(SQLError.java:1014)
at com.mysql.jdbc.MysqlIO.checkErrorPacket(MysqlIO.java:4120)
at com.mysql.jdbc.MysqlIO.checkErrorPacket(MysqlIO.java:4052)
at com.mysql.jdbc.MysqlIO.checkErrorPacket(MysqlIO.java:925)
at com.mysql.jdbc.MysqlIO.sendFileToServer(MysqlIO.java:4017)
at com.mysql.jdbc.MysqlIO.readResultsForQueryOrUpdate(MysqlIO.java:3129)
at com.mysql.jdbc.MysqlIO.readAllResults(MysqlIO.java:2335)
at com.mysql.jdbc.MysqlIO.sqlQueryDirect(MysqlIO.java:2722)
at com.mysql.jdbc.ConnectionImpl.execSQL(ConnectionImpl.java:2788)
at com.mysql.jdbc.ConnectionImpl.execSQL(ConnectionImpl.java:2738)
at com.mysql.jdbc.StatementImpl.execute(StatementImpl.java:899)
at com.mysql.jdbc.StatementImpl.execute(StatementImpl.java:737)
at fileUpload.ICDBoperations.uploadELG(ICDBoperations.java:61)
at com.intuitivecare.icserver.upload.doPost(upload.java:108)
at javax.servlet.http.HttpServlet.service(HttpServlet.java:707)
at javax.servlet.http.HttpServlet.service(HttpServlet.java:790)
at org.apache.catalina.core.StandardWrapper.service(StandardWrapper.java:1682)
at org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:318)
at org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:160)
at org.apache.catalina.core.StandardPipeline.doInvoke(StandardPipeline.java:734)
at org.apache.catalina.core.StandardPipeline.invoke(StandardPipeline.java:673)
at com.sun.enterprise.web.WebPipeline.invoke(WebPipeline.java:99)
at org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:174)
at org.apache.catalina.connector.CoyoteAdapter.doService(CoyoteAdapter.java:357)
at org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:260)
at com.sun.enterprise.v3.services.impl.ContainerMapper.service(ContainerMapper.java:188)
at org.glassfish.grizzly.http.server.HttpHandler.runService(HttpHandler.java:191)
at org.glassfish.grizzly.http.server.HttpHandler.doHandle(HttpHandler.java:168)
at org.glassfish.grizzly.http.server.HttpServerFilter.handleRead(HttpServerFilter.java:189)
at org.glassfish.grizzly.filterchain.ExecutorResolver$9.execute(ExecutorResolver.java:119)
at org.glassfish.grizzly.filterchain.DefaultFilterChain.executeFilter(DefaultFilterChain.java:288)
at org.glassfish.grizzly.filterchain.DefaultFilterChain.executeChainPart(DefaultFilterChain.java:206)
at org.glassfish.grizzly.filterchain.DefaultFilterChain.execute(DefaultFilterChain.java:136)
at org.glassfish.grizzly.filterchain.DefaultFilterChain.process(DefaultFilterChain.java:114)
at org.glassfish.grizzly.ProcessorExecutor.execute(ProcessorExecutor.java:77)
at org.glassfish.grizzly.nio.transport.TCPNIOTransport.fireIOEvent(TCPNIOTransport.java:838)
at org.glassfish.grizzly.strategies.Abstractiostrategy.fireIOEvent(AbstractIOStrategy.java:113)
at org.glassfish.grizzly.strategies.WorkerThreadIOStrategy.run0(WorkerThreadIOStrategy.java:115)
at org.glassfish.grizzly.strategies.WorkerThreadIOStrategy.access$100(WorkerThreadIOStrategy.java:55)
at org.glassfish.grizzly.strategies.WorkerThreadIOStrategy$WorkerThreadRunnable.run(WorkerThreadIOStrategy.java:135)
at org.glassfish.grizzly.threadpool.AbstractThreadPool$Worker.doWork(AbstractThreadPool.java:564)
at org.glassfish.grizzly.threadpool.AbstractThreadPool$Worker.run(AbstractThreadPool.java:544)
at java.lang.Thread.run(Thread.java:745)

该代码适用于1个查询(任何查询)。

我已经设置了allowMultiQueries = true,问题也不是数据包太大的问题,因为我只截断了第3个查询并得到了相同的错误。

有任何想法吗?谢谢!

答案

我在spring boot应用程序中遇到了这个问题,发现“mysql-connector-java”版本5.1.6导致了这个问题。我用5.1.45更新了它并解决了这个问题。

另一答案

我仍然没有答案,但我找到了解决方法。我没有尝试一次执行多个查询并在java中建立事务,而是添加了BEGIN TRANSACTION;查询开头和COMMIT;在末尾。然后我用以下代码执行查询:

stmt = connection.createStatement();
    for(int i=0;i<numQueries;i++){
        stmt.execute(queries[i]);
    }

其中queries []是一个字符串数组,包含我想以适当的顺序执行的查询。这可能不是最佳的,但是,在我的情况下,它做了伎俩。当我在netbeans IDE中执行查询时,我需要大约22秒,而执行上面的代码需要大约25秒。希望人们觉得这很有帮助!

以上是关于MySQL java jdbc MySQLNonTransientConnectionException:得到的数据包乱序的主要内容,如果未能解决你的问题,请参考以下文章

Java JDBC连接MySQL数据库,基础语法及自定义JDBC的MySQL工具类

jdbc 类型 4 -java.lang.ClassNotFoundException:com.mysql.jdbc.Driver [重复]

MySQL数据库 --- Java的JDBC编程

java JDBC

Java_JDBC_MySql

JAVA 访问MySQL数据库 JDBC-MySQL