import java.io.*;
import java.sql.*;
import java.util.*;
import java.util.Date;
/**
* 管理类DBConnectionManager支持对一个或多个由属性文件定义的数据库连接
* 池的访问.客户程序可以调用getInstance()方法访问本类的唯一实例.
* Copyright: Copyright (c) 2003
* Company: www.allfu.com
* @author: 辛志强
* @version 1.0
*/
public class DBConnectionManager
{
static private DBConnectionManager instance;
static private int clients = 0; private Vector drivers = new Vector();
private PrintWriter log;
private Hashtable pools = new Hashtable(); /**
* 返回唯一实例.如果是第一次调用此方法,则创建实例
*
* @return DBConnectionManager 唯一实例
*/
static synchronized public DBConnectionManager getInstance()
{
if (instance == null)
{
instance = new DBConnectionManager();
//System.out.println("instance == null get new DBConnection Manager()");
}
clients++; //System.out.println("instance != null get DBConnection Manager(), clients is : " + clients);
return instance;
}
/**
* 建构函数私有以防止其它对象创建本类实例
*/
private DBConnectionManager()
{
init();
}
/**
* 将连接对象返回给由名字指定的连接池
*
* @param name 在属性文件中定义的连接池名字
* @param con 连接对象
*/
import java.sql.*;
import java.util.*;
import java.util.Date;
/**
* 管理类DBConnectionManager支持对一个或多个由属性文件定义的数据库连接
* 池的访问.客户程序可以调用getInstance()方法访问本类的唯一实例.
* Copyright: Copyright (c) 2003
* Company: www.allfu.com
* @author: 辛志强
* @version 1.0
*/
public class DBConnectionManager
{
static private DBConnectionManager instance;
static private int clients = 0; private Vector drivers = new Vector();
private PrintWriter log;
private Hashtable pools = new Hashtable(); /**
* 返回唯一实例.如果是第一次调用此方法,则创建实例
*
* @return DBConnectionManager 唯一实例
*/
static synchronized public DBConnectionManager getInstance()
{
if (instance == null)
{
instance = new DBConnectionManager();
//System.out.println("instance == null get new DBConnection Manager()");
}
clients++; //System.out.println("instance != null get DBConnection Manager(), clients is : " + clients);
return instance;
}
/**
* 建构函数私有以防止其它对象创建本类实例
*/
private DBConnectionManager()
{
init();
}
/**
* 将连接对象返回给由名字指定的连接池
*
* @param name 在属性文件中定义的连接池名字
* @param con 连接对象
*/
{
DBConnectionPool pool = (DBConnectionPool) pools.get(name);
if (pool != null)
{
pool.freeConnection(con);
}
else
{
log("pool is null .......");
}
} /**
* 获得一个可用的(空闲的)连接.如果没有可用连接,且已有连接数小于最大连接数
* 限制,则创建并返回新连接
*
* @param name 在属性文件中定义的连接池名字
* @return Connection 可用连接或null
*/
public Connection getConnection(String name)
{
DBConnectionPool pool = (DBConnectionPool) pools.get(name);
if (pool != null)
{
return pool.getConnection();
}
else
{
System.out.println("pool == null in getConnection(String name)");
}
return null;
} /**
* 获得一个可用连接.若没有可用连接,且已有连接数小于最大连接数限制,
* 则创建并返回新连接.否则,在指定的时间内等待其它线程释放连接.
*
* @param name 连接池名字
* @param time 以毫秒计的等待时间
* @return Connection 可用连接或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()
{
// 等待直到最后一个客户程序调用
log("release Starting clients is : " + clients);
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("撤销JDBC驱动程序 " + driver.getClass().getName()+"的注册");
}
catch (SQLException e)
{
log(e, "无法撤销下列JDBC驱动程序的注册: " + driver.getClass().getName());
}
}
System.out.println("release End ....");
} /**
* 根据指定属性创建连接池实例.
*
* @param 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("没有为连接池" + poolName + "指定URL");
continue;
}
String user = props.getProperty(poolName + ".user");
String password = props.getProperty(poolName + ".password");
String maxconn = props.getProperty(poolName + ".maxConns", "0");
String minconn = props.getProperty(poolName + ".minConns", "0");
String str_logInterval=props.getProperty(poolName + ".logInterval");
log("str_logInterveal="+str_logInterval);
log("minconn="+minconn);
log("maxconn="+maxconn);
int max, min;
long logInterval;
try
{
max = Integer.valueOf(maxconn).intValue();
min = Integer.valueOf(minconn).intValue();
logInterval = Long.valueOf(str_logInterval).longValue();
}
catch (NumberFormatException e)
{
log("错误的参数数限制:连接池: " + poolName);
max = 0; min = 0; logInterval=2000;
instance = null; // WL()
return; // WL() } DBConnectionPool pool = new DBConnectionPool(poolName, url, user, password, max, min, logInterval);
pools.put(poolName, pool);
log("成功创建连接池" + poolName);
}//end if
}//end while
}//end creatpools /**
* 读取属性完成初始化
*/
private void init()
{
InputStream is = getClass().getResourceAsStream("db.properties");
Properties dbProps = new Properties();
try
{
dbProps.load(is);
}
catch (Exception e)
{
System.err.println("不能读取属性文件. " + "请确保db.properties在CLASSPATH指定的路径中");
return;
}
String logFilepath = dbProps.getProperty("logfile", "DBConnectionManager.log"); ////////////////////////////////////////////////////////////////////////////////
//制作日志文件
//如果当前日志文件大于某数值是删除原有日志
//建立新的日志文件 File logFile = null;
FileWriter fileWriter=null;
try
{
logFile=new File(logFilepath);
//System.out.println("logFile.length()=" + logFile.length()); if (logFile.length() > 1024)
{
logFile.delete();
logFile=new File(logFilepath);
}
fileWriter = new FileWriter(logFile,true);
log = new PrintWriter(fileWriter, true);
}
catch(IOException e)
{
System.err.println("无法打开日志文件: " + logFilepath);
log = new PrintWriter(System.err);
} /*try {
log = new PrintWriter(new FileWriter(logFile, true), true);
}
catch (IOException e) {
System.err.println("无法打开日志文件: " + logFile);
log = new PrintWriter(System.err);
}
*/
///////////////////////////////////////////////////////////////////////////////////
loadDrivers(dbProps);
createPools(dbProps);
} /**
* 装载和注册所有JDBC驱动程序
* @param props 属性
*/