gpt4 book ai didi

java - 从 java : why does it hang the process? 恢复 Mysql 转储

转载 作者:行者123 更新时间:2023-11-29 03:07:35 26 4
gpt4 key购买 nike

我正在尝试从 Java 应用程序恢复 Mysql 转储。我正在使用这段代码:

    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";
}

但是这段代码开启的Mysql进程永远不会结束。它挂起,如果我杀死它,Mysql 服务将停止正常工作(我必须重新启动它)。我尝试了很多代码:关闭流、读取它……但结果相同。

有什么提示吗?

如果我的英语不太好,请原谅。

谢谢

Ps:我尝试将 -v 选项添加到命令以从输入流读取它。它开始读取但随后再次挂起(并且它没有完成读取命令的输出:它突然停止)

最佳答案

Process 的 JavaDoc:

The created subprocess does not have its own terminal or console. All its standard io (i.e. 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.

您需要从这些流中读取,而不是关闭它们。

关于java - 从 java : why does it hang the process? 恢复 Mysql 转储,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13114282/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com