java调用pg_dump备份postgre数据库问题


在项目中,用java调用pg_dump进行postgre数据库备份,在本地机上跑,可以正确运行,发布

到其他机器上之后,会在调用外部程序,进行备份的地方卡住,代码如下:



/**备份数据库**/


File workPath=fetchDBInstallPath();//获取数据库的pg_dump工具的目录

if(workPath==null){

return false;


}

String dbBackupPath=fetchDBBackupPath();

StringBuffer sb=new StringBuffer();


sb.append( cmd /c pg_dump -h 127.0.0.1 -p 5432 -U postgres -F c -b -v

-f ).append(dbBackupPath+ \\ +fileName+ .backup ).append( dbname );


log.info( ********************************************** );


log.info( workPath= +workPath); www.docin.com


log.info( command: +sb.toString());



log.info( ********************************************** );


Process

process=Runtime.getRuntime().exec(sb.toString(),null,workPath);

log.info( ~~~~~~~~~~~~~~~~~~~~~~~~~~~ );


StreamGobbler errorGobbler = new

StreamGobbler(process.getErrorStream(), Error ); //线程,用于输出外部程序的输

出内容,防止阻塞



StreamGobbler outputGobbler = new

StreamGobbler(process.getInputStream(), Output ); //线程,用于输出外部程序的输

出内容,防止阻塞



log.info( +++++++++++++++++++++++++++ );



errorGobbler.start();



outputGobbler.start();



process.waitFor();



if(process.exitValue()!=0){



return false;



} www.docin.com



从部署机器上的日志来看,运行完成了log.info( +++++++++++++++++++++++++++ );


之后,线程没有正确运行,也就是说,应该是外部程序被阻塞了,然后我把命令行中的-v参数

去掉,不让外部程序输出内容,可还是会阴塞,具体日志如下:


2012-10-12 10:23:43.551 INFO] **********************************************

2012-10-12 10:23:43.551 INFO] workPath=C:\Program

Files\hikvision\CMS\database\bin



2012-10-12 10:23:43.551 INFO] command:cmd /c pg_dump -h 127.0.0.1 -p 5432 -U

postgres -F c -b -v -f c:\backup\test.backup 5221cms



2012-10-12 10:23:43.551 INFO] **********************************************



2012-10-12 10:23:43.711 INFO] ~~~~~~~~~~~~~~~~~~~~~~~~~~~


2012-10-12 10:23:43.711 INFO] +++++++++++++++++++++++++++

------最佳解决方案--------------------------------------------------------



在命令前添加一行

www.docin.com


set PGPASSWORD= password