slow remote Mysql database with java swing application

Asked

Viewed 1,033 times

4

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

When running the program it works normal, except for the slow response of the bank. 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 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 consultations:

SELECT * FROM Table Name_table

  • Welcome to Stack Overflow Filipe! I suggest you include the snippet of the code that makes the request to the database - it will give a better idea to the staff than is occurring! :)

  • edited the post, please look at it.

  • 1

    It usually takes a little longer even, locally it is much faster, which server is your bank hosted? You could post this query that takes 20 seconds?

  • +1 pro @Rafaelferreira - Removing the server question, which is usually considered confidential information ;) - Include the 20 seconds query, as that’s probably where the problem is.

  • Look, the server I’m using is hostgator. While the query, it’s all of them! Anything I do takes 15 to 30 seconds! I put a simple SELECT query up there.

  • you are making queries via Linq or lambda?

Show 1 more comment

1 answer

4

With the information you have given us 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 the app is making?

It is important to remember that on the Internet things are infinitely slower than locally. On most connections, data needs to be sent, and must be confirmed. No wonder blank spaces are removed from pages HTML and documents CSS - each byte matter.

Reduce the number of requests as much as you can.

  • What size is the request being made?

It is 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 makes requests that return exactly the information you need. Do not treat data received from banks in your application. The banks are built for this and are very good at what they do.

  • The requisitions are "blocking"?

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

Try to put time-consuming tasks in 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 by 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 might be worth including the rest of your code for the staff to take a look.

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

Heed: Consider the amount of records you have in the bank - a SELECT * FROM Tabela as in your example, if used in a bank like facebook, you can simply never return.

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

Browser other questions tagged

You are not signed in. Login or sign up in order to post.