0

Scenario:

I had developed a Java/Swing/MySQL application. The following code works fine when both MySQL and Swing application are installed on the same computer.

Code:

public static Connection ConnectDB(){
    try{
        String userName = "root";
        String password = "chen";
        String url = "jdbc:mysql://localhost/javaandmysqltut";
        Connection conn;
        conn = DriverManager.getConnection (url, userName, password);

        //JOptionPane.showMessageDialog(null,"Connection Established");
        return conn;
    }catch (Exception e){
        JOptionPane.showMessageDialog(null,"Cannot connect to database server"+e);
        return null;
    }
}

Question:

Now, I want to install the application on multiple computers with a remote centralised database. I had replaced "locahost" in the connection parameter with ip address of the remote server. It can connect to the database but the application becomes very very slow and irresponsive.

What is the best/recommended way to do this?

4
  • 1
    Are you familiar and/or are you implementing corectly the Event Dispatch Thread? Commented Mar 5, 2015 at 15:21
  • @Kon,No sir Iam not familiar with Event Dispatch Thread Commented Mar 5, 2015 at 15:22
  • Read the short summary here. Sounds like your problem to me, though it's not necessarily the case: docs.oracle.com/javase/tutorial/uiswing/concurrency/… Commented Mar 5, 2015 at 15:23
  • @Kon,Are you sure it is because of Event Dispatch Thread. Can i have any example code. Commented Mar 5, 2015 at 15:26

1 Answer 1

1

As it has been recommended, and since you do not seem to be familiar with it, this is most likely due to the fact that you are making database related operations on the same thread which is responsible for all your application's graphics rendering, the Event Dispatching Thread (EDT).

For instance, you have something like so (haven't done Swing in a while so not exactly compilable code):

button.addActionListener(new ...
    @Override
    public void actionPerformed(...) {
        Database Operations
    }

This means that your application is keeping the event dispatching thread busy with database stuff, which means that your EDT cannot keep up with the user's requirements because you now need to take into account network connection speed.

To solve this problem, you will need to make your application multi-threaded which I am afraid might not be a simple task since it might require you redesign your application.

EDIT: As per your question in the comments, you could do something like so:

button.addActionListener(new ...
    @Override
    public void actionPerformed(...) {
        new Thread(new Runnable() {
            @Override
            public void run() {  
                Database Operations
            }
        }).start();
    }

The above will spawn off a new thread within which your database operations are being performed. This is of course a simplistic view. The problem would be to make your application wait, but not hang, while awaiting for new data. For that, you will need to look into concurrency tutorials.

Sign up to request clarification or add additional context in comments.

7 Comments

Good explanation. I think I would suggest to OP to call the boolean javax.swing.SwingUtilities.isEventDispatchThread directly before his DB call(s) to make sure he is NOT on the event dispatch thread. If he is, then a redesign is in order.
@Kon, javax.swing.SwingUtilities.isEventDispatchThread is giving output "true"
And you called that directly before a DB call? That means that you are making the call inside the Event Dispatch Thread, which is bad. It's the reason your UI is hanging.
@Kon, Yes,I called that directly before a DB call(conn)? .Is there any way to make it work with some modifications.Plese give me the tutorial with examples.
@user2601972 This site isn't about writing somebody's code for them. It's about answering questions that you were unable to find the answer to yourself. What you've done in this thread is found the correct question. If you're not familiar with multi-threading and concurrency and Java, you'll need to read up on it. You will need to understand these ideas well in order to write any decent solution to this.
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.