0

I'm using ProcessBuilder to run Postgres SQL scripts and my process seems to hang, because I'm not getting the exit value. My code has a test method that loads and executes certain SQL files. I then run some individual queries before executing the script file whose operations I want to test on.

    @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());
    }

The process seems to hang on my second call to loadFileAndExecuteSQL() Could anyone explain why this is happening and suggest how I can make sure this does not happen? Thanks.

Rebecca
  • 21
  • 1
  • 5
  • It's probably waiting for password input. See [here](https://stackoverflow.com/questions/9736085) or [here](https://stackoverflow.com/questions/6523019) for tips on how to prevent the password prompt –  Apr 01 '19 at 05:51
  • It does run all the script files in the first call to loadFileAndExecuteSQL(), so there is no authentication problem, I used pgAdmin to verify too. – Rebecca Apr 01 '19 at 06:07
  • Try to debug by placing sysout's after each line and see at which place it stops. – Santosh Apr 01 '19 at 14:45
  • Also look into it, https://stackoverflow.com/questions/5483830/process-waitfor-never-returns – Santosh Apr 01 '19 at 14:49
  • I figured this out, I had set autoCommit to false, so this was not allowing the inserts to go through. Thanks for the suggestions! – Rebecca Apr 02 '19 at 16:59

0 Answers0