简体   繁体   中英

Restoring Mysql dump from java: why does it hang the process?

I'm trying to restore a Mysql dump from a Java app. I'm using this code:

    Runtime rt = Runtime.getRuntime();
                try {
                   comando = "mysql -u root -ppass -e \"source " + path + "\\sql\\backup.sql\" legapelis" ; 

                   Process proceso = rt.exec(comando);
                   System.out.println("ejecutando");
                   System.out.println(comando);


                   proceso.getInputStream().close();
                   proceso.getOutputStream().close();
                   proceso.getErrorStream().close();


                   completado = proceso.waitFor();
                   if (completado != 0) {
                       System.out.println("error");
                       addActionError(getText("backup.errcopia") + ": " + String.valueOf(completado));
                       consultarCopias();
                       return "error";
                   }
                   System.out.println("fin");


                } catch (Exception e) {
                    addActionError(e.getLocalizedMessage());
                    consultarCopias();
                    return "error";
                }

But the Mysql process that is opened by this code never ends. It hangs and if I kill it, the Mysql service stops working fine (I must restart it). I've tried a lot of codes: closing streams, reading it... but with the same result.

Any hint?

Excuse me if my English isn't so good.

Thank's

Ps: I've tried to add the -v option to the command to read it from inputstream. It start reading but then it hangs again (and it dosn't finish reading the output of the command: it stops suddenly)

JavaDoc for Process :

The created subprocess does not have its own terminal or console. All its standard io (ie stdin, stdout, stderr) operations will be redirected to the parent process through three streams (getOutputStream(), getInputStream(), getErrorStream()). The parent process uses these streams to feed input to and get output from the subprocess. Because some native platforms only provide limited buffer size for standard input and output streams, failure to promptly write the input stream or read the output stream of the subprocess may cause the subprocess to block, and even deadlock.

You need to read from these streams, not close them.

Well, finally I changed the procedure because I couldn't solve this problem. Now, I read the SQL file and I create an ArrayList with the sentences and I execute them by myself.

Thank's everybody for your support.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM