程序在运行的抛出这个异常,查看cause by里面的原因,增加了autoReconnect=true属性,"jdbc:mysql://"+ip+":"+port+"/"+dbName+"?user="+userName+"&password="+password+"&useUnicode=yes&autoReconnect=true&characterEncoding=UTF-8";
详细异常见面具体信息java.lang.reflect.InvocationTargetException
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(Unknown Source)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(Unknown Source)
at java.lang.reflect.Method.invoke(Unknown Source)
at db.DBUtil.directInvokeAction(DBUtil.java:62)
at db.DBUtil.invokeAction(DBUtil.java:48)
at com.serva.server.db.DBDao.invokeAction(DBDao.java:638)
at com.serva.server.db.DBDao.getDefaultValueByBoProperty(DBDao.java:144)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(Unknown Source)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(Unknown Source)
at java.lang.reflect.Method.invoke(Unknown Source)
at com.serva.server.SystemDaemon.dealwithBusFrame(SystemDaemon.java:235)
at com.serva.server.BusServer.callback(BusServer.java:297)
at com.serva.server.BusServer.access$5(BusServer.java:293)
at com.serva.server.BusServer$3.run(BusServer.java:263)
at java.lang.Thread.run(Unknown Source)
Caused by: com.mysql.jdbc.exceptions.jdbc4.CommunicationsException: The last packet successfully received from the server was 48,242,281 milliseconds ago.  The last packet sent successfully to the server was 48,242,281 milliseconds ago. is longer than the server configured value of 'wait_timeout'. You should consider either expiring and/or testing connection validity before use in your application, increasing the server configured values for client timeouts, or using the Connector/J connection property 'autoReconnect=true' to avoid this problem.
at sun.reflect.NativeConstructorAccessorImpl.newInstance0(Native Method)
at sun.reflect.NativeConstructorAccessorImpl.newInstance(Unknown Source)
at sun.reflect.DelegatingConstructorAccessorImpl.newInstance(Unknown Source)
at java.lang.reflect.Constructor.newInstance(Unknown Source)
at com.mysql.jdbc.Util.handleNewInstance(Util.java:411)
at com.mysql.jdbc.SQLError.createCommunicationsException(SQLError.java:1121)
at com.mysql.jdbc.MysqlIO.send(MysqlIO.java:3871)
at com.mysql.jdbc.MysqlIO.sendCommand(MysqlIO.java:2484)
at com.mysql.jdbc.MysqlIO.sqlQueryDirect(MysqlIO.java:2664)
at com.mysql.jdbc.ConnectionImpl.execSQL(ConnectionImpl.java:2809)
at com.mysql.jdbc.ConnectionImpl.execSQL(ConnectionImpl.java:2758)
at com.mysql.jdbc.StatementImpl.executeQuery(StatementImpl.java:1612)
at db.DBAccess.executeQuerySql(DBAccess.java:236)
... 17 more
Caused by: java.net.SocketException: Software caused connection abort: socket write error
at java.net.SocketOutputStream.socketWrite0(Native Method)
at java.net.SocketOutputStream.socketWrite(Unknown Source)
at java.net.SocketOutputStream.write(Unknown Source)
at java.io.BufferedOutputStream.flushBuffer(Unknown Source)
at java.io.BufferedOutputStream.flush(Unknown Source)
at com.mysql.jdbc.MysqlIO.send(MysqlIO.java:3852)
... 23 more
java.lang.reflect.InvocationTargetException
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(Unknown Source)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(Unknown Source)
at java.lang.reflect.Method.invoke(Unknown Source)
at db.DBThread.run(DBThread.java:33)
并在代码中加了判断,见下面代码,判断Connection.isClosed,如果连接关闭重新创建连接public Connection getDirectConnection(String database){
synchronized(directConns){
try {
Connection conn = directConns.get(database);
if(conn.isClosed()){
reconnect(database);

return directConns.get(database);
} catch (SQLException e) {
e.printStackTrace();
}
return null;
}
}保存后,又抛出异常
Caused by: java.sql.SQLException: Communication link failure: java.net.SocketException, underlying cause: Software caused connection abort: recv failed
找原因说这个异常是由于连接池的连接没有被释放,而MySQL默认的空闲等待时间是8个小时,也就是28800秒,一旦空闲超过8个小时,就会抛出异常。在my.ini文件末尾添加interactive_timeout=288000,实际上是将MySQL默认的空闲等待时间设置为80小时,这样暂时就没问题了。但是在my.ini里添加后,还是报这个错。我想知道报这些错的原因具体是什么,是以上的原因吗?如果按上面的说,我在connect长时间没有被使用后,将其释放,是否是这样java.lang.reflect.InvocationTargetException
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(Unknown Source)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(Unknown Source)
at java.lang.reflect.Method.invoke(Unknown Source)
at db.DBUtil.directInvokeAction(DBUtil.java:62)
at db.DBUtil.invokeAction(DBUtil.java:48)
at com.serva.server.db.DBDao.invokeAction(DBDao.java:659)
at com.serva.server.db.DBDao.findModules(DBDao.java:84)
at com.serva.server.db.DBDao.getModuleMap(DBDao.java:39)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(Unknown Source)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(Unknown Source)
at java.lang.reflect.Method.invoke(Unknown Source)
at com.serva.server.SystemDaemon.dealwithBusFrame(SystemDaemon.java:235)
at com.serva.server.BusServer.callback(BusServer.java:297)
at com.serva.server.BusServer.access$5(BusServer.java:293)
at com.serva.server.BusServer$3.run(BusServer.java:263)
at java.lang.Thread.run(Unknown Source)
Caused by: java.sql.SQLException: Communication link failure: java.net.SocketException, underlying cause: Software caused connection abort: recv failed** BEGIN NESTED EXCEPTION ** java.net.SocketException
MESSAGE: Software caused connection abort: recv failedSTACKTRACE:java.net.SocketException: Software caused connection abort: recv failed
at java.net.SocketInputStream.socketRead0(Native Method)
at java.net.SocketInputStream.read(Unknown Source)
at java.net.SocketInputStream.read(Unknown Source)
at java.io.BufferedInputStream.fill(Unknown Source)
at java.io.BufferedInputStream.read1(Unknown Source)
at java.io.BufferedInputStream.read(Unknown Source)
at com.mysql.jdbc.MysqlIO.readFully(MysqlIO.java:1385)
at com.mysql.jdbc.MysqlIO.reuseAndReadPacket(MysqlIO.java:1532)
at com.mysql.jdbc.MysqlIO.checkErrorPacket(MysqlIO.java:1923)
at com.mysql.jdbc.MysqlIO.sendCommand(MysqlIO.java:1163)
at com.mysql.jdbc.MysqlIO.sqlQueryDirect(MysqlIO.java:1272)
at com.mysql.jdbc.MysqlIO.sqlQuery(MysqlIO.java:1218)
at com.mysql.jdbc.Connection.execSQL(Connection.java:2233)
at com.mysql.jdbc.Connection.execSQL(Connection.java:2181)
at com.mysql.jdbc.Statement.executeQuery(Statement.java:1158)
at db.DBAccess.executeQuerySql(DBAccess.java:236)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(Unknown Source)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(Unknown Source)
at java.lang.reflect.Method.invoke(Unknown Source)
at db.DBUtil.directInvokeAction(DBUtil.java:62)
at db.DBUtil.invokeAction(DBUtil.java:48)
at com.serva.server.db.DBDao.invokeAction(DBDao.java:659)
at com.serva.server.db.DBDao.findModules(DBDao.java:84)
at com.serva.server.db.DBDao.getModuleMap(DBDao.java:39)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(Unknown Source)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(Unknown Source)
at java.lang.reflect.Method.invoke(Unknown Source)
at com.serva.server.SystemDaemon.dealwithBusFrame(SystemDaemon.java:235)
at com.serva.server.BusServer.callback(BusServer.java:297)
at com.serva.server.BusServer.access$5(BusServer.java:293)
at com.serva.server.BusServer$3.run(BusServer.java:263)
at java.lang.Thread.run(Unknown Source)
** END NESTED EXCEPTION **
at com.mysql.jdbc.MysqlIO.reuseAndReadPacket(MysqlIO.java:1707)
at com.mysql.jdbc.MysqlIO.checkErrorPacket(MysqlIO.java:1923)
at com.mysql.jdbc.MysqlIO.sendCommand(MysqlIO.java:1163)
at com.mysql.jdbc.MysqlIO.sqlQueryDirect(MysqlIO.java:1272)
at com.mysql.jdbc.MysqlIO.sqlQuery(MysqlIO.java:1218)
at com.mysql.jdbc.Connection.execSQL(Connection.java:2233)
at com.mysql.jdbc.Connection.execSQL(Connection.java:2181)
at com.mysql.jdbc.Statement.executeQuery(Statement.java:1158)
at db.DBAccess.executeQuerySql(DBAccess.java:236)
... 18 more