codefans

导航

<2024年12月>
24252627282930
1234567
891011121314
15161718192021
22232425262728
2930311234

统计

常用链接

留言簿(2)

随笔分类

随笔档案

文章分类

文章档案

程序设计链接

搜索

最新评论

阅读排行榜

评论排行榜

一个简单的pool代码

the detail pool management code

DBConnectionManager.java

package com.coa.cim.database;

/**
* <p>Title: CIM SYSTEM</p>
* <p>Description: The Customer Infomation Managment System</p>
* <p>Copyright: Copyright (c) 2002</p>
* <p>Company: COA Sci&Tech</p>
* @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;

/**
* <p>Title: CIM SYSTEM</p>
* <p>Description: The Customer Infomation Managment System</p>
* <p>Copyright: Copyright (c) 2002</p>
* <p>Company: COA Sci&Tech</p>
* @author Mula Liu
* @version 1.0
*/

import java.sql.*;
import java.util.*;

public class DBConnectionPool {

private String dbUserName;
private String dbPassword;
private String connectionURL;
private int maxConnection;
private Vector freeConnections;
private int checkedOut;

public DBConnectionPool(String userName,String password,String url,int maxConn) {
this.dbUserName=userName;
this.dbPassword=password;
this.connectionURL=url;
this.maxConnection=maxConn;
freeConnections=new Vector();
}// initalize

public synchronized Connection getDBConnection(){
Connection conn=null;
if(freeConnections.size() > 0){
conn=(Connection)freeConnections.elementAt(0);
freeConnections.removeElementAt(0);
try{
if(conn.isClosed()){
conn=getDBConnection();
}
}catch(SQLException ex){
conn=getDBConnection();
}
}else if(maxConnection==0 || checkedOut < maxConnection){
conn=newDBConnection();
}
if(conn!=null){
checkedOut++;
}
return(conn);
}// using FIFO method to get connection instance

public synchronized Connection getDBConnection(long timeout){
long startTime=new java.util.Date().getTime();
Connection conn;
while((conn=getDBConnection())==null){
try{
wait(timeout);
}catch(InterruptedException ex){}
if(new java.util.Date().getTime()-startTime >= timeout){
return(null);
}
}
return conn;
}

public Connection newDBConnection(){
Connection conn=null;
try{
if(dbUserName==null){
conn=DriverManager.getConnection(connectionURL);
}else{
conn=DriverManager.getConnection(connectionURL,dbUserName,dbPassword);
}
}catch(SQLException ex){
ex.printStackTrace();
}
return(conn);
}

public synchronized void freeDBConnection(Connection conn){
freeConnections.addElement(conn);
checkedOut--;
notifyAll();
}

public synchronized void release(){
Enumeration allConnections=freeConnections.elements();
while(allConnections.hasMoreElements()){
try{
Connection conn=(Connection)allConnections.nextElement();
conn.close();
}catch(SQLException ex){
ex.printStackTrace();
}
}
freeConnections.removeAllElements();
}

}

posted on 2005-11-22 11:23 春雷的博客 阅读(117) 评论(0)  编辑  收藏


只有注册用户登录后才能发表评论。


网站导航: