那位有连接池方面的类,如果有例子,给我提供一个。

解决方案 »

  1.   

    [email protected]
    发个信息过来就OK了
      

  2.   

    public class PooledConnection {
      private Connection connection=null;
      private boolean inUser;
      public PooledConnection(Connection connection) {
        this.connection=connection;
      }
      public Connection getConnection() {
        return connection;
      }
      public boolean isInUser() {
        return inUser;
      }
      public void setConnection(Connection connection) {
        this.connection = connection;
      }
      public void setInUser(boolean inUser) {
        this.inUser = inUser;
      }  public void close(){
        try{
          if (connection != null) {
            connection.close();
          }
        }
        catch(Exception e)
        {
          e.printStackTrace();
        }
      }
    }public class ConnectionPool {
      private static ConnectionPool pcon=new ConnectionPool();
      private  ArrayList pools=new ArrayList();
      private String driver;
      private String url;
      private String uesrname;
      private String password;
      private int size=5;  private ConnectionPool() {
        initialPools();
      }
     
      public synchronized static ConnectionPool getConnectionPool(){
        if(pcon==null){
          pcon=new ConnectionPool();
        }
         return pcon;
      }
      //init the pool
      private  void initialPools() {
       try{
         InputStream in = this.getClass().getResourceAsStream("/DB.properties");
         Properties props = new Properties();
         props.load(in);
         this.setDriver(props.getProperty("driver"));
         this.setUrl(props.getProperty("url"));
         this.setUesrname(props.getProperty("username"));
         this.setPassword(props.getProperty("password"));
         this.setSize(Integer.parseInt(props.getProperty("size")));     Class.forName(driver);
         for (int i = 0; i < size; i++) {
           Connection con = DriverManager.getConnection(url, uesrname, password);
           PooledConnection poolcon = new PooledConnection(con);
           poolcon.setInUser(false);
           pools.add(poolcon);
         }
    System.err.println("initialPools().pools.size()=  "+pools.size());
       }catch(Exception e){
         e.printStackTrace();
       }
      }
      //close the connection
      public synchronized void close(){
        try{
          Iterator its=pools.iterator();
          while(its.hasNext()){
            PooledConnection pool=(PooledConnection)its.next();
            pool.close();
          }
          pools=null;
        }catch(Exception e){
         e.printStackTrace();
        }
      }
      public synchronized Connection getConnection()throws Exception{
        Iterator its=pools.iterator();
         while(its.hasNext()){
           PooledConnection poolCon=(PooledConnection)its.next();
           if(!poolCon.isInUser())
             poolCon.setInUser(true);
             return poolCon.getConnection();
         }    Class.forName(driver);
        Connection con=DriverManager.getConnection(url,uesrname,password);
        PooledConnection poolcon=new PooledConnection(con);
        poolcon.setInUser(true);
        pools.add(poolcon);
        return con;
      }
      //release connection
      public synchronized void releaseConnection(Connection con){
        Iterator its=pools.iterator();
        while(its.hasNext()){
         PooledConnection poolcon=(PooledConnection)its.next();
          if(poolcon.getConnection()==con)
          {
            poolcon.setInUser(false);
            break;
          }
        }
      }
      public String getDriver() {
        return driver;
      }
      public void setDriver(String driver) {
        this.driver = driver;
      }
      public String getUrl() {
        return url;
      }
      public void setUrl(String url) {
        this.url = url;
      }
      public String getUesrname() {
        return uesrname;
      }
      public void setUesrname(String uesrname) {
        this.uesrname = uesrname;
      }
      public String getPassword() {
        return password;
      }
      public void setPassword(String password) {
        this.password = password;
      }
      public int getSize() {
        return size;
      }
      public void setSize(int size) {
        this.size = size;
      }
    }
      

  3.   

    都不写连接池了,stuts中配置一个数据源就OK了!