문제

I need some advice from you more experienced coders with an issue I am having.

I created a Java SE application which deals with data stored locally on a MySQL server. It works fine where there are small number or records but when i start to pull large number of records , it sometimes freezes the app for a few seconds until it gets all the data.

I used a DAO pattern and made sure to close the connection each time I am done with it. I used prepared statement 99% of the time (the only times i didn't used it are not connected to the parts of the program that freeze up)

I tried to load records in small batches, that cuts down on freezing a bit but its still quite a problem for me.

I checked the log in NetBeans when the application freezes up and didn't see any errors, only the stuff like some system.out.println i put in the various loops and functions processing the data. I just see the NetBeans output log with the system.out.println text scrolling up as the app works in the back ground while the GUI is frozen.

I want to know if there is anything I can do to stop the freezing or at least reduce it ?

I had been thinking of using an API like c3p0 but I wanted to ask your opinions first.

Here is a sample of my code from the GUI part

    fx_trade_model ftm = new fx_trade_model();
    ArrayList all2 = ftm.loadall_B(t);

and here is the load all_B function from the fx trade model class which extends my DAO class :

    public ArrayList loadall_B(int x) throws SQLException {
    ArrayList g = new ArrayList();
    connect = Get_Conn();
    statement = connect.createStatement();
    resultSet = statement.executeQuery("select * from fx_trade  limit "+ x +" ");
    //preparedStatement.setInt(1, x);
    while (resultSet.next()) {

        fx_trade t = new fx_trade();
        t.id = resultSet.getInt("id");
        t.BS = resultSet.getString("BS");
        t.account_id = resultSet.getInt("Account_id");
        t.comm = resultSet.getDouble("comm");
        t.fr = resultSet.getString("fr");
        t.to = resultSet.getString("to");
        t.price = resultSet.getDouble("price");
        t.amount = resultSet.getInt("amount");
        t.settle_date = resultSet.getDate("Settle_Date");
        t.trade_date = resultSet.getDate("Trade_Date");
        t.note = resultSet.getString("note");
        //get qty in stock
        t.qty = get_qty(t.id);
        g.add(t);
    }
    connect.close();
    return g;
}
도움이 되었습니까?

해결책

What you're seeing here is the database taking lot of time (?) to resolve your query because you're loading lot of records. This call is a synchronized call, so the application will wait until the database could generate a proper response with all the results.

What do to for these cases?

Usually, you execute this slow work in another thread, and show to the user a nice Loading message/gif/animation/etc. After the background thread finishes its work, it retrieves the data back to the main thread and continue with the current work to do.

다른 팁

You could eliminate the freezing by creating a new thread to load the data. If you want the user to wait for the data, you could provide a progress bar or spinner to show it is loading. Otherwise, you could let the user interact with the UI while the data loads in the background.

라이센스 : CC-BY-SA ~와 함께 속성
제휴하지 않습니다 StackOverflow
scroll top