'Remote SQL connection stops working every few hours

Started working with SQL databases recently and am relatively new to it so bear with me.

I have a database on a 4GB raspberry pi and am storing logs on that database. Using my computer I access that database using java and process it.

Everything was working fine until 2 days ago, when there was some trouble with the internet, which stopped the logging of information on the pi. After sorting out that problem, my PC started to give me error messages every few hours that it was unable to connect to the database on the pi. Even the MySQL workbench stops connecting to that database when this happens.

I also use VNC viewer to access my pi from the PC, and since that problem began, I have been unable to connect to the pi locally using IP, but am able to view it through my account.

The code in java uses try with resources for opening and closing SQL connections, and I only open the connections for as long as necessary each time. This is a sample of the code I use for connecting each time :

try ( Connection con = DriverManager.getConnection("jdbc:mysql://" + SQLurl + ":3306/" + SQLDataBase, SQLUserName, SQLPassword)) {
    try ( PreparedStatement pstmt = con.prepareStatement(statement)) {
                pstmt.executeUpdate();
    } catch (SQLException ex) {
        Logger.getLogger(DiscordBotFunctions.class.getName()).log(Level.SEVERE, null, ex);
    }
} catch (SQLException ex) {
    Logger.getLogger(DiscordBotFunctions.class.getName()).log(Level.SEVERE, null, ex);
}

So far, I have fixed the problem by stopping the programs, putting the computer on airplane mode for a bit and then connecting it to the internet and running them again, but I want the programs to run even when I am busy with other things and not around to monitor it.

Can anyone offer insight as to why this may be happening?

**Obviously connecting to the database is through the local IP of the device (192.168.1.*)



Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source