⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 connectionpool.java~1~

📁 cnc网通小灵通网关接口源码
💻 JAVA~1~
字号:
package cncgw.smdb;/** * <p>Title: </p> * <p>Description: </p> * <p>Copyright: Copyright (c) 2003</p> * <p>Company: </p> * @author * @version 1.0 */import java.sql.*;import java.util.*;public class ConnectionPool{  //这些参数都是创建一个连接池所必须提供的参数  private String name;  private String URL;  private String user;  private String password;  private int maxConns;  private int timeOut;//  private org.apache.log4j.Category log;  private int checkedOut;  private Vector freeConnections = new Vector();  public ConnectionPool(String name, String URL, String user,      String password, int maxConns, int initConns, int timeOut) {    this.name = name;    this.URL = URL;    this.user = user;    this.password = password;    this.maxConns = maxConns;    this.timeOut = timeOut > 0 ? timeOut : 5;    initPool(initConns);    System.out.println("New pool created");    String lf = System.getProperty("line.separator");    System.out.println(lf +    " url=" + URL + lf +                    " user=" + user + lf +                    " password=" + password + lf +                    " initconns=" + initConns + lf +                    " maxconns=" + maxConns + lf +                    " logintimeout=" + this.timeOut);    System.out.println(getStats());  }  private void initPool(int initConns) {    for (int i = 0; i < initConns; i++) {      try {        Connection pc = newConnection();        freeConnections.addElement(pc);      }      catch (SQLException e)  { }    }  }  public Connection getConnection() throws SQLException {    System.out.println("Request for connection received");    try {      Connection conn = getConnection(timeOut * 1000);      return new ConnectionWrapper(conn, this);    }    catch (SQLException e)  {      System.err.println(e + " Exception getting connection");      throw e;    }  }  synchronized void wrapperClosed(Connection conn) {    // 把连接放置回连接池Vector中    freeConnections.addElement(conn);    checkedOut--;    notifyAll();    System.out.println("Returned connection to pool");    System.out.println(getStats());  }  private synchronized Connection getConnection(long timeout)                      throws SQLException  {      // 从数据连接池中去得一个新的数据连接,如果数据连接不能获得,则      // 最多等待timeout的时间。    long startTime = System.currentTimeMillis();    long remaining = timeout;    Connection conn = null;    while ((conn = getPooledConnection()) == null) {      try {        System.out.println("Waiting for connection. Timeout=" + remaining);        wait(remaining);      }      catch (InterruptedException e) {}      remaining = timeout - (System.currentTimeMillis() - startTime);      if (remaining <= 0) {            // Timeout has expired        System.out.println("Time-out while waiting for connection");        throw new SQLException("getConnection() timed-out");      }    }    // 判断一下,所获得的连接是否可以使用,如不能使用则继续去获取连接    if (!isConnectionOK(conn)) {      System.err.println("Removed selected bad connection from pool");      return getConnection(remaining);    }    checkedOut++;    System.out.println("Delivered connection from pool");    System.out.println(getStats());    return conn;  }   private boolean isConnectionOK(Connection conn) {    Statement testStmt = null;    try {      if (!conn.isClosed()) {        // Try to createStatement to see if it's really alive        testStmt = conn.createStatement();        testStmt.close();      }      else {        return false;      }    }    catch (SQLException e) {      if (testStmt != null)  {        try {          testStmt.close();        }        catch (SQLException se) { }      }      System.err.println(e + "Pooled Connection was not okay");      return false;    }    return true;  }  private Connection getPooledConnection() throws SQLException {    Connection conn = null;    if (freeConnections.size() > 0) {      // 从数据连接池中取第一个数据连接      conn = (Connection) freeConnections.firstElement();      freeConnections.removeElementAt(0);    }    else if (maxConns == 0 || checkedOut < maxConns) {      conn = newConnection();    }    return conn;  }  private Connection newConnection() throws SQLException {    Connection conn = null;    if (user == null) {      conn = DriverManager.getConnection(URL);    }    else {      conn = DriverManager.getConnection(URL, user, password);    }    System.out.println("Opened a new connection");    return conn;  }  public synchronized void freeConnection(Connection conn)   {    // 把应用系统关闭的数据连接放回数据连接池中    freeConnections.addElement(conn);    checkedOut--;    notifyAll();    System.out.println("Returned connection to pool");    System.out.println(getStats());  }  public synchronized void release()   {    Enumeration allConnections = freeConnections.elements();    while (allConnections.hasMoreElements()) {      Connection con = (Connection) allConnections.nextElement();      try {        con.close();        System.err.println("Closed connection");      }      catch (SQLException e) {        System.err.println(e +  "Couldn't close connection");      }    }    freeConnections.removeAllElements();  }  private String getStats() {    return "Total connections: " +         (freeConnections.size() + checkedOut) +         " Available: " + freeConnections.size() +         " Checked-out: " + checkedOut;  }}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -