📄 dbconnectionmanager.java
字号:
package eise.db;//------------------------------------------------------------------------------import java.io.*;import java.sql.*;import java.util.*;//------------------------------------------------------------------------------public class DBConnectionManager{ private static DBConnectionManager instance; private static int clients;//使用此连接池的DbConnect的数目 private Vector drivers;//驱动程序列表 private PrintWriter log;//日志文件 private Hashtable pools;//连接池//------------------------------------------------------------------------------ private DBConnectionManager(){ drivers = new Vector(); pools = new Hashtable(); init(); }//------------------------------------------------------------------------------ private void init(){ java.io.InputStream inputstream = getClass().getResourceAsStream("db_pool.properties"); Properties properties = new Properties(); try{properties.load(inputstream);} catch(Exception ee) {System.err.println("加载db.properties文件时出错:"+ee.toString()); return; } String log_file_name= properties.getProperty("logfile", "DBConnectionManager.log"); try{log = new PrintWriter(new FileWriter(log_file_name, true), true);} catch(IOException ee) {System.err.println("打开LOG文件时出错: " + log_file_name); log = new PrintWriter(System.err); } loadDrivers(properties); createPools(properties); }//------------------------------------------------------------------------------ private void loadDrivers(Properties properties){ String driver_items = properties.getProperty("drivers"); for(StringTokenizer stringtokenizer = new StringTokenizer(driver_items); stringtokenizer.hasMoreElements();) {String driver_item = stringtokenizer.nextToken().trim(); try{Driver driver = (Driver)Class.forName(driver_item).newInstance(); DriverManager.registerDriver(driver); drivers.addElement(driver); log("成功加载数据库驱动程序:" + driver_item); } catch(Exception ee) {log("加载数据库驱动程序时出错: " + driver_item + ";错误信息是: " + ee.toString()); } } }//------------------------------------------------------------------------------ private void createPools(Properties properties){ for(Enumeration enumeration = properties.propertyNames(); enumeration.hasMoreElements();) {String db_url_name=(String)enumeration.nextElement(); if(db_url_name.endsWith(".url")) {String db_name = db_url_name.substring(0, db_url_name.lastIndexOf(".")); String db_url = properties.getProperty(db_name + ".url"); if(db_url == null) {log("加载数据库项目时出错:" + db_name); } else {String db_user = properties.getProperty(db_name + ".user"); String db_password = properties.getProperty(db_name + ".password"); String db_max = properties.getProperty(db_name + ".maxconn", "0"); int max_int; try{max_int = Integer.valueOf(db_max).intValue();} catch(NumberFormatException _ex) {log("最大连接数目的参数有误: " + db_max + "所属数据库类型为: " + db_name); max_int = 0; } DBConnectionPool dbconnectionpool = new DBConnectionPool(this,db_name, db_url, db_user, db_password, max_int); pools.put(db_name, dbconnectionpool); log("成功创建数据库连接池,数据库的类型为:" + db_name); } } } }//------------------------------------------------------------------------------ public void freeConnection(String db_name, Connection connection){ DBConnectionPool dbconnectionpool = (DBConnectionPool)pools.get(db_name); if(dbconnectionpool != null){dbconnectionpool.freeConnection(connection);} }//------------------------------------------------------------------------------ public Connection getConnection(String db_name){ DBConnectionPool dbconnectionpool = (DBConnectionPool)pools.get(db_name); if(dbconnectionpool != null){return dbconnectionpool.getConnection();} else{return null;} }//------------------------------------------------------------------------------ public Connection getConnection(String db_name, long limit_long){ DBConnectionPool dbconnectionpool = (DBConnectionPool)pools.get(db_name); if(dbconnectionpool != null){return dbconnectionpool.getConnection(limit_long);} else{return null;} }//------------------------------------------------------------------------------ public static synchronized DBConnectionManager getInstance(){ if(instance == null){instance = new DBConnectionManager();} clients++; return instance; }//------------------------------------------------------------------------------ public void log(String messageStr){ log.println("日志记录:时间- "+String.valueOf(new java.util.Date()) + " 内容: " + messageStr); }//------------------------------------------------------------------------------ public void log(Throwable throwable, String messageStr){ log.println("日志记录:时间- "+String.valueOf(new java.util.Date()) + " 内容: " + messageStr); throwable.printStackTrace(log); }//------------------------------------------------------------------------------ public synchronized void release(){ if(--clients != 0){return;} DBConnectionPool dbconnectionpool; for(Enumeration enumeration = pools.elements(); enumeration.hasMoreElements(); dbconnectionpool.release()) {dbconnectionpool = (DBConnectionPool)enumeration.nextElement(); } for(Enumeration enumeration1 = drivers.elements(); enumeration1.hasMoreElements();) {Driver driver = (Driver)enumeration1.nextElement(); try{DriverManager.deregisterDriver(driver); log("成功注销数据库驱动:" + driver.getClass().getName()); } catch(SQLException ee) {log(ee, "注销数据库驱动时出错: " + driver.getClass().getName()); } } }//------------------------------------------------------------------------------}//end of class//------------------------------------------------------------------------------
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -