一个关于数据库连接池的问题(欢迎进来讨论)?

fzhang 2003-10-09 09:35:45
我写了一个数据库连接池,缺少了一个方法,谁能帮帮忙?
设计思想如下:
1.初始话时根据最大连接数生成可用连接。(已实现)
2.获得连接。
3.返回连接。
4.释放连接。
5.用户取得连接后一定时间要强制归还( waitTime )( 未能实现 )

问题:
1.谁能告诉我如何跟踪获得的每一个连接的状态?
2.如何强制用户归还连接?

以下是我的原代码:


public class ConnManager
{
/*
** 全局变量声明区。
** 变量名 含义
** maxConn 连接池最多连接数
** waitTime 线程等待的时间
** user 数据库用户名
** password 数据库密码
** url 数据库连接字符串
** connAmount 在使用的连接的个数
** connVector Vector容器对象用与保存数据库的连接。
*/

int maxConn = 10;
int waitTime = 8000;
int connAmount = 0;
String user = "scott";
String password = "tiger";
String url = "jdbc:oracle:thin:@localhost:1521:zf01";
Vector connVector = new Vector();


Hashtable connHashtable = new Hashtable();

public static ConnManager instance;

/*
** 返回唯一实例。如果是第一次调用此方法,则创建该实例。
** (保证连接池只生成一个实例。)
*/
public static synchronized ConnManager getInstance()
{
if ( instance == null )
{
instance=new ConnManager();
}
return instance;
}

/*
** 构造函数实现类的初始化
** 功能:注册驱动程序,根据最大连接数初始化连接。
*/
private ConnManager()
{
try
{
DriverManager.registerDriver( new oracle.jdbc.driver.OracleDriver() );
}
catch( Exception e )
{
System.out.println( "无法注册驱动程序" );
}
newConnection(); //调用 newConnection 方法初始化连接池。
}

/*
** 将不再使用的连接归还给连接池。
*/
public synchronized void freeConnection( Connection con )
{
connVector.addElement( con ); //调用 Vector容器类的push方法来实现归还连接的功能。
connAmount ++;
notifyAll(); //唤醒正在等待的进程。
}


/*
** 从连接池获得一个可用的连接。
*/
public synchronized Connection getConnection()
{
Connection con = null;
long startTime = new Date().getTime();
if ( !connVector.isEmpty() )
{
con = ( Connection )connVector.firstElement(); //从连接池中取得连接,此处调用了
connVector.removeElementAt( 0 );
//System.out.println(startTime);
//此处如何记录用户在什么时候取走哪一个连接。以便定时能够强制归还?
connAmount --;
} //Vector容器类的pop方法来实现。
else
{
try
{
wait( waitTime ); //线程等待状态,等待连接。
return getConnection(); //利用递归,检查连接状态。
}
catch( InterruptedException ie )
{}
}
return con;
}


/*
** 创建新的连接,初始化连接池。
*/
private Connection newConnection()
{
Connection con = null;
try
{
for ( int i = 1; i < maxConn; i ++ )
{
if ( connAmount < maxConn )
{
con = DriverManager.getConnection( url, user, password );
connVector.addElement( con );
connAmount ++;
}
//System.out.println(String.toString(connAmount));
}
}
catch( SQLException e )
{
return null;
}
return con;
}
}
...全文
108 4 打赏 收藏 转发到动态 举报
写回复
用AI写文章
4 条回复
切换为时间正序
请发表友善的回复…
发表回复
Onega 2003-10-12
  • 打赏
  • 举报
回复
你研究一下aparche得连接池吧
http://jakarta.apache.org/site/sourceindex.cgi
JamesGosling 2003-10-09
  • 打赏
  • 举报
回复
up
fzhang 2003-10-09
  • 打赏
  • 举报
回复
to cuihao(java_豆):
能加上注释吗?偶是菜鸟啊!
cuihao 2003-10-09
  • 打赏
  • 举报
回复
TimerTask
Timer
public class RefreshConnStateTask extends TimerTask {

/**
* DBConnectionPool instance
*/
private DBConnectionPool _connectionPool = null;

/**
* write trace
*/
// private Log _log = null;

/**
* constructor
*/
public RefreshConnStateTask() {
this._connectionPool = DBConnectionPool.getInstance();
// _log = this._connectionPool.getLog();
}

/**
* refresh connection state
*/
public void run() {
synchronized(this._connectionPool.getLock()) {
Vector statVec = this._connectionPool.getUsedConnVec();
long lIncreasement = this._connectionPool.getTimerPeriod();
ConnectionState connStat = null;
Connection conn = null;
long lInterval = 0;

// refresh each connection state in _usedConnVec
for(int i = 0; i < statVec.size(); i++){
connStat = (ConnectionState)statVec.get(i);
connStat.incInterval(lIncreasement);

// release connection which is overtime
lInterval = connStat.getInterval();
if(lInterval >= this._connectionPool.getConnTimeLimit()) {
conn = connStat.getConnection();
this._connectionPool.releaseConnection(conn);
}
}
}
}
}

public void startTimer(){
// start refresh connection state timer
this._refConnStatTask = new RefreshConnStateTask();
this._refConnStatTimer = new Timer();
this._refConnStatTimer.schedule(this._refConnStatTask,
this._timerPeriod,
this._timerPeriod);
Log.logInformation("Connection status refresh timer started successfully.");
}

定时刷新查看连接时间

67,512

社区成员

发帖
与我相关
我的任务
社区描述
J2EE只是Java企业应用。我们需要一个跨J2SE/WEB/EJB的微容器,保护我们的业务核心组件(中间件),以延续它的生命力,而不是依赖J2SE/J2EE版本。
社区管理员
  • Java EE
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

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