mysql remote database slow with java swing application

4

I have a java swing application that is connected to a remote Mysql database, ie on an online server.

When I run the program it works fine, except for the slowness of the bank's response. For example, I open the user registration window and it takes about 20 seconds to open. this problem did not happen when I was with the local bank.

Here is the code that connects to the bank:

public java.sql.Connection conectaBanco() {
    Connection conn = null; //pro compilador ficar feliz
    try {
        // Carrega o driver JDBC 
        String driverName = "com.mysql.jdbc.Driver";   
        Class.forName(driverName);
        System.out.println("ok");
        // Configuração da conexão com um Conexao1 de dados//
        //troque por seu ip, senha, user, etc
        String serverName = "aqui vai o server onde o banco está";

        //caminho do servidor do BD - para acesso local coloque : localhost
        String mydatabase ="nome_do_banco";        //nome do seu banco de dados 
        String url = "jdbc:mysql://" + serverName + "/" + mydatabase;
        System.out.println("ok");
        String user = "nome_do_usuario";        //nome de um usuário de seu BD      
        String key = "senha";      //sua senha de acesso 
        conn = DriverManager.getConnection(url, user, key);
        //Testa sua conexão//
        System.out.println("Conectado!");
        return conn; 
    } catch (ClassNotFoundException e) {  //Driver não encontrado 
        System.out.println("O driver expecificado nao foi encontrado.");                
    } catch (SQLException e) {
        //Não conseguindo se conectar ao Conexao1
        System.out.println("Nao foi possivel conectar ao Banco de Dados.");            
    }
    return conn;
}

One of the queries:

SELECT * FROM TableName

    
asked by anonymous 17.09.2015 / 01:59

1 answer

4

With the information you have passed so far, unfortunately it is not possible to find a "guilty" for your 15-second requests.

However, we can list some basic ideas that should be considered when moving a local application to web :

  • How many requests does the application make?

It's important to remember that on the internet things are infinitely slower than locally. On most connections, the data needs to be sent, and must be acknowledged. It is no wonder that whitespaces of pages HTML and documents CSS are removed - every byte imports.

Reduce the number of requests as much as you can.

  • How big is the request being made?

It's very easy to make the mistake of asking for unnecessary records from a bank and thereby greatly increase data traffic on the network.

Use the tools that SQL makes available, and make requests that return exactly the information you need. Do not handle data received from banks in your application. Banks are built for this and are very good at what they do.

  • Requests are blocking ?

This is an important idea for online applications that is easy to pass up when working locally. An application that expects a database request to return to load windows / menus / etc, greatly damages the user experience.

Try to put time-consuming tasks into separate processes, and remember to warn the user that something is happening in the background.

If you already have these ideas implemented, I suggest you make a connection with prompt de comando with your remote database and check the time it takes to connect and make requests.

If the connection is much faster, it may be worth including the rest of your code for people to take a look at.

If it is slow, I would take this issue to the hosting service and consider the change of service.

Attention: Consider how many records you have in the database - a SELECT * FROM Tabela as in your example, if used in a database like facebook, you can simply never return .

PS. When I asked for the code, I meant what the SQL statement encompasses, not the statement itself.

    
20.09.2015 / 09:04