如何设置连接池 ?

nanfang 2003-09-13 11:57:45
我用的是TOMCAT4.0+mysql+JSP,现在想做一个工作就是联接池,我的数据库是:kmplat,请问我该如何设置和使用!?
...全文
108 7 打赏 收藏 转发到动态 举报
写回复
用AI写文章
7 条回复
切换为时间正序
请发表友善的回复…
发表回复
nanfang 2003-09-14
  • 打赏
  • 举报
回复
大哥们多提意见吧!我试了多次不行
seaman0916 2003-09-13
  • 打赏
  • 举报
回复
我觉得有些参数也可以通过配置文件呀! 很方便的!
nanfang 2003-09-13
  • 打赏
  • 举报
回复
多谢,.邮箱地址:mailforme@ynmail.com
xinshou1979330 2003-09-13
  • 打赏
  • 举报
回复
Tomcat中SERVER.XML配置:
<Context path="/bizcity" docBase="bizcity" debug="0">
<Resource name="jdbc/bizcity" auth="Container" type="javax.sql.DataSource" />
<ResourceParams name="jdbc/bizcity">
<parameter><name>factory</name>
<value>org.apache.commons.dbcp.BasicDataSourceFactory</value>
</parameter>
<parameter><name>driverClassName</name> <value>com.microsoft.jdbc.sqlserver.SQLServerDriver</value>
</parameter>
<parameter><name>url</name><value>jdbc:microsoft:sqlserver://192.168.10.1:1433;DataBaseName=BizCity</value>
</parameter>
<parameter><name>username</name><value>sa</value></parameter>
<parameter><name>password</name><value>123456</value></parameter>
<parameter><name>maxActive</name><value>20</value></parameter>
<parameter><name>maxIdle</name><value>10</value></parameter>
<parameter><name>maxWait</name><value>-1</value></parameter>
</ResourceParams>
</Context>

代碼部分:
package biz;

import java.sql.*;
import javax.sql.*;
import java.util.Vector;
import java.util.Date;a
import javax.naming.*;

public class connpool{ // 連接池類
private int maxconn; //最大連接數
private int nActive; //已創建的連接數
private Vector freeCons=new Vector(); //存儲當前可用連接的數組

private Context initCtx = null; //Context 上下文类型
private Context ctx =null;
private DataSource ds=null;

public connpool()
{
this.maxconn=20;
this.nActive =0;
try{
initCtx = new InitialContext(); //初始化上下文,可以读配置文件如:web.xml
if(initCtx==null) throw new Exception("Initial Failed!");
ctx = (Context) initCtx.lookup("java:comp/env");
//szJdbc="jdbc/bizcity" 此值在Tomcat的SERVER.XML文件中配置
if(ctx!=null) ds= (DataSource) ctx.lookup("jdbc/bizcity");
if(ds==null) throw new Exception("Look up DataSource Failed!"); //以上这是从配置文件读取上下文的一种方法!
参数在上下文中定义
}
catch(Exception e){
System.out.println(e.getMessage());
}
}

public void SetMaxConn(int n){//默認最大連接數為 20 ~ 200;
if(n<20) n=20;
if(n>200) n=200;
this.maxconn =n;
}

public synchronized void freeConnection(Connection con) //歸還連接
{ //synchronized关键字定义可异步同时连接
try{
if(con.isClosed()){
con=null; //System.out.println("Connection is Closed\n");
}
else{
freeCons.addElement(con);
System.out.println("Add Exists Connection,Current Conut=" + freeCons.size());
}
}
catch(Exception e){System.out.println(e.getMessage());}
//notifyAll();
}

public synchronized Connection getConnection(long timeout) //取得一個連接(指定超時時間)
{
long startTime=new Date().getTime(); //記錄超時用
Connection con=null;
while(con==null){
if(freeCons.size()>0){//在有可用的連接時,選用可用連接
con=(Connection) freeCons.firstElement(); //從數組中取得第一個 把能用的连接取走了一个,当然再能用的连接中,减去一个了
freeCons.removeElementAt(0); //並從數組中刪除
try{
if(con.isClosed()){ //如果已關閉,則將已創建連接減一
nActive--;
con=null;
}
}
catch(Exception e){System.out.println(e.getMessage());}
if(con!=null) System.out.println("Get Exists Connection\n");
}else if(nActive<maxconn){ //沒有可用連接且已創建的連接數小于最大數時,創建新連接
con=newConnection();
if(con!=null) System.out.print("Create New Connection ******" + nActive );
}
if(con!=null) break; //取得可用連接後,則直接退出等待
//否則,進行等待0.5s,進行下一次獲取連接的操作
try{
wait(500); //等待0.5秒
}
catch(InterruptedException e) {}
if((new Date().getTime()-startTime)>=timeout){
System.out.println("Connection timeout\n");
break; //超時則退出
}
}
return con;
}

public void release(){ //關閉所有連接
int i=freeCons.size();
int j=0;
for (j=0;j<i;j++){
Connection con=(Connection)freeCons.elementAt(j);
try{
con.close();
}
catch(SQLException e) {
System.out.println("無法關閉連接池中的當前連接");
}
}
freeCons.removeAllElements();
nActive=0;
}

private Connection newConnection(){ //創建一個新連接
Connection con=null;
try{
con=ds.getConnection();
if(con==null) throw new Exception("Create Connection Failed!");
else nActive++;
}
catch(Exception e){
System.out.println(e.getMessage());
}
return con;
}
}


JSP調用:
<%@ page language="java" import="java.sql.*"%>
<jsp:useBean id="vCon" scope="application" class="biz.connpool"/>

<%!
Connection con=null;
java.sql.Statement stmt=null;
%>

<%
try{
if(con==null) con=vCon.getConnection(10000); //最大等待時間為10s
stmt=con.createStatement(ResultSet.TYPE_SCROLL_INSENSITIVE,ResultSet.CONCUR_READ_ONLY);
} //两个结果集 第一个参数时说结果集是不可滚动的,只能向前滚!
第二个是结果集是只读的
catch(Exception e){
System.err.println(e.getMessage());
}

ResultSet rs=stmt.executeQuery("Select *........");
........

stmt.close();
vCon.freeConnection(con);
con=null;
%>
这是前段时间在网上找的,我正在研究中,我想对楼主也会有帮助吧
瓦力1981 2003-09-13
  • 打赏
  • 举报
回复
想要给我留邮箱!
rootcn 2003-09-13
  • 打赏
  • 举报
回复
原来这个要配JNDI,一直没有看明白。

UP一下!
shaokun305 2003-09-13
  • 打赏
  • 举报
回复
tomcat 中自带有例子,一看就明白了,
http://127.0.0.1:8080/tomcat-docs/jndi-datasource-examples-howto.html
找到mysql的连接池配置

81,092

社区成员

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

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