由於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&characterEncoding=utf8&autoReconnect=true&failOverReadOnly=false&maxReconnects=10&testOnBorrow=true&validationQuery=select 1</URL>
(參數直接的連接不能直接用“;”,而是需要用“&”來代替)
如果有出現類似問題的同學可以嘗試一下,我這邊是有效的。
具體會報這個錯的原因,在其他博客上都有解釋,我這裏就不做說明了。