⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 connectpool.java

📁 广州物价系统开发部分模块
💻 JAVA
字号:
package gzwj.sql;

import java.io.*; 
import java.sql.*; 
import java.util.*;
import java.util.Date;


public class ConnectPool {
	static public ConnectPool instance; 
	static public int clients; 
	public Vector drivers = new Vector(); 
	public PrintWriter log; 
	public Hashtable pools = new Hashtable(); 
	 
	static synchronized public ConnectPool getInstance() 
	{ 
		if (instance == null) 
		{ 
			instance = new ConnectPool(); 
		} 
		
		clients++; 
		
		return instance; 
	} 
	 
	public ConnectPool() {  
		init(); 
	} 
	 
	public void freeConnection(String name, Connection con) 
	{ 
		DBConnectionPool pool = (DBConnectionPool) pools.get(name); 
		if (pool != null) 
		{ 
			pool.freeConnection(con); 
		} 
		else 
		{ 
			System.out.println("pool ==null"); 
		} 
		clients--; 
	} 
	 
	public Connection getConnection(String name) 
	{ 
		log("1");
		DBConnectionPool pool = (DBConnectionPool) pools.get(name); 
		log("2");
		if (pool != null) 
		{ 
			log("3");
			//return pool.getConnection(); 
			return pool.returnConnection(); 
		} 
		return null; 
	} 

	public Connection getConnection(String name, long time) 
	{ 
		DBConnectionPool pool = (DBConnectionPool) pools.get(name); 		
		if (pool != null) 
		{ 
			return pool.getConnection(time); 
		} 
		return null; 
	} 
	 
	public synchronized void release() 
	{  
		if (--clients != 0) 
		{ 
			return; 
		} 
		
		Enumeration allPools = pools.elements(); 
		while (allPools.hasMoreElements()) 
		{ 
			DBConnectionPool pool = (DBConnectionPool) allPools.nextElement(); 
			pool.release(); 
		} 
		Enumeration allDrivers = drivers.elements(); 
		while (allDrivers.hasMoreElements()) 
		{ 
			Driver driver = (Driver) allDrivers.nextElement(); 
			try { 
				DriverManager.deregisterDriver(driver); 		
				log("untread JDBC programe driver :" + driver.getClass().getName()); 
			} 
			catch (SQLException e) 
			{ 
				log(e, "untread JDBC programe driver :" + driver.getClass().getName()); 
			}
		} 
	} 
	
 
	private void createPools(Properties props) 
	{ 
		Enumeration propNames = props.propertyNames(); 
		while (propNames.hasMoreElements()) 
		{ 
			String name = (String) propNames.nextElement(); 
			if (name.endsWith(".url")) { 
				String poolName = name.substring(0, name.lastIndexOf(".")); 
				String url = props.getProperty(poolName + ".url"); 
				if (url == null) { 
					log("have not point URL for " + poolName); 
					continue; 
				} 
				String user = props.getProperty(poolName + ".user"); 
				String password = props.getProperty(poolName + ".password"); 
				String maxconn = props.getProperty(poolName + ".maxconn", "100");
				log("MMMMMMMMMMMMMMMMMMM : " + maxconn); 
				int max; 
				try{ 
					maxconn=maxconn.trim();
					max = Integer.parseInt(maxconn); 
				} 
				catch (NumberFormatException e) 
				{ 
					log("error max : " + maxconn + " .connect pool: " + poolName); 
					max = 0; 
				}  ; 
				DBConnectionPool pool = new DBConnectionPool(poolName, url, user, password, max); 
				pools.put(poolName, pool); 
				log("Successfully to create the connect pool the name is :" + poolName); 
			} 
		} 
	} 
	 
	private void init() 
	{ 
		try 
		{ 
			Properties p = new Properties(); 
			String configs =  "C:\\conf\\db.properties"; 
			 
			FileInputStream is;
			Properties dbProps;
			try 
			{ 
				is = new FileInputStream(configs); 
				dbProps = new Properties();  
				dbProps.load(is);  
			} 
			catch (Exception e) 
			{ 
				System.err.println("can not got the properte file. " +"please make sure the db.properties is in CLASSPATH point director"); 
				return; 
			}  
			String logFile = dbProps.getProperty("logfile", "DBConnectionManager.log"); 
			try{  
				log = new PrintWriter(new FileWriter(logFile, true), true); 
			} 
			catch (IOException e) 
			{ 
				System.err.println("can not open the log file: " + logFile); 
				log = new PrintWriter(System.err); 
			}
			loadDrivers(dbProps);  
			createPools(dbProps); 
		}catch(Exception e){
			
		} 
	} 
	 
	private void loadDrivers(Properties props) 
	{ 
		String driverClasses = props.getProperty("drivers");
		StringTokenizer st = new StringTokenizer(driverClasses); 
		while (st.hasMoreElements()) 
		{ 
			String driverClassName = st.nextToken().trim(); 
			try{
				log("loadDrivers 1");
				Driver driver = (Driver)Class.forName(driverClassName).newInstance(); 
				log("loadDrivers 2");
				DriverManager.registerDriver(driver); 
				log("loadDrivers 3");
				drivers.addElement(driver);  
				log("Successfully to register the JDBC programe driver" + driverClassName); 
			} 
			catch (Exception e) 
			{   
				log("can not to register the JDBC programe driver: " + 
				driverClassName + ", Error: " + e); 
			} 
		} 
	} 
	 
