英文:
How to keep the connection to the MySQL database?
问题
我正在尝试解决与MySQL数据库的连接问题。几个小时后,我的服务器意外关闭了与MySQL数据库的连接。
这是我的错误代码:
com.mysql.jdbc.exceptions.jdbc4.CommunicationsException: 从服务器成功接收到的最后一个数据包已经是 37,521,865 毫秒前的事情了。成功发送到服务器的最后一个数据包也是 37,521,865 毫秒前的事情。这比服务器配置的 'wait_timeout' 值长。您应该考虑在应用程序中在使用之前使连接失效和/或测试连接的有效性,增加服务器配置的客户端超时值,或者使用 Connector/J 连接属性 'autoReconnect=true' 来避免此问题。
这是我的MySQL Java代码:
public class MySQL {
public static String host = "localhost";
public static String port = "3306";
public static String database = "SignSystem";
public static String username = "SignSystem";
public static String password = "12345678910";
public static Connection con;
public static void connect() {
if (!(isConnected())) {
try {
con = DriverManager.getConnection("jdbc:mysql://" + host + ":" + port + "/" + database, username, password);
System.out.println("[MySQL] Connected successfully!");
} catch (SQLException e) {
e.printStackTrace();
}
}
}
public static void disconnect() {
try {
con.close();
} catch (SQLException e) {
e.printStackTrace();
}
}
public static boolean isConnected() {
return (con == null ? false : true);
}
public static void update(String qry) {
if (!(isConnected())) {
try {
con = DriverManager.getConnection("jdbc:mysql://" + host + ":" + port + "/" + database, username, password);
System.out.println("[MySQL] Connected successfully!");
} catch (SQLException e) {
e.printStackTrace();
}
try {
PreparedStatement ps = con.prepareStatement(qry);
ps.executeUpdate();
} catch (SQLException e) {
e.printStackTrace();
}
} else {
try {
PreparedStatement ps = con.prepareStatement(qry);
ps.executeUpdate();
} catch (SQLException e) {
e.printStackTrace();
}
}
}
public static ResultSet getResult(String qry) {
try {
PreparedStatement ps = con.prepareStatement(qry);
return ps.executeQuery();
} catch (SQLException e) {
e.printStackTrace();
}
return null;
}
public static Connection getConnection() {
return con;
}
}
我已经尝试通过在执行任务之前检查数据库是否连接来解决问题。但似乎我的修复方法没有起作用。
如何解决这个错误?我想要自动重新连接或稳定的连接。
英文:
I'm trying to fix a connection problem with the MySQL database. After a few hours my server unexpectly closes the connection to the MySQL database.
This is my error code:
> com.mysql.jdbc.exceptions.jdbc4.CommunicationsException: The last packet successfully received from the server was 37,521,865 milliseconds ago. The last packet sent successfully to the server was 37,521,865 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.
This is my MySQL java code:
public class MySQL {
public static String host = "localhost";
public static String port = "3306";
public static String database = "SignSystem";
public static String username = "SignSystem";
public static String password = "12345678910";
public static Connection con;
public static void connect() {
if (!(isConnected())) {
try {
con = DriverManager.getConnection("jdbc:mysql://" + host + ":" + port + "/" + database, username, password);
System.out.println("[MySQL] Connected successfully!");
} catch (SQLException e) {
e.printStackTrace();
}
}
}
public static void disconnect() {
try {
con.close();
} catch (SQLException e) {
e.printStackTrace();
}
}
public static boolean isConnected() {
return (con == null ? false : true);
}
public static void update(String qry) {
if (!(isConnected())) {
try {
con = DriverManager.getConnection("jdbc:mysql://" + host + ":" + port + "/" + database, username, password);
System.out.println("[MySQL] Connected successfully!");
} catch (SQLException e) {
e.printStackTrace();
}
try {
PreparedStatement ps = con.prepareStatement(qry);
ps.executeUpdate();
} catch (SQLException e) {
e.printStackTrace();
}
} else {
try {
PreparedStatement ps = con.prepareStatement(qry);
ps.executeUpdate();
} catch (SQLException e) {
e.printStackTrace();
}
}
}
public static ResultSet getResult(String qry) {
try {
PreparedStatement ps = con.prepareStatement(qry);
return ps.executeQuery();
} catch (SQLException e) {
e.printStackTrace();
}
return null;
}
public static Connection getConnection() {
return con;
}
}
I have already tried to fix the problem by checking if the database is connected before doing a task.
But it seems that my fix is not working.
How can I fix this error?
I would like to have an automatic reconnect or a stable connection.
答案1
得分: 1
如果您正在寻找稳定的连接,请尝试在您的连接字符串中使用:
con = DriverManager.getConnection("jdbc:mysql://" + host + ":" + port + "/" + database + "?autoReconnect=true", username, password);
英文:
if are u looking for a stable connection try to have on your connection String :
con = DriverManager.getConnection("jdbc:mysql://" + host + ":" + port + "/" + database+"?autoReconnect=true", username, password);
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论