我正在使用ProcessBuilder运行Postgres SQL脚本,但是我的进程似乎挂起了,因为我没有得到退出值。 我的代码有一个加载和执行某些SQL文件的测试方法。 然后,在执行要测试其操作的脚本文件之前,我先运行一些查询。
@Test
public void test340() throws Exception {
String filePath = testFilePath; // Script file I want to test
TreeSet<Path> set = getFilePaths();
for (Path file : set) { // Run previous scripts to setup the db
loadFileAndExecuteSQL(file.toString());
}
preCondition340(); // Run some inserts
loadFileAndExecuteSQL(testFilePath); // Now load the script file I need to test
postCondition340(); // Run some other queries to verify resultset
}
public void loadFileAndExecuteSQL(String scriptFilePath) throws Exception {
String command = String.format("psql -v ON_ERROR_STOP=1 --host %s --port %s --dbname %s --file %s",
"localhost", "5432", dbName, scriptFilePath);
List<String> commands = Arrays.asList(command.split("\\s+"));
ProcessBuilder pb = new ProcessBuilder(commands);
pb.redirectErrorStream(true);
final Process process = pb.start();
if(!process.waitFor(10000, TimeUnit.MILLISECONDS)) {
process.destroy();
}
System.out.println("Executing command " + command + process.exitValue());
}
该过程似乎在我第二次致电loadFileAndExecuteSQL()
时挂起,请问有人能解释为什么会发生这种情况,并建议我如何确保不会发生这种情况?谢谢。