求解一个比较标准的连接池代码  看下面的代码有问题吗
package bookshopping.dbo;import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.SQLException;
import java.util.ArrayList;/**
 * 数据库的连接池 
 * @author aa
 *
 */
public class ConnectionPool {
private static final String DRIVER=
"com.microsoft.sqlserver.jdbc.SQLServerDriver";
private static final String URL=
"jdbc:sqlserver://localhost:1433;databaseName=BookShooping";
private static final String USER="sa";
private static final String PWD="sjfeng";

private ArrayList<Connection> pool=null;
private int poolSize=5;
private Connection conn=null;
//私有静态成员
private static ConnectionPool isPool=null;
//私有构造
private ConnectionPool(){
this.init();
}
//公共静态访问点
public static ConnectionPool getPool(){
if(isPool==null){
isPool=new ConnectionPool();
}
return isPool;
}
private void init(){
if(pool==null){
pool=new ArrayList<Connection>(this.poolSize);//初始化池子的大小
}
this.addConnection();// 把Connection加入池子的方法
}
/*
 * 得到连接就放到池子里
 */
private void addConnection(){
for (int i = 0; i < this.poolSize; i++) {
try {
Class.forName(DRIVER);
conn=DriverManager.getConnection(URL, USER, PWD);
this.pool.add(conn);//得到一个连接放到池子里
} catch (ClassNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
System.out.println("驱动加载失败");
} catch(SQLException e){
e.printStackTrace();
System.out.println("数据库连接失败");
}
}
}
//得到连接
public synchronized Connection getConnection() {
if(this.pool.size()>0){
conn=this.pool.get(0);//取走连接
this.pool.remove(conn);//取走后池子里的元素删除
return conn;
}else{
//?????
}
return null;

}
//关闭池子
public synchronized void colseAll() {
for (int i = 0; i < this.pool.size(); i++) {
try {
this.pool.get(i).close();//得到池子里的元素关闭
this.pool.remove(i);//关闭后就删除
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
//归还连接
public synchronized void relse(Connection conn) {
this.pool.add(conn);
}

}

解决方案 »

  1.   

    1 容器中的配置<Context>    <!-- Default set of monitored resources -->
        <WatchedResource>WEB-INF/web.xml</WatchedResource>
     
        <!-- Uncomment this to disable session persistence across Tomcat restarts -->
        <!--
        <Manager pathname="" />
        --> <!-- sql 2005 -->
     <Resource name="jdbc/mysqlsample" auth="Container" type="javax.sql.DataSource" username="sa" password="" maxActive="100" maxIdle="30" maxWait="10000" driverClassName="com.microsoft.sqlserver.jdbc.SQLServerDriver" url="jdbc:sqlserver://localhost:1433;DatabaseName=Sample"/><!--auth指定管理的Manager,它有两个可选值:Container和Application,Container表示有容器来创建,Application表示由Web容器创建--> <!--type指定Resouce所属java类别--><!--maxActive指定数据库连接中处于活动状态的数据连接最大数目,取值为0不受限制--><!-- mysql -->
     <Resource name="jdbc/anothersample" auth="Container" type="javax.sql.DataSource" username="root" password="mysql" maxActive="100" maxIdle="30" maxWait="10000" driverClassName="com.mysql.jdbc.Driver" url="jdbc:mysql://localhost:3306/mysql_sample"/>
    </Context>2 web.xml中的配置 <!-- 配置服务器资源参考 -->
     <!-- mssql -->
     <resource-ref>
           <res-ref-name>jdbc/mysqlsample</res-ref-name>
           <res-type>javax.sql.DataSource</res-type>
           <res-auth>Container</res-auth>
     </resource-ref>
     
     <!-- mysql -->
     <resource-ref>
              <res-ref-name>jdbc/anothersample</res-ref-name> <!--要与容器中的名字一样-->
               <res-type>javax.sql.DataSource</res-type>
               <res-auth>Container</res-auth>
     </resource-ref>3 添加数据库驱动文件将JDBC驱动复制到Tomcat的common\lib目录下!4在Connection中使用。package db;import java.sql.Connection;
    import java.sql.PreparedStatement;
    import java.sql.ResultSet;
    import java.sql.SQLException;//JNDI所导的包!import javax.naming.Context; 
    import javax.naming.InitialContext;
    import javax.sql.DataSource;public class SQLHelper {
     
     public Connection openConnection(){
      
      Connection connection = null;
      
      try {
       //Web - 通过JNDI获取服务器连接池内的连接  
       //初始化上下文
       Context context = new InitialContext();
       
       //上下文.查找数据源("java:comp/env/JNDI名字");
       DataSource ds = (DataSource)
        context.lookup("java:comp/env/jdbc/anothersample");   //从数据源中取得连接
       connection = ds.getConnection();
       
       return connection;
      } catch (Exception e) {
       // TODO Auto-generated catch block
       e.printStackTrace();
      }
      
      return null;
     }
     
     public void closeAll(ResultSet res, PreparedStatement ps, Connection conn){
      
      try {
       if(res != null){
        res.close();
       }
       if(ps != null){
        ps.close();
       }
      } catch (SQLException e) {
       // TODO Auto-generated catch block
       e.printStackTrace();
      }
      finally{
       try {
        if(conn != null){
         conn.close();
        }
       } catch (SQLException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
       }
      }
     }
    }