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

📄 dbconnmanager.java

📁 考勤管理系统是针对学校每个月的考勤的报表进行总结
💻 JAVA
字号:
package setting;

import java.io.InputStream;
import java.sql.Connection;
import java.sql.Driver;
import java.sql.DriverManager;
import java.sql.SQLException;
import java.util.Enumeration;
import java.util.Hashtable;
import java.util.Properties;
import java.util.StringTokenizer;
import java.util.Vector;

import javax.naming.NameNotFoundException;

import action.Log;

public class DBConnManager {
	private static DBConnManager instance = null;
	private static int clients = 0;
	private static Hashtable<String, DBConnPool> pools = null;
	private static Vector<Driver> drivers = null;
			
	static {
		pools = new Hashtable<String, DBConnPool>(2, 0.75F);
		drivers = new Vector<Driver>();
	}
	
	public DBConnManager() {
		init();
	}
	
	/**
	 * 返回唯一实例,如果是第一次调用此方法,则创建实例
	 * 
	 * @return DBConnManager 唯一实例
	 */
	public static synchronized DBConnManager getInstance() {
		if (instance == null) {
			instance = new DBConnManager();
		}
		clients ++;
		return instance;
	}
	
	/**
	 * 释放数据库的连接。获得一个可用的连接。
	 * @param name 连接池的名称
	 * @param conn 要释放的连接
	 */
	public void freeConn(String name, Connection conn) {
		DBConnPool pool = null;
		try {
			pool = lookup(name);
		} catch (NameNotFoundException e) {
			Log.writeLog("setting.DBConnManager.freeConn:" +
					"找不到" + name + "连接池!");
		}
		if (pool != null) {
			pool.freeConn(conn);
		}
	}
	
	/**
	 * 获得一个可用的连接
	 * @param name 连接池的名称
	 * @return Connection 返回一个有用的连接
	 */
	public Connection getConn(String name) {
		DBConnPool pool = null;
		//pool = (DBConnPool)pools.get(name);
		try {
			pool = lookup(name);
		} catch (NameNotFoundException e) {
			Log.writeLog("setting.DBConnManager.getConn:" +
					"找不到" + name + "连接池!");
		}
		if (pool != null) {
			return pool.getConn();
		}
		return null;
	}
	
	/**
	 * 关闭所有连接,撤销驱动程序的注册
	 *
	 */
	public synchronized void release() {
		//等待直到最后一个用户程序调用
		if (--clients != 0) {
			return;
		}
		Enumeration allPools = pools.elements();
		while (allPools.hasMoreElements()) {
			DBConnPool pool = (DBConnPool) allPools.nextElement();
			pool.release();
		}
		Enumeration allDrivers = drivers.elements();
		while (allDrivers.hasMoreElements()) {
			Driver driver = (Driver) allDrivers.nextElement();
			try {
				DriverManager.deregisterDriver(driver);
				Log.writeLog("撤销JDBC驱动程序 " + 
						driver.getClass().getName() + " 的注册");
			} catch (SQLException e) {
				Log.writeLog(e, "无法撤销下列JDBC驱动程序的注册:" + 
						driver.getClass().getName());
			}
		}
	}
	
