我写的一个数据库连接池的源代码,请多提宝贵意见以便修改!

oicu 2003-05-16 02:20:59
package dbutil;

import java.io.*;
import java.sql.Connection;
import java.sql.Driver;
import java.sql.DriverManager;
import java.sql.SQLException;
import java.util.Date;
import java.util.Enumeration;
import java.util.Properties;
import java.util.Vector;

/**
* 数据库连接池
*/
public class DBConnectionPool implements Serializable
{
/**
* 连接池的实例
*/
static private DBConnectionPool instance;

/**
* 正在被使用数据库连接个数
*/
private int checkedOut;

/**
* 保存空闲数据库连接的向量
*/
private Vector freeConnections;

/**
* 允许取得的最大的连接个数
*/
private int max;

/**
* 数据库密码
*/
private String password;

/**
* 数据库主机地址
*/
private String url;

/**
* 数据库用户名
*/
private String user;


/**
* 供外部调用的取得数据库连接的方法
* @param 可用的数据库连接
*/
public static synchronized Connection getConnection()
{
if (instance == null)
{
instance = new DBConnectionPool();
instance.init();
}
return instance.getConn();
}

/**
*读取配置文件内容
*/
private void init()
{
InputStream is = getClass().getResourceAsStream("./db.conf");
Properties dbProps = new Properties();
String filename = null;
freeConnections = new Vector();
try
{
dbProps.load(is);
}catch (Exception e)
{
System.err.println("Can not read config file!");
return;
}
url = dbProps.getProperty("url");
user = dbProps.getProperty("user","root");
password = dbProps.getProperty("password");
String maxConn = dbProps.getProperty("maxconn","50");
try
{
max = Integer.parseInt(maxConn);
}catch(Exception e){}
String driverClass = dbProps.getProperty("driver");
try
{
Object obj = Class.forName(driverClass).newInstance();
DriverManager.registerDriver((Driver)obj);
}catch(Exception e)
{
System.out.println("Can not load driver:"+driverClass);
}
}

/**
* 取得一个空闲连接或者新建一个连接
*/
private Connection getConn()
{
Connection con = null;
if(freeConnections.size() > 0)
{
con = (Connection) freeConnections.firstElement();
freeConnections.removeElementAt(0);
try
{
if (con.isClosed())
{
con = newConn();
}
}catch (SQLException e)
{
return null;
}
}else
{
if (checkedOut >= max)
{
return null;
}
con = newConn();
}
if (con != null)
{
checkedOut++;
}
return con;
}

/**
* 建立一个新的数据库连接
*/
private Connection newConn()
{
Connection con = null;
try
{
if (user == null)
{
con = DriverManager.getConnection(url);
}else
{
con = DriverManager.getConnection(url, user, password);
}
}catch (SQLException e)
{
e.printStackTrace();
return null;
}
return con;
}

/**
* 释放一个使用完了的数据库连接
*/
public static synchronized void freeConnection(Connection con)
{
if (instance == null)
{
instance = new DBConnectionPool();
instance.init();
}
instance.freeConn(con);
}

/**
* ConnectionManager实例释放连接
*/
private void freeConn(Connection con)
{
freeConnections.addElement(con);
checkedOut--;
}
}
///:~

附:
配置文件内容例子:
url=jdbc:mysql://localhost/testdb
driver=org.gjt.mm.mysql.Driver
user=root
password=
maxconn=60
output=false

使用方法:
...
Connection con = DBConnectionPool.getConnection();
...
DBConnectionPool.freeConnection(con);
...
...全文
81 7 打赏 收藏 转发到动态 举报
写回复
用AI写文章
7 条回复
切换为时间正序
请发表友善的回复…
发表回复
oicu 2003-05-18
  • 打赏
  • 举报
回复
这个我本人完全同意的,
它是相当简单的。
我把它用于小的项目,
大的项目就用商业化的工具了。
oicu 2003-05-17
  • 打赏
  • 举报
回复
很感谢你的回复,
并且希望能够得到宝贵意见,
我的愿意就是希望能够得到各位Java爱好者的支持把它改进,
因为我自己是很需要它.
oicu 2003-05-17
  • 打赏
  • 举报
回复
你说的网络上的连接池不是我写的,
可能是数据库连接池的程序都很相似的原因,
我这个应该算是很简单的一种.
至于测试方面,
我已经用它放进了自己参与的多个项目,
都还是运行正常,
至于强度测试,
鉴于笔者的能力,
恐怕是不能应付太大强度的要求.
csrcom 2003-05-17
  • 打赏
  • 举报
回复
楼主,不知道网络上面的哪个程序是否是你写的。几乎是一样的,你这个代码我还没有测试过。
但是很遗憾,我测试了网络上面的那段mysql连接池的代码。简直无法相信,我不知道笔者是否经过自己的单元测试,也无法考证笔者是否经过强度测试。

dkmilk 2003-05-17
  • 打赏
  • 举报
回复
可能简单了点。用于大的项目不行,还要改进
oicu 2003-05-17
  • 打赏
  • 举报
回复
那么请你发发善心嘛,
看在大家都用java的份上啊,
谢谢!
takecare 2003-05-17
  • 打赏
  • 举报
回复
其中有不少不足的地方,可惜就只有0分,哎...

23,405

社区成员

发帖
与我相关
我的任务
社区描述
Java 非技术区
社区管理员
  • 非技术区社区
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