Blame view

src/com/sitech/ismp/coll/TTSQLTarget.java 4.07 KB
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189
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();
		}
	}

}