	/**
	 * 根据指定属性创始连接池实例
	 * 
	 * @param props 连接池属性
	 */
	private void createPools(Properties props) {
		Enumeration propNames = props.propertyNames();
		while (propNames.hasMoreElements()) {
			//获取数据库名
			String name = (String)propNames.nextElement();
			if (name.endsWith(".ip")) {
				String poolName = name.substring(0, name.lastIndexOf("."));
				String ip = props.getProperty(poolName + ".ip");
				if (ip == null) {
					Log.writeLog("setting.DBConnManager.createPools:" +
							"没有为连接池 " + poolName + " 指定UEL");
					continue;
				}
				String port = props.getProperty(poolName + ".port");
				String dataName = props.getProperty(poolName + ".dataname");
				String user = props.getProperty(poolName + ".user");
				String password = props.getProperty(poolName + ".password");
				String minconn = props.getProperty(poolName + ".minconn", "0");
				String maxconn = props.getProperty(poolName + ".maxconn", "0");
				int min;
				try {
					min = Integer.valueOf(minconn).intValue();
				} catch (NumberFormatException e) {
					Log.writeLog("setting.DBConnManager.createPools:" +
							"错误的最小连接数限制: " + maxconn + 
							".连接池: " + poolName);
					min = 0;
				}
				int max;
				try {
					max = Integer.valueOf(maxconn).intValue();
				} catch (NumberFormatException e) {
					Log.writeLog("setting.DBConnManager.createPools:" +
							"错误的最大连接数限制: " + maxconn + 
							".连接池: " + poolName);
					max = 0;
				}
				String timeOut = props.getProperty(poolName + ".timeout", "0");
				long outtime;
				try {
					outtime = Long.valueOf(timeOut).longValue();
				} catch (NumberFormatException e) {
					Log.writeLog("setting.DBConnManager.createPools:" +
							"错误的最大空闲时间数限制: " + maxconn + 
							".连接池: " + poolName);
					outtime = 600000;
				}
				String timeWait = props.getProperty(poolName + ".waitTime", "0");
				long waittime;
				try {
					waittime = Long.valueOf(timeWait).longValue();
				} catch (NumberFormatException e) {
					Log.writeLog("setting.DBConnManager.createPools:" +
							"错误的等待时间数限制: " + maxconn + 
							".连接池: " + poolName);
					waittime = 30000;
				}
				
				DBConnPool pool = new DBConnPool(poolName, ip, port, dataName, 
						user, password, min, max, outtime, waittime);
				pools.put(poolName, pool);
				Log.writeLog("settng.DBConnManager.createPools:" +
						"成功创建连接池" + poolName);
			}
		}
	}

	/**
	 * 读取指定的属性初始化连接池
	 * @param propName 属性
	 */
	private void init() {
		InputStream is = getClass().getResourceAsStream("db.properties");
		Properties dbProps = new Properties();
		try {
			dbProps.load(is);
		} catch (Exception e) {
			Log.writeLog("setting.DBConnManager.init:" +
					"不能读取属性文件。请确保" +
					"db.properties在CLASSPATH指定的路径中");
			return;
		}
		loadDrivers(dbProps);
		createPools(dbProps);
	}
	
	/**
	 * 装载和注册所有的JDBC驱动程序
	 * @param props 属性
	 */
	private void loadDrivers(Properties props) {
		String driverClasses = props.getProperty("drivers");
		StringTokenizer st = new StringTokenizer(driverClasses);
		while (st.hasMoreElements()) {
			String driverClassName = st.nextToken().trim();
			try {
				Driver driver = (Driver)
						Class.forName(driverClassName).newInstance();
				DriverManager.registerDriver(driver);
				drivers.addElement(driver);
				Log.writeLog("setting.DBConnManager.loadDrivers:\n\t" +
						"成功注册JDBC驱动程序" + driverClassName);
			} catch (Exception e) {
				Log.writeLog("setting.DBConnManager.loadDrivers:\n\t" +
						"无法注册JDBC驱动程序:"+driverClassName + 
						", 错误:" + e);
			}
		}
	}
	
	/**
	 * 从所有连接池中获得指定名称对应的连接池对象
	 * @param name 连接池对象
	 * @return DBConnPool 返回名称对应的连接池对象
	 * @throws NameNotFoundException 无法找到指定的连接池
	 */
	public static DBConnPool lookup(String name) 
			throws NameNotFoundException {
		Object ob = null;
		ob = pools.get(name);
		if (ob == null || !(ob instanceof DBConnPool)) {
			throw new NameNotFoundException(name);
		}
		return (DBConnPool)ob;
	}
}

⌨️ 快捷键说明

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