【发布时间】:2016-05-11 14:05:04
【问题描述】:
我想在 java 中使用 pg_dump 创建我的数据库的备份。备份的创建工作正常,但在程序退出之前不会启动。
有什么方法可以立即开始备份?
public static void backupDb() throws IOException, InterruptedException {
String path = "C:\\HACENE\\test.backup";
Runtime r = Runtime.getRuntime();
//PostgreSQL variables
String host = "localhost";
String user = "postgres";
String dbase = "gtr_bd";
String password = "postgres";
Process p;
ProcessBuilder pb;
r = Runtime.getRuntime();
pb = new ProcessBuilder("C:\\Program Files\\PostgreSQL\\9.3\\bin\\pg_dump", "-v", "-h", host, "-f", path, "-U", user, dbase);
pb.environment().put("PGPASSWORD", password);
pb.redirectErrorStream(true);
p = pb.start();
System.out.println("end of backup");
}
【问题讨论】:
-
p.waitFor()也许? -
行为取决于操作系统环境。流程执行不是由 java 本身完成的。因此,由操作系统实际执行您定义并移交执行的进程。另外 pprogramme 退出前还有多长时间(毫秒、秒、小时)?
-
进程由操作系统处理,所以从逻辑上讲它应该在我从应用程序启动时启动,但进程在我退出程序时启动
标签: java postgresql processbuilder