正文

还是自己写的一个连接池,代码少,实用。。2006-05-24 10:01:00

【评论】 【打印】 【字体: 】 本文链接:http://blog.pfan.cn/ggbm/14768.html

分享到:

package e_office.systemframework.pool; /***编者:李国庆*类的描述信息:此类为一个数据库连接池类,是一个单态的类,其中对数据库连接的信息*都放在一个属性文件中,可以进行方便的设置****最后修改时间:2006-1-12 10:30**********/import java.sql.*;import java.io.*;import java.util.*;import javax.servlet.ServletException;import javax.servlet.http.HttpServlet;import javax.servlet.http.HttpServletRequest;import javax.servlet.http.HttpServletResponse; import javax.servlet.*;import javax.servlet.http.*;import java.util.ResourceBundle; /** * 这是一个数据库连接池 *访问属性文件:mypool.properties *返回一个Connection * */ public class ConnectionPool{ private int minConn;        //最少连接数 private int maxConn;        //最大连接数 private String user;        //用户名 private String password;    //数据库密码 private int connAmount;       //现有连接个数 private Stack connStack;     //使用Stack保存数据库连接 private String dry;            //驱动 private String connString;    //连接字符串    private static ConnectionPool  connectionPool; private int waitTime;               //忙时的等告待时间 static ResourceBundle  bundle=null;    //属性文件读取    /**  *返回一个实例。如果是第一次调用此方法则个建一个实例  */  public static  synchronized ConnectionPool getInstance(){   if(connectionPool==null){    connectionPool = new ConnectionPool();   }   return connectionPool;  }   /**  *读属性文件得到数据库连接信息及连接池信息  */  public void readProperties(){       try   {    bundle=ResourceBundle.getBundle("properties.mypool");    this.user=bundle.getString("user");    this.password = bundle.getString("password");    this.connString=bundle.getString("connString");    this.dry = bundle.getString("dry");       this.minConn=Integer.parseInt(bundle.getString("minConn"));    System.out.println("this  mincon:"+this.minConn);    this.maxConn=Integer.parseInt(bundle.getString("maxConn"));    this.waitTime=Integer.parseInt(bundle.getString("waitTime"));           }   catch(Exception w) { w.toString();  }   System.out.println("初始化成功..........");     }    private ConnectionPool(){   readProperties();   this.connStack = new Stack();   System.out.println(this.dry);   try{    Class.forName(this.dry);   }catch(Exception e){    e.printStackTrace();   }   for(int i = 0;i<this.minConn;i++){    System.out.println("myPool:"+i);    connStack.push(newConnection());   }  }    /**   *从连接池得到连接   */   public synchronized Connection getConnection(){    Connection conn = null;    System.out.println("user connection:"+this.connStack.size());    if(!this.connStack.empty()){     conn = (Connection)connStack.pop();     System.out.println("得到一个连接");    }else if(this.connAmount<this.maxConn){     conn = newConnection();    }else{     try{      wait(this.waitTime);      System.out.println("等待");      return getConnection();     }catch(Exception e){      e.printStackTrace();     }    }    return conn;   }      /**    /*释放连接    */    public synchronized void freeConnection(Connection con){     this.connStack.push(con);     System.out.println("归还连接");     notifyAll();    }     /**   *创建新连接   */   public Connection newConnection(){    Connection conn=null;    try{         conn = DriverManager.getConnection(this.connString,this.user,this.password);     this.connAmount++;     System.out.println("连接池创建一个连接"+conn.toString());     return conn;    }catch(Exception e){     e.printStackTrace();     return null;    }   } }  

阅读(5320) | 评论(1)


版权声明:编程爱好者网站为此博客服务提供商,如本文牵涉到版权问题,编程爱好者网站不承担相关责任,如有版权问题请直接与本文作者联系解决。谢谢!

评论

loading...
您需要登录后才能评论,请 登录 或者 注册