Bartosz Sochacki
Bartosz Sochacki

Reputation: 13

Slow connection after connection to database with web-hosting

Heading ##I have problem with my java application with database in mySQL and swing GUI.

When I've used localhost everything worked properly. But now I would like to share project with my friend and we decided to use server hosting. And here is a problem: Now application works very slow, after pressing a button I have to wait a few seconds for the program to respond. Also the connection is lost from time to time. I have no idea where can I find reason for the problem... Do somebody now what is the reason of this problem?

    private static Connection connection = null;
    Boolean error = false;
    private static String jdbcURL = "jdbc:mysql://host_name:3306/db_name";
    private static String user = "user";
    private static String password = "password";

    MakeConnection(Connection connection) throws SQLException{
        this.connection = connection;
    try {
        getConnection();
        System.out.print("Connected to the data base in MakeConnection");       
        }
        catch(Exception e) {
            error = true;
            System.out.print("Error in connection in MakeConnection consturctor" + e.getMessage());
            }

        finally{
        if(connection!=null)            connection.close();
        if(error)                       System.out.print("Problem with connection");
        else                            System.out.print("Program finished");
        }
    }
    public Connection getConnection() throws SQLException {
        try {
            Class.forName("com.mysql.cj.jdbc.Driver");
            connection = DriverManager.getConnection(jdbcURL,user,password);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return connection;
    }
}

Also sometimes application shows this error: The last packet sent successfully to the server was 0 milliseconds ago. The driver has not received any packets from the server.

Upvotes: 0

Views: 209

Answers (1)

Erik Maruškin
Erik Maruškin

Reputation: 146

I don't see any problem in your code. The problem is probably with your server hosting. You should check the country of the host provider and measure the time required to send a request to the server. Also you should use logger instead of System.out.println so you can examine required time for actions like db access, application logic and find a bottleneck.

Upvotes: 0

Related Questions