/*
* copyright (c) 2000-2002 jackliu
* <a href="mailto: service@cn-java.com">service@cn-java.com</a>
* www.cn-java.com all rights reserved
*/
package com.cn-java.database;
import java.util.*;
import java.sql.*;
/**
* <font size=4><b>数据库连接池</b></font>
* <font color=gray>这个类为系统提供一个数据库的连接池</font>
* <br><br>
* @see sys_config
* @author <a href="mailto: suntoday@eyou.com">jackliu</a>
*/
public class dbpoolsmanager{
static private dbpoolsmanager instance; // 唯一实例
dbpools pools;
driver thedrv;
/**
* 构造方法
* <br>创建数据库连接池(oracle oci)
*/
private dbpoolsmanager(){
/* use oracle oci8*/
string url = "jdbc:oracle:oci8:@"+sys_config.getdatabase_instance();
string user = sys_config.getdatabase_userid();
string password = sys_config.getdatabase_userpwd();
int max = integer.parseint(sys_config.getdata_session());
try{
/*use mysql driver
thedrv = new org.gjt.mm.mysql.driver();*/
/*use oracle driver*/
thedrv = new oracle.jdbc.driver.oracledriver();
drivermanager.registerdriver(thedrv);
}
catch ( sqlexception e ){
//debug to err.log
e.printstacktrace(system.err);
}
pools = new dbpools(url, user, password, max);
pools.showdetail();
}
/**
* 得到一个数据库的连接池管理的实例
* @return 返回一个数据库连接池管理的一个实例
*/
static synchronized public dbpoolsmanager getinstance(){
if (instance == null){
instance = new dbpoolsmanager();
}
return instance;
}
/**
* 从数据库连接池中获取一个空闲的数据库连接实例
* <br>如果超出连接池的最大连接,返回一个空的对象
* @return 返回一个数据库连接对象
*/
public connection getconnection(){
return pools.getconnection();
}
/**
* 释放一个正在工作的数据库连接到数据库连接池
* @param con connection 一个数据库连接
*/
public void freeconnection(connection con){
pools.freeconnection(con);
}
/**
* 关闭数据库连接池
*/
public void close(){
try{
pools.showdetail();
pools.close();
drivermanager.deregisterdriver(thedrv);
}
catch ( sqlexception e ){
//debug to err.log
e.printstacktrace(system.err);
}
}
class dbpools{
private vector freeconnections = new vector();
private vector currentconnections = new vector();
private int maxconn;
private string url;
private string password;
private string user;
/**
* 构造函数
* <br>创建一个数据库连接池
* @param url string jdbc url
* @param user string user id || null
* @param password string user pwd ||null
* @param maxconn int max connect
*/
public dbpools(string url, string user, string password, int maxconn){
this.url = url;
this.user = user;
this.password = password;
this.maxconn = maxconn;
}
/**
* 释放一个正在工作的数据库连接到数据库连接池
* @param con connection 一个数据库连接
*/
public synchronized void freeconnection(connection con){
//remove from freeconnection vector
currentconnections.remove(con);
freeconnections.add(con);
}
/**
* 从连接池获得一个可用连接.如没有空闲的连接且当前连接数小于最大连接
* 数限制,则创建新连接.如原来登记为可用的连接不再有效,则从向量删除之,
* 然后递归调用自己以尝试新的可用连接.
* @return 返回一个数据库连接对象
*/
public synchronized connection getconnection(){
connection con = null;
if (freeconnections.size() > 0) {
// 获取向量中第一个可用连接
con = (connection) freeconnections.firstelement();
freeconnections.removeelementat(0);
try{
if (con.isclosed()){
//递归调用自己,尝试再次获取可用连接
con = getconnection();
}
currentconnections.add(con);
}
catch (sqlexception e){
//递归调用自己,尝试再次获取可用连接
con = getconnection();
}
}
else if (maxconn == 0 || currentconnections.size() < maxconn){
con = newconnection();
}
return con;
}
/**
* 关闭数据库连接池
*/
public void close(){
for ( int i=0; i<freeconnections.size(); i++ ){
try{
((connection)freeconnections.get(i)).close();
}
catch( sqlexception e ){
//debug to err.log
e.printstacktrace(system.err);
}
}
for ( int i=0; i<currentconnections.size(); i++ ){
try{
((connection)currentconnections.get(i)).close();
}
catch( sqlexception e ){
//debug to err.log
e.printstacktrace(system.err);
}
}
}
/**
* 创建新的连接
*/
private synchronized connection newconnection(){
oads_log.tofileln("dbpool create new!!!");
//showdetail();
connection con = null;
try{
if (user == null){
con = drivermanager.getconnection(url);
}
else{
con = drivermanager.getconnection(url, user, password);
}
currentconnections.add(con); }
catch (sqlexception e){
return null;
}
return con;
}
/**
* 显示数据库连接池的状态信息
*/
public void showdetail(){
oads_log.tofileln("current db connections : " + currentconnections.size());
oads_log.tofileln("free db connections : " + freeconnections.size());
}
}
}
闽公网安备 35060202000074号