MySQLNonTransientConnectionException的处理

mac2024-04-19  5

由于10多年的老代码了,连接数据库的是短连接,所以程序起来后,第二天就报错;

测试环境报错信息如下:

[2019-10-29 09:58:31](5510)ERROR - com.mysql.jdbc.exceptions.jdbc4.MySQLNonTransientConnectionException: No operations allowed after connection closed. com.mysql.jdbc.exceptions.jdbc4.MySQLNonTransientConnectionException: No operations allowed after connection closed. at com.test.fileGen.mcas.sub.SubFileGen.chgSendFlg(SubFileGen.java:72) at com.test.fileGen.mcas.CustomFileGenWorker.run(CustomFileGenWorker.java:795) at java.base/java.lang.Thread.run(Thread.java:844) Caused by: com.mysql.jdbc.exceptions.jdbc4.CommunicationsException: Communications link failure The last packet successfully received from the server was 160,088 milliseconds ago. The last packet sent successfully to the server was 67 milliseconds ago. at com.test.fileGen.mcas.CustomFileGenWorker.run(CustomFileGenWorker.java:803) ... 1 more Caused by: java.io.EOFException: Can not read response from server. Expected to read 4 bytes, read 0 bytes before connection was unexpectedly lost. at com.mysql.jdbc.MysqlIO.readFully(MysqlIO.java:3017) at com.mysql.jdbc.MysqlIO.reuseAndReadPacket(MysqlIO.java:3467) at com.mysql.jdbc.MysqlIO.reuseAndReadPacket(MysqlIO.java:3456) at com.mysql.jdbc.MysqlIO.checkErrorPacket(MysqlIO.java:3997) at com.mysql.jdbc.MysqlIO.sendCommand(MysqlIO.java:2468) at com.mysql.jdbc.MysqlIO.sqlQueryDirect(MysqlIO.java:2629) at com.mysql.jdbc.ConnectionImpl.execSQL(ConnectionImpl.java:2719) at com.mysql.jdbc.PreparedStatement.executeInternal(PreparedStatement.java:2155) at com.mysql.jdbc.PreparedStatement.executeUpdate(PreparedStatement.java:2450) at com.mysql.jdbc.PreparedStatement.executeUpdate(PreparedStatement.java:2371) at com.mysql.jdbc.PreparedStatement.executeUpdate(PreparedStatement.java:2355) at com.test.fileGen.mcas.sub.SubFileGen.chgSendFlg(SubFileGen.java:68) at com.test.fileGen.mcas.CustomFileGenWorker.run(CustomFileGenWorker.java:795) ... 1 more

生产上的程序和测试的一样,但生产没有报错,之前在生产上的数据库连接的配置文件中加了一些参数,就把这些参数也配置到测试的数据库连接的配置文件上。前天修改的,昨天看了一下程序正常运行,添加的参数如下:

<URL>jdbc:mysql://127.0.0.1:3306/test?useUnicode=true&amp;characterEncoding=utf8&amp;autoReconnect=true&amp;failOverReadOnly=false&amp;maxReconnects=10&amp;testOnBorrow=true&amp;validationQuery=select 1</URL>

(参数直接的连接不能直接用“;”,而是需要用“&”来代替) 如果有出现类似问题的同学可以尝试一下,我这边是有效的。 具体会报这个错的原因,在其他博客上都有解释,我这里就不做说明了。

最新回复(0)