是不是边了没有放开啊?
去这里看看吧
http://www.chinajavaworld.net/forum/forums.cgi?forum=43

解决方案 »

  1.   

    也许是因为你在jsp中使用后没有将连接归还连接池?
    还有,ResultSet也要及时close
      

  2.   

    用完了要关闭,java不会自动清除数据库连接的
      

  3.   

    前段在网上找到一个朋友写得连接池
    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>