com.mysql.jdbc.exceptions.jdbc4.CommunicationsException: The last packet successfully received from the server was51093 seconds ago.The last packet sent successfully to the server was 51093 seconds ago, which 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(NativeConstructorAccessorImpl.java:39)
at sun.reflect.DelegatingConstructorAccessorImpl.newInstance(DelegatingConstructorAccessorImpl.java:27)
at java.lang.reflect.Constructor.newInstance(Constructor.java:513)
at com.mysql.jdbc.Util.handleNewInstance(Util.java:406)
at com.mysql.jdbc.SQLError.createCommunicationsException(SQLError.java:1074)
at com.mysql.jdbc.MysqlIO.send(MysqlIO.java:3246)
at com.mysql.jdbc.MysqlIO.sendCommand(MysqlIO.java:1917)
at com.mysql.jdbc.MysqlIO.sqlQueryDirect(MysqlIO.java:2060)
at com.mysql.jdbc.ConnectionImpl.execSQL(ConnectionImpl.java:2536)
at com.mysql.jdbc.StatementImpl.executeUpdate(StatementImpl.java:1564)
at com.mysql.jdbc.StatementImpl.executeUpdate(StatementImpl.java:1485)
我已经按照提示加了autoReconnect=true,问题依然系统: redhat as 5连接字符串为jdbc:mysql://127.0.0.1:3306/meta?user=root&password=000000&autoReconnect=true&characterEncoding=utf-8请指点~~~~~~~~~~
减少Connection pools中connection的lifetime。
测试Connection pools中connection的有效性。
当然最好的办法是同时综合使用上述3种方法,下面举个例子,假设wait_timeout为默认的8小时C3P0增加以下配置信息://获取connnection时测试是否有效
testConnectionOnCheckin = true
//自动测试的table名称automaticTestTable=C3P0TestTable//set to something much less than wait_timeout, prevents connections from going stale
idleConnectionTestPeriod = 18000
//set to something slightly less than wait_timeout, preventing 'stale' connections from being handed out
maxIdleTime = 25000
//if you can take the performance 'hit', set to "true"
testConnectionOnCheckout = true
2. 检查是不是有防火墙
我前面做了一个网站也有这样的问题,我是用C3P0解决的。就是隔一段时间查找数据库,就相当于隔一段时间连接一样,这样就不会出现8小时的问题了。
if (conn==null||conn.isClosed())
重新获取conn。虽然每次都执行,但保证不出错。
这个我也加了的 ,还是会出现,我干脆把代码贴出来
import java.sql.*;
import Core.Tr.table.table_api;
public class dbclass {
private Connection conn = null;
private String driverStr = null;
private String connStr = null;
private String uName = null;
private String uPwd = null; public dbclass(table_api t) {
driverStr = "com.mysql.jdbc.Driver";
connStr = "jdbc:mysql://" + t.configTable.get("meta_host") + "/" +t.configTable.get("meta_db") +"?autoReconnect=true&useUnicode=true&characterEncoding=utf-8";
uName = t.configTable.get("meta_user");
uPwd = t.configTable.get("meta_pwds");
} public Connection getConnection() {
try {
Class.forName(driverStr).newInstance();
return java.sql.DriverManager.getConnection(connStr, uName,uPwd);
} catch (Exception e) {
e.printStackTrace();
}
return null;
} public void close() {
try {
if (conn != null && !conn.isClosed()) {
conn.close();
}
} catch (SQLException se) {
se.printStackTrace();
}
} public void Execute(String sqlstr) {
try {
if (conn == null || conn.isClosed()) {
conn = getConnection();
}
PreparedStatement statement = null;
statement = conn.prepareStatement(sqlstr);
statement.execute();
statement.close();
statement = null;
} catch (Exception ex) {
ex.printStackTrace();
}
} public ResultSet SqlResultSet(String sql) {
ResultSet rs = null;
PreparedStatement statement = null;
try {
if (conn == null || conn.isClosed()) {
conn = getConnection();
}
statement = conn.prepareStatement(sql);
rs = statement.executeQuery();
return rs;
} catch (Exception ex) {
ex.printStackTrace();
} return null;
}
/*获取表名*/
public int getTableName(String names) {
int number = 0;
try {
if (conn == null || conn.isClosed()) {
conn = getConnection();
}
boolean flag = false;
if ("%".equals(names)) {
flag = true;
} String tname = null;
DatabaseMetaData m_meta = conn.getMetaData();
ResultSet tableRet = m_meta.getTables(null, "%", "%",new String[] {"TABLE"});
while (tableRet.next()) {
tname = tableRet.getString("TABLE_NAME");
if (flag) {
number++;
} else {
if (tname.indexOf(names) != -1) {
number++;
}
}
}
tableRet.close();
tableRet = null;
} catch (Exception se) {
se.printStackTrace();
}
return number;
}}