TTSQLTarget.java 4.07 KB
package com.sitech.ismp.coll;

import java.sql.CallableStatement;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.Vector;

import org.apache.log4j.Logger;

public class TTSQLTarget extends CollBase {

	String ObjectIP = null;
	String DBType = "TimesTen";
	String Drivers = "com.timesten.jdbc.TimesTenDriver";
	String driverpre = "jdbc:timesten:direct:dsn="; // jdbc:timesten:direct:dsn=test
//	String driverpre = "jdbc:timesten:client:dsn="; // jdbc:timesten:client:dsn=test
	String ObjectIPPort = "1521";
	String SIDName = null;
	String user = null;
	String passwd = null;
	String cUrl = null;
	boolean STATE = false;
	
	private Connection dbConnection = null;

	public String getObjectIP() {
		return ObjectIP;
	}

	public void setObjectIP(String objectIP) {
		ObjectIP = objectIP;
	}

	public String getDBType() {
		return DBType;
	}

	public void setDBType(String type) {
		DBType = type;
	}

	public String getObjectIPPort() {
		return ObjectIPPort;
	}

	public void setObjectIPPort(String objectIPPort) {
		ObjectIPPort = objectIPPort;
	}

	public String getSIDName() {
		return SIDName;
	}

	public void setSIDName(String name) {
		SIDName = name;
	}

	public String getUser() {
		return user;
	}

	public void setUser(String user) {
		this.user = user;
	}

	public String getPasswd() {
		return passwd;
	}

	public void setPasswd(String passwd) {
		this.passwd = passwd;
	}

	public String getCUrl() {
		return cUrl;
	}

	public void setCUrl(String url) {
		cUrl = url;
	}

	public void init(String ip_addr, String port, String username, String password, String sidname) {
		KPISet.clear();
		this.setObjectIP(ip_addr);
		this.setObjectIPPort(port);
		this.setUser(username);
		this.setPasswd(password);
		this.setSIDName(sidname);
	}

	public Vector getSQLKPIInfos(String sql) {
		if (sql == null || sql.equals(""))
			return null;
		Vector result = getKPISets(sql);
		return result;
	}
	
	public Vector getKPISets(String sql) {
		Vector recordSet = new Vector();
		try {
			if (!getState()) {
				return recordSet;
			}
			CallableStatement stmt = null;
			ResultSet result = null;
			stmt = dbConnection.prepareCall(sql);
			result = stmt.executeQuery();
			while (result.next()) {
				Vector vTemp = new Vector();
				for (int i = 0; i < result.getMetaData().getColumnCount(); i++) {
					Object oTemp = result.getObject(i + 1);
					String sTemp = oTemp == null ? "" : oTemp.toString();
					vTemp.addElement(sTemp.trim());
				}
				recordSet.add(vTemp);
			}
			stmt.close();
			dbConnection.commit();
		} catch (SQLException sqle) {
			logger.error(sql);
			sqle.printStackTrace();
		} catch (Exception ae) {
			ae.printStackTrace();
		} finally {
			releaseResources();
		}
		return recordSet;
	}

	public boolean getState() {
		getConnection();
		try {
			if (!STATE) {
				System.err.println("--second--conn error----");
				java.lang.Thread.sleep(1000);// 等待1秒
				getConnection();
				if (!STATE) {
					System.err.println("--third--conn error----");
					java.lang.Thread.sleep(5000);// 等待5秒
					getConnection();
				}
			}
		} catch (Exception e) {
			e.printStackTrace();
		}
		return STATE;
	}

	public Connection getConnection() {
		String url = null;
		if (cUrl == null) {
			url = this.driverpre + SIDName;
			System.out.println("connect url: " + url);
		} else {
			url = cUrl;
			System.out.println("connect url: " + url);
		}
		try {
			if (dbConnection == null || dbConnection.isClosed()) {
				/*TimesTenDataSource ds = new TimesTenDataSource();
				ds.setUrl(url);
				dbConnection = ds.getConnection();*/
				logger.info("now, connection is null");
				Class.forName(Drivers);
				dbConnection = DriverManager.getConnection(url);
			}
			logger.info("get a conn success");
			STATE = true;
		} catch (Exception e) {
			STATE = false;
			logger.info("getConnection Error! url=" + url);
			e.printStackTrace();
		}
		return dbConnection;
	}

	public void releaseResources() {
		try {
			if (dbConnection != null && !dbConnection.isClosed()) {
				dbConnection.close();
			}
		} catch (SQLException se) {
			se.printStackTrace();
		}
	}

}