user14071617
user14071617

Reputation:

How to keep the connection to the MySQL database?

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.

Upvotes: 1

Views: 367

Answers (1)

DEV
DEV

Reputation: 1726

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);

Upvotes: 1

Related Questions