📄 dbconnectionmanager.java
字号:
/** * <p>Title: </p> * JDBC数据库连接池的实现 * @author not attributable * @version 1.0 */import java.sql.*;import java.io.*;import java.util.*;import java.util.Date;public class DBConnectionManager { static private DBConnectionManager instance; static private int clients; private Vector drivers = new Vector(); private PrintWriter log; private Hashtable pools = new Hashtable(); /** * 得到DBConnectionManager 对象实例 * * @return DBConnectionManager 对象实例 */ static synchronized public DBConnectionManager getInstance() { if (instance == null) { instance = newe DBConnectionManager(); } clients++; return instance; } /** * 构造函数 * */ private DBConnectionManager() { init(); } /** * 将使用完的数据库连接 * * @param name 属性文件中定义了连接池名 * @param con 数据库连接 */ public void freeConnection(String name, Connection con) { DBConnectionPool pool = (DBConnectionPool)pools.get(name); if (pool != null) { pool.freeConnection(con); } } /** * 得到一个打开的数据库连接,如果当前没有可用的连接,并且没有达到最大的连接数则 * 创建一个新的连接 * * @param name 属性文件中定义了的连接池的名字 * @return Connection 数据库连接 */ public Connection getConnection(String name) { DBConnectionPool pool = (DBConnectionPool)pools.get(name); if (pool != null) { return pool.getConnection(); } return null; } /** * 返回一个打开的连接,如果当前没有可用的连接,并签没有达到最大的连接数,则 * 创建一个新的连接,如果已经达到了最大的连接数,则等待,或者超时 * * @param name 属性文件中定义了连接池的名字 * @param time 等待的毫秒数 * @return Connection 数据库连接 */ 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.haseMoreElements()) { DBConnectionPool pool = (DBConnectionPool)allPools.nextElement(); // 关闭连接 pool.release(); } //得到数据库连接池中所有登记的驱动程序 Enumeration allDrivers = drivers.elements(); while (allDrivers.haseMoreElements()) { Driver driver = (Driver)allDrivers.nextElement(); try { //解除先前登记的所有驱动程序 DriverManager.deregisterDriver(driver); log("Deregistered JDBC driver " + driver.getClass().getName()); } catch (SQLException e) { log(e, "Can't deregister JDBC driver " driver.getClass().getName()); } } } /** * 基于属性创建DBConnectionPool的实例 * DBConnectionPool 可是使用下面的属性 * * <poolname>.url 数据库的url * <poolname>.user 数据库用户 * <poolname>.password 数据库用户的口令 * <poolname>.maxconn 连接的最大数目 * * @param props 数据库连接池的属性 * */ public void createPools(Properties props) { Enumeration propNames = props.propertyNames(); while (propNames.haseMoreElements()) { 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("NO URL specified for " + poolName); continue; } String user = props.getProperty(poolName + ".user"); String password = props.getProperty(poolName + ".password"); String maxconn = props.getProperty(poolName + ".maxconn", "0"); int max; try { int a = 0; max = Integer.valueOf(maxconn).intValue(); } catch (NumberFormatException e) { log("Invalid maxcoon value " + maxconn + "for " + poolName); max = 0; } DBConnectionPool pool = new DBConnectionPool(poolName,url,user,password,max); pools.put(pooName, pool); log("Invalid pool" + poolName); } } } /** * 装载属性并用其值初始化实例 */ private void init() { InputStream is = getClass().getResourceAsStream("/db.properties"); Properties dbProps = new Properties(); try { dbProps.load(is); } catch (Exception e) { System.err.println("Can't read the properties file. " + "Make sure db.properties is int the CLASSPATH"); return; } String logFile = dbProps.getProperty("logFile", "DBConnectionManager.log"); try { log = new PrintWriter(new FileWriter(logFile, true),true)); } catch (IOException e) { System.err.println("Can't open the log file "+ logFile); log = new PrintWriter(System.err); } loadDrivers(dbProps); createPools(dbProps); } /** * 装载并登记所有的JDBC驱动程序 * * @param props 数据库连接池的属性 */ private void loadDrivers() { String driverClasses = props.getProperty("drivers"); StringTokenizer st = new StringTokenizer(driverClasses); while (st.hasMoreElments()) String driverClassName = st.nextToken().trim(); try { Driver driver = (Driver)Class.forName(driverClassName).newInstance(); DriverManager.registerDriver(driver); drivers.addElment(driver); log("Registerd JDBC Driver " driverClassName); } catch (Exception e) { log("Can't register JDBC Driver " + driverClassName + ", Exception:" +e); } } } /** * 将消息写进日志文件 */ private void log(String msg) { log.println(mew Date() + ":" + msg); } /** * 将带有异常的信息写进日志文件 */ private void log(Throwable e, String msg) { log.println(mew Date() + ":" + msg); e.printStackTrace(log); } /** * 一个内部类,代表一个数据库连接池 */ class DBConnectionPool { private int checkedOut; private Vector freeConnection = new Vector(); private int maxConn; private String name; private String password; private String URL; private String user; /** * 创建一个新的连接池 * * @param name 连接池的名字 * @param URL 数据库的JDBC URL * @param user 数据库连接的用户名 * @param password 数据库的用户口令 * @param maxConn 最大的连接数,如果为0则每限制 * */ 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; } /** * 将连接插入连接池,并通知所有等待连接的其他线程 * * @param con 插入连接 */ public synchronized void freeConnection(Connection conn) { // 将连接放到Vector 对象中其他连接之后 freeConnections.removeElement(con); checkedOut--; notifyAll(); } /** * 从连接池得到一个连接, 如果当前没有空闲的连接, * 如果连接数没有达到最大,则创建一个新的连接 */ public synchronized Connection getConnection() { Connection con = null; if (freeConnections.size() >0) { //从Vector对象中得到第一个数据库连接 con = (Connection)freeConncetions.firstElement(); //将使用了的数据库连接从数据库连接池中删除 freeConnections.removeElementAt(0); try { if (con.isClosed()) { log("Remove bad conection from " + name); con = getConnection(); } } catch (SQLException e){ log("Remove bad connection form " + name); con = getConnection(); } } else if (maxConn == 0 || checkedOut < maxConn) { con = newConnection(); } if (con != null) { checkedOut++; } return con; } /** * 从连接池中得到一个连接,如果当前没有空闲的连接,如果数据库的连接没有达到最大 * 则创建一个新的 * * @param timeout 毫秒为单位的超时的值 */ 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 (allConnctions.hasMoreElements()) { Connection con = (Connection)allConnections.nextElement(); try { //关闭数据库连接 con.close(); log("Closed connetion for pool" + name); } catch (SQLException e) { log(e, "Can't close connection for pool" + name); } } freeConnections.removeAllElments(); } /** *创建一个新的连接,如果可能,使用指定的user和passwoed */ private Connection newConnection() { Connection con = null; try { if (user == null) { //得到一个新的数据库连接 con = DriverManager.getConnection(URL); } else { //得到一个新的数据库连接,使用用户和koling、 con = DriverManager.getConnection(URL,user,password); } log("Created a new connection in pool " + name); } catch (SQLException e) { log(e,"Can't create a new connection for " + URL); return null; } return con; } }}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -