连接池管理的代码
2008-01-05 10:43:19 来源:WEB开发网核心提示:the detail pool management code DBConnectionManager.java package com.coa.cim.database; /** * Title: CIM SYSTEM * Description: The Customer Infomation Managment
the detail pool management code
DBConnectionManager.java
package com.coa.cim.database;
/**
*
Title: CIM SYSTEM
*
Description: The Customer Infomation Managment System
*
Copyright: Copyright (c) 2002
*
Company: COA Sci&Tech
* @author Mula Liu
* @version 1.0
*/
import java.sql.*;
import java.util.*;
import java.io.*;
public class DBConnectionManager {
PRivate static DBConnectionManager instance=null;
private DBConnectionPool pool;
private static int client;
private Properties dbProps;
private Vector drivers;
public DBConnectionManager() {
init();
}
public synchronized static DBConnectionManager getInstance(){
if(instance==null){
instance=new DBConnectionManager();
}
client++;
return(instance);
} //create an instance of connection manager. if exits ,just returen the instance
void init(){
drivers=new Vector();
InputStream is=this.getClass().getResourceAsStream("../resource/Res.properties");
try{
dbProps=new Properties();
dbProps.load(is);
}catch(Exception ex){
System.out.println("Miss Resource File "+ex.getMessage());
}
loadDriver();
createPool();
} //using Properties.load() method to locate outter properties file
public void loadDriver(){
String driverClasses=dbProps.getProperty("dbDriver");
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);
}catch(Exception ex){
ex.printStackTrace();
}
}
} //parse the file, load mutil driver class in
public void createPool(){
String userName=dbProps.getProperty("dbUserName");
String passWord=dbProps.getProperty("dbPassword");
String url=dbProps.getProperty("connectionURL");
int maxConn;
try{
maxConn=Integer.valueOf(dbProps.getProperty("maxConnection","0")).intValue();
}catch(NumberFormatException ex){
maxConn=0;
}
pool=new DBConnectionPool(userName,password,url,maxConn);
} //parse the file, load username,password,url and maxconnection in
public synchronized int getClientCount(){
return(client);
}
public Connection getDBConnection(){
if(pool != null){
return(pool.getDBConnection());
}
return(null);
}//act as facade
public Connection getDBConnection(long timeout){
if(pool != null){
return(pool.getDBConnection(timeout));
}
return(null);
}//act as facade
public void freeDBConnection(Connection conn){
if(pool != null){
pool.freeDBConnection(conn);
}
}//act as facade
public void realse(){
if(this.client != 0){
return;
}
if(pool != null){
pool.release();
Enumeration enum=drivers.elements();
while(enum.hasMoreElements()){
Driver driver=(Driver)enum.nextElement();
try{
DriverManager.deregisterDriver(driver);
}catch(Exception ex){
System.out.println("Can not deregister driver "+driver.getClass().getName());
}
}
}
}//act as facade then de register driver
}
________________________________________
DBConnectionPool.java
package com.coa.cim.database;
/**
*
- ››管理/var/spool/clientmqueue/下的大文件
- ››连接各种数据库的URL和DRIVER
- ››连接上192.168.1.1没账号输入框
- ››代码隐藏文件如何与页面链接
- ››管理私有云,第 2 部分: 使用 WebSphere CloudBur...
- ››连接池:深入 J2EE 的连接合用
- ››代码格式化脚本CodeFormat
- ››管理 Eclipse 环境:Eclipse 维护的神秘艺术
- ››管理私有云:WebSphere CloudBurst Appliance 命令...
- ››管理启动项 - Windows 7中的BCDEDIT命令
- ››管理Exchange 2003注意事项
- ››代码注入的三种方法
更多精彩
赞助商链接