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;
/**
*
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.*;
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();
}
}
闽公网安备 35060202000074号