www.pudn.com > dataswap.rar > ConnectionFactory.java


package com.gemt.dataswap.dao; 
 
import java.sql.Connection; 
import java.sql.DriverManager; 
import java.sql.PreparedStatement; 
import java.sql.ResultSet; 
import java.sql.SQLException; 
import java.util.HashMap; 
import java.util.Map; 
import java.util.Set; 
import oracle.jdbc.OracleDriver; 
import org.apache.commons.logging.Log; 
import org.apache.commons.logging.LogFactory; 
import com.gemt.dataswap.config.DataBaseConfig; 
import com.gemt.dataswap.resources.ReadConfig; 
import com.gemt.dataswap.util.Constants; 
 
public abstract class ConnectionFactory 
{ 
	private static final Log log = LogFactory.getLog(ConnectionFactory.class); 
	 
	public static Connection connections[]; 
	public static Map dbNameMap = new HashMap(); 
	public static Map dbDialectMap = new HashMap(); 
	 
	static { 
		getAllConnection(); 
	} 
	 
	public static Connection getConnection(String dbName) { 
		if(log.isDebugEnabled()) { 
			log.debug("Connection getConnection(String dbName) begin"); 
		} 
 
		if(log.isDebugEnabled()) { 
			log.debug("Connection getConnection(String dbName) end"); 
		}		 
		return connections[dbNameMap.get(dbName).intValue()]; 
		 
		//ReadConfig.dataBaseConfig(); 
	} 
	 
	public static Connection[] getAllConnection() { 
		if(log.isDebugEnabled()) { 
			log.debug("Connection[] getAllConnection() begin"); 
		} 
		Map dataBaseConfigs = ReadConfig.dataBaseConfig; 
		if(dataBaseConfigs == null || dataBaseConfigs.size() == 0) { 
			if(log.isErrorEnabled()) { 
				log.error("lost database config, can not build database connection"); 
			} 
		} 
		 
		if(log.isDebugEnabled()) { 
			log.debug("fetch connection step one "); 
		} 
		connections = new Connection[dataBaseConfigs.size()]; 
		Set dbNameSet = dataBaseConfigs.keySet(); 
		if(log.isDebugEnabled()) { 
			log.debug("fetch connection step two " + dbNameSet); 
		}		 
		int i = 0; 
		for(String dbName : dbNameSet) { 
			dbNameMap.put(dbName, new Integer(i)); 
			if(log.isDebugEnabled()) { 
				log.debug("fetch connection step three "); 
			} 
			 
			DataBaseConfig dataBaseConfig = dataBaseConfigs.get(dbName); 
			dbDialectMap.put(dbName, dataBaseConfig.getDbDialect()); 
			if(Constants.DATABASE_ORACLE.equals(dataBaseConfig.getDbDialect())) { 
				try 
				{ 
					DriverManager.registerDriver(new OracleDriver()); 
					final String url = Constants.ORACLE_DB_URL_PREFIX + dataBaseConfig.getHost() + ":"  
						+ dataBaseConfig.getPort() + ":" + dataBaseConfig.getDbName(); 
					if(log.isDebugEnabled()) { 
						log.debug("oracle :: url = " + url); 
					} 
					connections[i] = DriverManager.getConnection(url, dataBaseConfig.getUserName(), dataBaseConfig.getPassword()); 
				} 
				catch (SQLException e) 
				{ 
					if(log.isErrorEnabled()) { 
						log.error("cant not build connetion for database :: " + dbName + " , Please check config file || " + Constants.DATABASE_CONFIG); 
					} 
					//e.printStackTrace(); 
				} 
			} 
			else if(Constants.DATABASE_SQL_SERVER.equals(dataBaseConfig.getDbDialect())) { 
				try 
				{ 
					DriverManager.registerDriver(new net.sourceforge.jtds.jdbc.Driver()); 
					final String url = Constants.SQL_SERVER_URL_PREFIX + dataBaseConfig.getHost() + ":"  
						+ dataBaseConfig.getPort()+ "/" + dataBaseConfig.getDbName() + ";TDS=7.0";//"jdbc:jtds:sqlserver://server/db;TDS=7.0"; 
					if(log.isDebugEnabled()) { 
						log.debug("SQL Server :: url = " + url); 
					} 
					connections[i] = DriverManager.getConnection(url, dataBaseConfig.getUserName(), dataBaseConfig.getPassword()); 
					 
					 
				} 
				catch (SQLException e) 
				{ 
					if(log.isErrorEnabled()) { 
						log.error("cant not build connetion for database :: " + dbName + " , Please check config file || " + Constants.DATABASE_CONFIG); 
					} 
					//e.printStackTrace(); 
				} 
			} 
			else { 
				if(log.isWarnEnabled()) { 
					log.warn("please config database dialect properly for database :: " + dbName + " , Please check config file || " + Constants.DATABASE_CONFIG); 
				} 
			} 
			//jdbc:oracle:thin:@192.168.0.16:1521:szwrb 
			i++; 
		} 
		 
		if(log.isDebugEnabled()) { 
			log.debug("Connection[] getAllConnection() end"); 
		}		 
		return connections; 
	} 
	 
	public static void closeAllConnection() { 
		if(log.isDebugEnabled()) 
			log.debug("closeAllConnection() begin.."); 
		if(connections != null && connections.length > 0) { 
			for(int ci = 0; ci < connections.length; ci++) { 
				 
				try { 
					if(connections[ci] != null && !connections[ci].isClosed()) { 
						connections[ci].close(); 
					} 
				} 
				catch (SQLException e) { 
                    if(log.isErrorEnabled()) { 
                    	log.error("throw exception when close database connection"); 
                    } 
				} 
			} 
		} 
		 
		if(log.isDebugEnabled()) 
			log.debug("closeAllConnection() end..");		 
	} 
	 
	 
	/** 
	 * @param args 
	 */ 
	public static void main(String[] args) 
	{ 
		//getAllConnection(); 
		 
		Connection con = getConnection("db3"); 
		try { 
			PreparedStatement pstmt = con.prepareStatement("select * from FTD0311_SNPH_R "); 
			ResultSet rs = pstmt.executeQuery(); 
			while(rs.next()) { 
				for(int i = 1; i <= rs.getMetaData().getColumnCount(); i++) { 
					log.debug(rs.getMetaData().getColumnName(i) + " " + rs.getObject(i)); 
				} 
			} 
			if(rs != null) 
				rs.close(); 
			if(pstmt != null) 
				pstmt.close(); 
		} 
		catch (SQLException e) { 
			// TODO Auto-generated catch block 
			e.printStackTrace(); 
		} 
		finally { 
			 
		} 
		closeAllConnection(); 
		 
		// TODO Auto-generated method stub 
	} 
}