0

This code do restore mysql tables but application almost goes into sleep(20min plus). and my database Routines do not come in mysql workbench, can somebody tell me what is the actual solution?

public static boolean restoreDB(String dbName, String dbUserName, String dbPassword, String source) {

    String[] executeCmd = new String[]{"C:\\Program Files\\MySQL\\MySQL Server 5.5\\bin\\mysql", "--user=" + dbUserName, "--password=" + dbPassword, "-e", "source \"D:/khokher/mydb.sql\""};

    Process runtimeProcess;
    try {

        runtimeProcess = Runtime.getRuntime().exec(executeCmd);
        int processComplete = runtimeProcess.waitFor();

        if (processComplete == 0) {
            System.out.println("Backup restored successfully");
            return true;
        } else {
            System.out.println("Could not restore the backup");
        }
    } catch (Exception ex) {
        ex.printStackTrace();
    }

    return false;
}
2
  • Can you please post the actual problem? Commented Nov 5, 2012 at 6:51
  • Johnny Graber, i think this is the problem of importing mysql routine i guess mysql is dependent on its routines and we also need to make routines dump while creating backup of databases and for that there should be a different mysql backup query. Commented Nov 5, 2012 at 7:34

1 Answer 1

2

When using the Runtime.exec method, you have to read the output from the process it creates. Otherwise the process fills its output buffer and stops to wait until you empty the buffer by reading the output.

To read the output from a process you get the input streams from the Process object that exec returned. You can read the standard output from the stream returned by getInputStream and the standard error from the stream returned by getErrorStream.

If you can, use the Apache Commons Exec library. It makes launching an external processes and handling its input and output easier.

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

3 Comments

it seems nice information, but problem is my dump query is not generating routines i can import only tables and view.
mysql prints error messages, if there are any, to its output. You need to read the output to find if there were any errors. So the mysql command actually finishes after running for 20 minutes? What is the exit code?
command do not finishes exactly after 20 min it do take more than this i guess never finishes but it do restore the data base after 20 min i made few changes in above code and now only problem is this it keep me waiting. and i m not using exit code

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.