	private void log(String msg) 
	{  
		log.println(new Date() + ": " + msg); 
	}  
	private void log(Throwable e, String msg) 
	{ 
		log.println(new Date() + ": " + msg); 
		e.printStackTrace(log); 
	}  
	
	class DBConnectionPool 
	{ 
		//private int checkedOut; 
		private Vector freeConnections = new Vector(); 
		private int maxConn; 
		private String name; 
		private String password; 
		private String URL; 
		private String user;  
		
		public DBConnectionPool(String name, String URL, String user, String password,int maxConn) 
		{ 
			this.name = name; 
			this.URL = URL; 
			this.user = user; 
			this.password = password; 
			this.maxConn = maxConn; 
		}  
		public synchronized void freeConnection(Connection con) {  
			try 
			{ 
				if(con.isClosed())
				{
					System.out.println("before freeConnection con is closed");
				} 
				freeConnections.addElement(con); 
				Connection contest = (Connection) freeConnections.lastElement(); 
				if(contest.isClosed())
				{
					System.out.println("after freeConnection contest is closed");
				} 
				notifyAll(); 
			}catch(SQLException e)
			{
				System.out.println(e);
			} 
		}  
		public synchronized Connection getConnection() 
		{ 
			Connection con = null; 
			if (freeConnections.size() > 0) 
			{  
				con = (Connection) freeConnections.firstElement(); 
				freeConnections.removeElementAt(0); 
				try { 
					if (con.isClosed()) 
					{ 
						log("success to delete one invalidation connect from " + name); 
						System.out.println("success to delete one invalidation connect form pool" + name);  
						con = getConnection(); 
					} 
				} 
				catch (SQLException e) 
				{ 
					log("throw a exception when delete one invalidation connect form pool" + name); 
					System.out.println("throw a exception when delete one invalidation connect form pool" + name);  
					con = getConnection(); 
				} 
				if(freeConnections.size()>maxConn) 
				{ 
					System.out.println(" delete one overflow connect "); 
					releaseOne(); 
				} 
			} 
			
			else if((maxConn == 0)||(freeConnections.size()<maxConn)) 
			{ 
				con = newConnection(); 
			} 
			
			return con; 
		} 
		
		public synchronized Connection returnConnection() 
		{ 
			Connection con = null; 
			log("4 freeConnections.size :" + freeConnections.size());
			log("4 maxConn :" + maxConn);
			if(freeConnections.size()<maxConn) 
			{				
				log("5");
				con = newConnection(); 
				log("6");
			}  
			else if(freeConnections.size()>=maxConn) 
			{			 
				log("61");
				con = (Connection) freeConnections.firstElement();
				log("62");
				System.out.println(" [a availability number of connect form connectpool ] : "+"[ "+freeConnections.size()+" ]"); 
				freeConnections.removeElementAt(0); 
				System.out.println(" [b availability number of connect form connectpool ] : "+"[ "+freeConnections.size()+" ]"); 
				try 
				{
					if (con.isClosed()) 
					{ 
						log("success to delete one invalidation connect form pool" + name); 
						System.out.println("success to delete one invalidation connect form pool" + name); 
						returnConnection(); 
					} 
				}catch (SQLException e) 
				{ 
					log("throw a exception when delete one invalidation connect form pool" + name); 
					System.out.println("throw a exception when delete one invalidation connect form pool" + name); 
					returnConnection(); 
				} 
			} 
			return con; 
		} 
		 
		public synchronized Connection getConnection(long timeout) 
		{ 
			long startTime = new Date().getTime(); 
			Connection con; 
			while ((con = getConnection()) == null) 
			{ 
				try 
				{ 
					wait(timeout); 
				} 
				catch (InterruptedException e) 
				{
					
				} 
				if ((new Date().getTime() - startTime) >= timeout)
				{  
					return null; 
				} 
			} 
			return con; 
		} 
		 
		public synchronized void release() 
		{ 
			Enumeration allConnections = freeConnections.elements(); 
			while (allConnections.hasMoreElements()) 
			{ 
				Connection con = (Connection) allConnections.nextElement(); 
				try { 
					con.close(); 
					log("success to colose the connnect form " + name); 
				} 
				catch (SQLException e) 
				{ 
					log(e, "not able to close the connect " + name); 
				} 
			} 
			freeConnections.removeAllElements(); 
		} 
		/** 
		* colose the connect
		*/ 
		public synchronized void releaseOne() 
		{ 
			if(freeConnections.firstElement()!=null) 
			{ 
				Connection con = (Connection) freeConnections.firstElement(); 
				try { 
					con.close(); 
					System.out.println("success to colose the connnect form " + name); 
					log("success to colose the connnect form " + name); 
				} 
				catch (SQLException e) 
				{ 
				
					System.out.println("not able to close " + name); 
					log(e, "not able to close the connect " + name); 
				} 
			} 
			else 
			{ 
				System.out.println("releaseOne() bug......................................................."); 
			
			} 
		} 
		 
		private Connection newConnection() 
		{ 
			Connection con = null; 
			try 
			{ 
				if (user == null) 
				{ 
					log("--url: "+URL);
					con = DriverManager.getConnection(URL); 
				} 
				else{ 
					log("--url: "+URL + "user :" + user + "password :" +password );
					con = DriverManager.getConnection(URL, user, password); 
				} 
				log("connect pool " + name+"successfully to create new connect"); 
			
			} 
			catch (SQLException e) { 
				log(e, "can not create the following URL connect : " + URL); 
				return null; 
			} 
			return con; 
		} 
	} 
}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -