4 Replies Latest reply: Jul 6, 2012 8:52 AM by 947375 RSS

    mysqldump

    947375

      Hi,
      I am executing mysqlDump from a Java program with the following code:

      Process execCmd = Runtime.getRuntime().exec(<mysqldump with options>);

      and is working fine. But when I add the following commands to check the file created, the size of the file created is not correct as it seems that the size of the file is being checked before the mysqldump process is finished

       //if i tell the program to pause for a few seconds here, everything works fine. //I want that the prog waits here until mysqldump process is finished File mysqlDumpFile = new File(<mysqldump created file>); System.out.println(mysqlDumpFile.length()); 

      Can I make the program wait untill the mysqldump process is completely finished ?

      Thanks

        • 1. Re: mysqldump
          Kayaman
          The waitFor() method of Process class should do that for you.
          • 2. Re: mysqldump
            947375
            I tried that already but the program freezes, because it seems the mysqldump does not return a value 0 indicating that the process is finished.

            I tried the waitfor() command running the program both on Fedora Linux and Windows Os and the program became unresponsive (as if the application is waiting for the mysqldump process to finish) on both.
            • 3. Re: mysqldump
              sabre150
              'mysqldump' does return zero on success. You need to read,digest and implement the recommendations in http://www.javaworld.com/jw-12-2000/jw-1229-traps.html . I use a utility class SyncPipe rather than the StreamGobbler but the effect is very similar.

              I have just run
                       ProcessBuilder pb = new ProcessBuilder("mysqldump", "--password=" + password, "test");
                      Process process = pb.start();
                      new Thread(new SyncPipe(process.getErrorStream(),System.err).setCloseAfterCopy(false)).start();
                      new SyncPipe(process.getInputStream(), new FileOutputStream(System.getProperty("user.home") + "/temp1.sql")).run();
                      final int exitValue = process.waitFor();
                      System.out.println("Process exit value = " + exitValue);
              which dumps the database to file temp1.sql and returns an exit code of zero.


              Rather than perform the output in Java one can pipe it directly to the file using the shell :-
                      ProcessBuilder pb = new ProcessBuilder("bash", "-c", "mysqldump --password=" + password + " test > test1.sql");
                       pb.directory(new File(System.getProperty("user.home")));
                      Process process = pb.start();
                      new Thread(new SyncPipe(process.getErrorStream(),System.err).setCloseAfterCopy(false)).start();
                      new SyncPipe(process.getInputStream(), System.out).setCloseAfterCopy(false).run();
                      final int exitValue = process.waitFor();
                      System.out.println("Process exit value = " + exitValue);
              Edited by: sabre150 on Jul 4, 2012 2:14 PM

              Minor changes to code
              • 4. Re: mysqldump
                947375
                Thanks for your help it worked fine !