Wednesday, 14 August 2013

Why does java process hang executing mysqldump?

Why does java process hang executing mysqldump?

I want to backup database in mysql 5.6.
For this I use this method:
public boolean backupDB(String dbName, String dbUserName, String
dbPassword, String path) {
String executeCmd = "D://mysql-5.6.11-win32/bin/mysqldump -u " +
dbUserName + " -p" + dbPassword
+ " --add-drop-database -B " + dbName + " -r " + path;
Process runtimeProcess;
try {
runtimeProcess = Runtime.getRuntime().exec(executeCmd);
// any error message?
StreamGobbler errorGobbler = new
StreamGobbler(runtimeProcess.getErrorStream(), "ERR");
// any output?
StreamGobbler outputGobbler = new
StreamGobbler(runtimeProcess.getInputStream(), "OUT");
// kick them off
errorGobbler.start();
outputGobbler.start();
int processComplete = runtimeProcess.waitFor();
if (processComplete == 0) {
System.out.println("Backup created successfully");
return true;
} else {
System.out.println("Could not create the backup");
}
} catch (Exception ex) {
ex.printStackTrace();
}
return false;
}
StreamGobbler is Thread that wraps runtimeProcess.getErrorStream() and
runtimeProcess.getInputStream() into BufferedReader. BufferedReader merely
reads line by line using readLine() method. This is a hint I got on this
article.
My problem is that method backupDB hangs on this line :
int processComplete = runtimeProcess.waitFor();
If I substitute waitFor method with exitValue I receive error
java.lang.IllegalThreadStateException: process has not exited
Therefore I must use waitFor method. And unfortunately it runs forever. I
must press red icon on eclipse console to stop the JVM.
How to make process complete and backup mysql database?
P.S.My main goal is to backup mysql database using java. I use jdbc but if
you know good way to backup mysql database via mysqldump I am grateful to
know.

No comments:

Post a Comment