自写数据库连接池

前段时间因为不小心删掉了这篇文章,经高人指点,决定再次粘上来。也由衷地感谢这位高人。企业项目中的数据库连接池都是已经写好,程序员直接拿来用就可以了。自己写一下算是了解一下底层的东西吧!写得不好的地方,希望大家多多指教!

public class DbConnectionManager {
private static DbConnectionManager dbConnectionManager= null;
private static int clientCount = 0;
private static Vector<Driver> drivers = new Vector<Driver>();//存放数据库驱动
//pools存放数据库连接池
private static Hashtable<String,DbConnectionPool> pools = new Hashtable<String,DbConnectionPool>();
private PrintWriter log = null;

private DbConnectionManager(){
init();
}
/**
* 〈得到DbConnectionManager实例<单例模式>〉
*/
public static synchronized DbConnectionManager getInstance(){
if(null == dbConnectionManager){
dbConnectionManager = new DbConnectionManager();
}
clientCount++;
return dbConnectionManager;
}
/**
* 〈获取连接〉
*/
public synchronized Connection getConnection(String name){
DbConnectionPool dbConnectionPool = pools.get(name);
Connection connection = null;
if(null != dbConnectionPool){
connection = dbConnectionPool.getConnection();
}
return connection;
}
/**
* 〈释放连接〉
*/
public void freeConnection(String name,Connection connection){
DbConnectionPool dbConnectionPool = pools.get(name);
if(null != dbConnectionPool){
dbConnectionPool.freeConnection(connection);
}
}
/**
* 〈加载数据库属性文件〉
*/
private void init(){
InputStream inputStream = this.getClass().getResourceAsStream("/db.properties");
Properties properties = new Properties();
try {
properties.load(inputStream);
} catch (IOException e) {
System.err.println("Can't read the properties file. " + "Make sure db.properties is in the CLASSPATH");
return;
}
String logFile = properties.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(properties);//加载驱动
createPool(properties);//创建数据库连接池
}
/**
* 〈创建连接池〉
*/

private void createPool(Properties properties) {
Enumeration propertyNames = properties.propertyNames();
String propertyName = null;
while(propertyNames.hasMoreElements()){
propertyName = (String) propertyNames.nextElement();
if(propertyName.endsWith("url")){
String poolName = propertyName.substring(0, propertyName.lastIndexOf("."));
String url = properties.getProperty(propertyName);
String userName = properties.getProperty(poolName+".userName");
String password = properties.getProperty(poolName+".password");
String maxConn = properties.getProperty(poolName+".maxConn");
int max = 0;
try{
max = Integer.parseInt(maxConn);
}catch(NumberFormatException e){
max = 0;
}
DbConnectionPool dbConnectionPool = new DbConnectionPool(
poolName,url,userName,password,max);
pools.put(poolName, dbConnectionPool);

}
}
}
/**
* 〈释放连接池〉
*/
public void freePools(){
if(0 == --clientCount){
Enumeration<DbConnectionPool> allPools = pools.elements();
DbConnectionPool dbConnectionPool = null;
while(allPools.hasMoreElements()){
dbConnectionPool = allPools.nextElement();
dbConnectionPool.freeConnections();
}
Enumeration<Driver> allDrivers = drivers.elements();
while (allDrivers.hasMoreElements()) {
Driver driver = (Driver) allDrivers.nextElement();
try {
DriverManager.deregisterDriver(driver);
} catch (SQLException e) {
}
}
}
}
/**
* 〈加载驱动〉
*/
private void loadDrivers(Properties properties){
String driverClass = properties.getProperty("drivers");
Driver driver = null;
try {
driver = (Driver) Class.forName(driverClass).newInstance();
} catch (InstantiationException e) {
e.printStackTrace();
} catch (IllegalAccessException e) {
e.printStackTrace();
} catch (ClassNotFoundException e) {
e.printStackTrace();
}
drivers.addElement(driver);
}


private class DbConnectionPool{
private String poolName;
private String url;
private String userName;
private String password;
private int maxConn;
private final Vector<Connection> freeConnections = new Vector<Connection>();
private int checkedOut;
public DbConnectionPool(String poolName,String url,String userName,String password,int maxConn){
this.poolName = poolName;
this.url = url;
this.userName = userName;
this.password = password;
this.maxConn = maxConn;
}
/**
* 〈获取连接〉
*/
public Connection getConnection() {
Connection connection = null;
if(freeConnections.size()>0){
connection = freeConnections.firstElement();
freeConnections.remove(0);
try {
if (connection.isClosed()) {
connection = getConnection();
}
} catch (SQLException e) {
connection = getConnection();
}
}else if((0 == maxConn)|| checkedOut < maxConn){
connection = newConnection();
}
if(null != connection){
checkedOut++;
}
return connection;
}
/**
* 〈释放单个连接〉
*/
public void freeConnection(Connection connection) {
freeConnections.addElement(connection);
checkedOut--;
notifyAll();
}
/**
* 〈创建连接〉
*/
public Connection newConnection() {
Connection connection = null;
try {
if(null == userName){
DriverManager.getConnection(url);
}else {
connection = DriverManager.getConnection(url, userName, password);
}
} catch (SQLException e) {
e.printStackTrace();
}
return connection;
}
/**
* 〈释放所有连接〉
*/
public void freeConnections() {
Enumeration<Connection> connections = freeConnections.elements();
Connection connection = null;
while(connections.hasMoreElements()){
connection = connections.nextElement();
try {
connection.close();
} catch (SQLException e) {
e.printStackTrace();
}
}
freeConnections.removeAllElements();
}
}
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值