知识大全 好的连接池,免费的

Posted 数据库

篇首语:一箫一剑平生意,负尽狂名十五年。本文由小常识网(cha138.com)小编为大家整理,主要介绍了知识大全 好的连接池,免费的相关的知识,希望对你有一定的参考价值。

  /*   * Copyright (C) Jackliu   * <a href="mailto: "></a>     * WWW CN JAVA All Rights Reserved   */   package java database;   

  import java util *;    import java sql *;

  

  /**    * <font size= ><b>数据库连接池</b></font>    * <font color=gray>这个类为系统提供一个数据库的连接池</font>    * <br><br>    * @see Sys_config    * @author <a href="mailto: ">Jackliu</a>       */    public class DBPoolsManager    static private DBPoolsManager instance; // 唯一实例    DBPools pools;    Driver theDrv;       /**    * 构造方法    * <br>创建数据库连接池(oracle OCI)    */    private DBPoolsManager()    /* use oracle OCI */    String url = "jdbc:oracle:oci :@"+Sys_config getDATABASE_INSTANCE();       String user = Sys_config getDATABASE_USERID();    String password = Sys_config getDATABASE_USERPWD();    int max = Integer parseInt(Sys_config getDATA_SESSION());    try    /*use mysql driver    theDrv = new gjt mm mysql Driver();*/

  

  /*use oracle driver*/    theDrv = new oracle jdbc driver OracleDriver();    DriverManager registerDriver(theDrv);       catch ( SQLException e )    //debug to err log    e printStackTrace(System err);       pools = new DBPools(url user password max);    pools showDetail();   

  

  /**    * 得到一个数据库的连接池管理的实例    * @return 返回一个数据库连接池管理的一个实例    */    static synchronized public DBPoolsManager getInstance()    if (instance == null)    instance = new DBPoolsManager();       return instance;   

  

  /**    * 从数据库连接池中获取一个空闲的数据库连接实例    * <br>如果超出连接池的最大连接 返回一个空的对象    * @return 返回一个数据库连接对象    */    public Connection getConnection()    return pools getConnection();   

  

  /**    * 释放一个正在工作的数据库连接到数据库连接池    * @param con Connection 一个数据库连接    */    public void freeConnection(Connection con)    pools freeConnection(con);          /**    * 关闭数据库连接池    */    public void close()    try    pools showDetail();    pools close();    DriverManager deregisterDriver(theDrv);       catch ( SQLException e )    //debug to err log    e printStackTrace(System err);      

  

  class DBPools    private Vector freeConnections = new Vector();    private Vector currentConnections = new Vector();    private int maxConn;    private String URL;    private String password;    private String user;

  

  /**    * 构造函数    * <br>创建一个数据库连接池    * @param URL String JDBC URL    * @param user String User id || null    * @param password String User pwd ||null    * @param maxConn int Max connect    */    public DBPools(String URL String user String password int maxConn)    this URL = URL;    this user = user;    this password = password;    this maxConn = maxConn;   

  

  /**    * 释放一个正在工作的数据库连接到数据库连接池    * @param con Connection 一个数据库连接    */    public synchronized void freeConnection(Connection con)    //remove from freeConnection vector    currentConnections remove(con);    freeConnections add(con);   

  

  /**    * 从连接池获得一个可用连接 如没有空闲的连接且当前连接数小于最大连接    * 数限制 则创建新连接 如原来登记为可用的连接不再有效 则从向量删除之    * 然后递归调用自己以尝试新的可用连接    * @return 返回一个数据库连接对象    */    public synchronized Connection getConnection()    Connection con = null;    if (freeConnections size() > )    // 获取向量中第一个可用连接    con = (Connection) freeConnections firstElement();    freeConnections removeElementAt( );    try    if (con isClosed())    //递归调用自己 尝试再次获取可用连接    con = getConnection();       currentConnections add(con);       catch (SQLException e)    //递归调用自己 尝试再次获取可用连接    con = getConnection();          else if (maxConn == || currentConnections size() < maxConn)    con = newConnection();       return con;       /**    * 关闭数据库连接池    */    public void close()    for ( int i= ; i<freeConnections size(); i++ )    try    ((Connection)freeConnections get(i)) close();       catch( SQLException e )    //debug to err log    e printStackTrace(System err);          for ( int i= ; i<currentConnections size(); i++ )    try    ((Connection)currentConnections get(i)) close();       catch( SQLException e )    //debug to err log    e printStackTrace(System err);         

  

  /**    * 创建新的连接    */    private synchronized Connection newConnection()    OADS_log toFileLn("DBPool create new!!!");    //showDetail();    Connection con = null;    try    if (user == null)    con = DriverManager getConnection(URL);       else    con = DriverManager getConnection(URL user password);       currentConnections add(con);    catch (SQLException e)    return null;       return con;       /**    * 显示数据库连接池的状态信息    */    public void showDetail()    OADS_log toFileLn("current DB connections : " + currentConnections size());       OADS_log toFileLn("free DB connections : " + freeConnections size());         

     cha138/Article/program/Java/JSP/201311/19689

相关参考