java调用exp备份oracle数据库解决进程挂起问题

oracle备份数据库,这个问题因为进程挂起很是让我费劲,最后还是多亏了网友的力量,顺利解决,但是项目在运行当中,不知道是什么原因还是会出现进程挂起问题,重启一起tomcat,这个问题就解决了,本人水平有限,不知道具体是什么问题。
现将能用的代码贴出来,备份,以待后用。
实现的功能:备份全部数据库、备份两张数据表
需要前台传递进来一个备份标识:1:备份全部数据库,2:备份两张数据表
类名:IEEM060501Action.java
package com.jysd.ms.action;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.util.Date;
import java.util.Properties;
import com.jysd.ms.exception.BaseException;
import com.jysd.ms.util.ReadSystemConfig;
import com.jysd.ms.util.TimeCountUtil;
import com.opensymphony.xwork2.ActionSupport;

public class IEEM060501Action extends ActionSupport {

	private static final long serialVersionUID = 1L;
	private Runtime runtime = Runtime.getRuntime();   
	private Process process = null ;   
    //返回前台数据
	private String message;
    //备份文件地址
    private String backupUrl ;
    //备份文件名

    private String dmpName;
    //备份方式
    private int backupMode;
    //备份类型
    private int backupType;
	private Properties properties = new Properties() ;
    /**
	* @方法名称: execute
	* @描述: 数据备份管理初始化方法   
	* 创建人:
	* @return String
	* @param @return     
	*/
	public String execute()throws BaseException{
		InputStream in = ReadSystemConfig.class.getClassLoader().getResourceAsStream("backupOracle.properties");
		try {
			properties.load(in);
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		backupUrl = properties.getProperty("backupurl");
		return SUCCESS;
	}
	/**
	* @方法名称: backup
	* @描述: 备份数据库

	* 创建人:
	* @return String
	* @param @return
	* @param @throws BaseException     
	*/
	public String backup()throws BaseException{
		//从配置文件中读取数据库信息,并备份数据库
		boolean backupFlag = backupOracleDB(getExpString());
		if(backupFlag){
			message="备份成功!";
		}else{
			message="备份失败!";
		}
		return "backJson";
	}
   /**
	* @方法名称: backupOracleDB
	* @描述: 备份、还原 oracle 数据库的方法   
	* 创建人:
	* @return boolean
	* @param @param cmdStr 备份命令 ( 即导出 )
	* @param @return     
	*/
	public boolean backupOracleDB(String cmdStr) {   
		boolean isSuccess = true;
		try {   
			process = runtime .exec(cmdStr);
			String line = null; 
			BufferedReader br = new BufferedReader(new InputStreamReader(process.getErrorStream()));  
		   //读取ErrorStream很关键,这个解决了挂起的问题。  
		   while ((line = br.readLine()) != null){  
		        System.out.println(line);  
		    }  
		   br = new BufferedReader(new InputStreamReader(process.getInputStream()));  
		   while ((line = br.readLine()) != null){  
		        System.out.println(line);  
		    }  
		   //process.waitFor();  
			if(process.waitFor()!=0){
				isSuccess = false;
			}
		}catch (Exception e) {   
			e.printStackTrace();
			isSuccess = false;
		}   
		return isSuccess ;   
	}   
    /**
	* @方法名称: getLeftMenu
	* @描述: 获取备份数据库的执行语句,备份文件名称,以当前时间命名
	* @创建人:
	* @return String
	* @param @throws BaseException     
	*/
	private String getExpString()throws BaseException{
		StringBuffer sbu = new StringBuffer();
		try {
			InputStream in = ReadSystemConfig.class.getClassLoader().getResourceAsStream("backupOracle.properties");
			properties.load(in);
			 sbu.append("exp ");
			 sbu.append(properties.getProperty("username")).append("/");
			 sbu.append(properties.getProperty("password")).append("@");
			 //sbu.append(properties.getProperty("ipaddress")).append("/");
			 sbu.append(properties.getProperty("databasename")).append(" file=");
			 sbu.append(properties.getProperty("backupurl"));
			 Date date = new Date();
			 if(dmpName==null){
				 dmpName = TimeCountUtil.dateToString(date,"yyyy.MM.dd.HH.mm.ss");
			 }else{
				 dmpName += TimeCountUtil.dateToString(date,"yyyy.MM.dd.HH.mm.ss");
			 }
			 dmpName = dmpName.replace(" ","");
			 dmpName = dmpName.replace(".dmp","");
			 sbu.append(dmpName).append(".dmp");
			 //等于2,仅备份监测数据(实时表、日结表数据)
			 if(backupMode==2)
				 sbu.append(" TABLES=(").append(properties.getProperty("realtable")).append(",").append(properties.getProperty("daytable")).append(")");
	      } catch (Exception e) {   
	         e.printStackTrace();
	      }
	      System.out.println(sbu.toString());
		return sbu.toString();
	}
	/**
	 * 读取数据库信息

	 */
	/*private static Properties properties = new Properties() ;
	static {
        InputStream in = ReadSystemConfig.class.getClassLoader().getResourceAsStream("backupOracle.properties");
        try {
			properties.load(in);
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}*/
	
	public String getMessage() {
		return message;
	}
	public void setMessage(String message) {
		this.message = message;
	}
	public String getBackupUrl() {
		return backupUrl;
	}
	public void setBackupUrl(String backupUrl) {
		this.backupUrl = backupUrl;
	}
	public String getDmpName() {
		return dmpName;
	}
	public void setDmpName(String dmpName) {
		this.dmpName = dmpName;
	}
	public int getBackupMode() {
		return backupMode;
	}
	public void setBackupMode(int backupMode) {
		this.backupMode = backupMode;
	}
	public int getBackupType() {
		return backupType;
	}
	public void setBackupType(int backupType) {
		this.backupType = backupType;
	}
	
}


backupOracle.properties
username=1234
password=1234
ipaddress=192.168.1.126
backupurl=D\:/
databasename=orcl
realtable=table1
daytable=table2

你可能感兴趣的:(oracle数据库)