是不是边了没有放开啊?
去这里看看吧
http://www.chinajavaworld.net/forum/forums.cgi?forum=43
去这里看看吧
http://www.chinajavaworld.net/forum/forums.cgi?forum=43
解决方案 »
- 人力管理系统登陆问题
- org.hibernate.MappingException: Association references unmapped class
- 不显示删除回复显示所有回复显示星级回复显示得分回复 struts1中如何将textarea标签上的数据收集到ActionForm中
- 求教:我有一个方法,每隔5分钟就要和数据库进行交互,想问大家一下,我这个方法应该怎么写啊?而且这个方法不需要人为操作,是自己自动的
- 在线等答案
- mysql语句中怎么表示两天前的日期?
- 急,急 ,急!jxl 用已有的excel模板,创建新的文件
- 向Access 添加记录失败(一个很诡异又郁闷的问题!)
- 关于登陆密码问题
- MSSQLSERVER中 如何实现 有选折 查询
- 很弱智的问题
- 哪位老兄成功配置过apache+resin阿
还有,ResultSet也要及时close
package msg;
import java.sql.*;
import javax.sql.*;
import java.util.Vector;
import java.util.Date;
import javax.naming.*;
import msg.*;
public class connpool{ // 連接池類
private int maxconn; //最大連接數
private int nActive; //已創建的連接數
private Vector freeCons=new Vector(); //存儲當前可用連接的數組
private Context initCtx = null;
private Context ctx =null;
private DataSource ds=null; public connpool()
{
this.maxconn=20;
this.nActive =0;
try{
initCtx = new InitialContext();
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/wordboard");
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) //歸還連接
{
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) //取得一個連接(指定超時時間)
{ // synchronized 指明可以运行在多线程环境中
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;
}
}
SERVER。XML文件配置<Resource name="jdbc/wordboard" auth="Container" type="javax.sql.DataSource" />
<ResourceParams name="jdbc/wordboard">
<parameter><name>factory</name>
<value>org.apache.commons.dbcp.BasicDataSourceFactory</value>
</parameter>
<parameter><name>driverClassName</name> <value>sun.jdbc.odbc.JdbcOdbcDriver</value>
</parameter>
<parameter><name>url</name><value>jdbc:odbc:wordboard</value>
</parameter>
<!-- com.microsoft.jdbc.sqlserver.SQLServerDriver
jdbc:microsoft:sqlserver://127.0.0.1:1433;DataBaseName=wordboard-->
<parameter><name>username</name><value>sa</value></parameter>
<parameter><name>password</name><value>lpy</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>WEB。XML配置
<resource-ref>
<description>Connection connpool</description>
<res-ref-name>jdbc/wordboard</res-ref-name>
<res-type>javax.sql.DataSource</res-type>
<res-auth>Container</res-auth>
</resource-ref>