Я использую 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()
Может кто-нибудь объяснить, почему это происходит, и предложить, как я могу убедиться, что этого не происходит? Спасибо.