0

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.

  • May I know a region where a database and app server is being hosted? – Bhushan Sep 10 '19 at 17:00
  • PING host_name.com and how long does a simple ping take? Will be an indicator of basic connectivity difficulty. – Wilson Hauck Sep 10 '19 at 18:45
  • Pinging serwer1990494.home.pl [46.242.244.226] with 32 bytes of data: Reply from 46.242.244.226: bytes=32 time=15ms TTL=56 Reply from 46.242.244.226: bytes=32 time=16ms TTL=56 Reply from 46.242.244.226: bytes=32 time=16ms TTL=56 Reply from 46.242.244.226: bytes=32 time=16ms TTL=56 Ping statistics for 46.242.244.226: Packets: Sent = 4, Received = 4, Lost = 0 (0% loss), Approximate round trip times in milli-seconds: Minimum = 15ms, Maximum = 16ms, Average = 15ms – Bartosz Sochacki Sep 11 '19 at 11:54
  • Ping looks good but I have this error in my code: Communications link failure The last packet sent successfully to the server was 0 milliseconds ago. The driver has not received any packets from the server. – Bartosz Sochacki Sep 11 '19 at 11:55

1 Answers1

0

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.

  • I have this error in my code: Communications link failure The last packet sent successfully to the server was 0 milliseconds ago. The driver has not received any packets from the server. – Bartosz Sochacki Sep 11 '19 at 11:56
  • Check one of these https://stackoverflow.com/questions/6865538/solving-a-communications-link-failure-with-jdbc-and-mysql https://stackoverflow.com/questions/2983248/com-mysql-jdbc-exceptions-jdbc4-communicationsexception-communications-link-fai – Erik Maruškin Sep 11 '19 at 13:09
  • okey, thanks. I checked these links and try to fix my problem but i found something strange. In my.ini file I don't have line with "bind-adress" idk why. Should I add this line : bind-adress = 127.0.0.1 by myself or leave it? – Bartosz Sochacki Sep 11 '19 at 17:05
  • I cannot give you certain answer because i don't see your whole server configuration. Try to experiment. Also i recomend you to use logger to have more informations about future errors. – Erik Maruškin Sep 12 '19 at 21:42