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

📄 connectionpool.java

📁 大量java源程序
💻 JAVA
📖 第 1 页 / 共 2 页
字号:
    {
      String s = null;

      if ((p != null) &&
          (key != null)) {

        // Get the value of the key
        s = p.getProperty(key);

        // If found, remove it from the properties table
        if (s != null) {
          p.remove(key);
        }
      }
      return s;
    }

  /**
    * <p>Consumes the given property and returns the integer
    * value.
    *
    * @param properties Properties table
    * @param key Key of the property to retrieve and remove from
    * the properties table
    * @return Value of the property, or -1 if not found
    */
  private int consumeInt(java.util.Properties p, String key)
    {
      int n = -1;

      // Get the String value
      String value = consume(p, key);

      // Got a value; convert to an integer
      if (value != null) {
        try {
          n = Integer.parseInt(value);
        }
        catch (Exception ex) {
        }
      }
      return n;
    }

  /**
    * <p>Creates the initial connection pool. A timer thread
    * is also created so that connection timeouts can be
    * handled.
    *
    * @return true if the pool was created
    */
  private void createPool() throws Exception
    {
      // Sanity check our properties
      if (m_JDBCDriver == null) {
        throw new Exception("JDBCDriver property not found");
      }
      if (m_JDBCConnectionURL == null) {
        throw new Exception("JDBCConnectionURL property not found");
      }
      if (m_ConnectionPoolSize < 0) {
        throw new Exception("ConnectionPoolSize property not found");
      }
      if (m_ConnectionPoolSize == 0) {
        throw new Exception("ConnectionPoolSize invalid");
      }
      if (m_ConnectionPoolMax < m_ConnectionPoolSize) {
        trace("WARNING - ConnectionPoolMax is invalid and will " +
              "be ignored");
        m_ConnectionPoolMax = -1;
      }
      if (m_ConnectionTimeout < 0) {
        // Set the default to 30 minutes
        m_ConnectionTimeout = 30;
      }

      // Dump the parameters we are going to use for the pool.
      // We don't know what type of servlet environment we will
      // be running in - this may go to the console or it
      // may be redirected to a log file
      trace("JDBCDriver = " + m_JDBCDriver);
      trace("JDBCConnectionURL = " + m_JDBCConnectionURL);
      trace("ConnectionPoolSize = " + m_ConnectionPoolSize);
      trace("ConnectionPoolMax = " + m_ConnectionPoolMax);
      trace("ConnectionUseCount = " + m_ConnectionUseCount);
      trace("ConnectionTimeout = " + m_ConnectionTimeout +
            " seconds");

      // Also dump any additional JDBC properties
      java.util.Enumeration enum = m_JDBCProperties.keys();
      while (enum.hasMoreElements()) {
        String key = (String) enum.nextElement();
        String value = m_JDBCProperties.getProperty(key);
        trace("(JDBC Property) " + key + " = " + value);
      }

      // Attempt to create a new instance of the specified
      // JDBC driver. Well behaved drivers will register
      // themselves with the JDBC DriverManager when they
      // are instantiated
      trace("Registering " + m_JDBCDriver);
      java.sql.Driver d = (java.sql.Driver)
        Class.forName(m_JDBCDriver).newInstance();

      // Create the vector for the pool
      m_pool = new java.util.Vector();

      // Bring the pool to the minimum size
      fillPool(m_ConnectionPoolSize);
    }

  /**
    * <p>Adds a new connection to the pool
    *
    * @return Index of the new pool entry, or -1 if an
    * error has occurred
    */
  private int addConnection()
    {
      int index = -1;

      try {
        // Calculate the new size of the pool
        int size = m_pool.size() + 1;

        // Create a new entry
        fillPool(size);

        // Set the index pointer to the new connection if one
        // was created
        if (size == m_pool.size()) {
          index = size - 1;
        }
      }
      catch (Exception ex) {
        ex.printStackTrace();
      }
      return index;
    }

  /**
    * <p>Brings the pool to the given size
    */
  private synchronized void fillPool(int size) throws Exception
    {
      boolean useProperties = true;
      String userID = null;
      String password = null;

      // If the only properties present are the user id and
      // password, get the connection using them instead of
      // the properties object
      if (m_JDBCProperties != null) {

        // Make sure there are only 2 properties, and they are
        // the user id and password
        if (m_JDBCProperties.size() == 2) {
          userID =
            getPropertyIgnoreCase(m_JDBCProperties, "user");
          password =
            getPropertyIgnoreCase(m_JDBCProperties, "password");

          // If all we've got is a user id and password then
          // don't use the properties
          if ((userID != null) && (password != null)) {
            useProperties = false;
          }
        }

      }

      // Loop while we need to create more connections
      while (m_pool.size() < size) {

        ConnectionObject co = new ConnectionObject();

        // Create the connection
        if (useProperties) {
          co.con = DriverManager.getConnection(m_JDBCConnectionURL,
                                              m_JDBCProperties);
        }
        else {
          co.con = DriverManager.getConnection(m_JDBCConnectionURL,
                                               userID, password);
        }

        // Do some sanity checking on the first connection in
        // the pool
        if (m_pool.size() == 0) {

          // Get the maximum number of simultaneous connections
          // as reported by the JDBC driver
          java.sql.DatabaseMetaData md = co.con.getMetaData();
          m_MaxConnections = md.getMaxConnections();
        }

        // Give a warning if the size of the pool will exceed
        // the maximum number of connections allowed by the
        // JDBC driver
        if ((m_MaxConnections > 0) &&
            (size > m_MaxConnections)) {
          trace("WARNING: Size of pool will exceed safe maximum of " +
                m_MaxConnections);
        }

        // Clear the in use flag
        co.inUse = false;

        // Set the last access time
        touch(co);

        m_pool.addElement(co);
      }
    }

  /**
   * Gets a the named propery, ignoring case. Returns null if
   * not found
   * @param p The property set
   * @param name The property name
   * @return The value of the propery, or null if not found
   */
  private String getPropertyIgnoreCase(java.util.Properties p,
                                       String name)
    {
      if ((p == null) || (name == null)) return null;

      String value = null;

      // Get an enumeration of the property names
      java.util.Enumeration enum = p.propertyNames();

      // Loop through the enum, looking for the given property name
      while (enum.hasMoreElements()) {
        String pName = (String) enum.nextElement();
        if (pName.equalsIgnoreCase(name)) {
          value = p.getProperty(pName);
          break;
        }
      }

      return value;
    }

  /**
    * <p>Find the given connection in the pool
    *
    * @return Index into the pool, or -1 if not found
    */
  private int find(java.sql.Connection con)
    {
      int index = -1;

      // Find the matching Connection in the pool
      if ((con != null) &&
          (m_pool != null)) {
        for (int i = 0; i < m_pool.size(); i++) {
          ConnectionObject co = (ConnectionObject)
            m_pool.elementAt(i);
          if (co.con == con) {
            index = i;
            break;
          }
        }
      }
      return index;
    }

  /**
    * <p>Called by the timer each time a clock cycle expires.
    * This gives us the opportunity to timeout connections
    */
  public synchronized void TimerEvent(Object object)
    {
      // No pool means no work
      if (m_pool == null) {
        return;
      }

      // Get the current time in milliseconds
      long now = System.currentTimeMillis();

      // Check for any expired connections and remove them
      for (int i = m_pool.size() - 1; i >= 0; i--) {
        ConnectionObject co = (ConnectionObject)
          m_pool.elementAt(i);

        // If the connection is not in use and it has not been
        // used recently, remove it
        if (!co.inUse) {
          if ((m_ConnectionTimeout > 0) &&
              (co.lastAccess +
               (m_ConnectionTimeout * 1000) < now)) {
            removeFromPool(i);
          }
        }
      }

      // Remove any connections that are no longer open
      for (int i = m_pool.size() - 1; i >= 0; i--) {
        ConnectionObject co = (ConnectionObject)
          m_pool.elementAt(i);
        try {
          // If the connection is closed, remove it from the pool
          if (co.con.isClosed()) {
            trace("Connection closed unexpectedly");
            removeFromPool(i);
          }
        }
        catch (Exception ex) {
        }
      }

      // Now ensure that the pool is still at it's minimum size
      try {
        if (m_pool != null) {
          if (m_pool.size() < m_ConnectionPoolSize) {
            fillPool(m_ConnectionPoolSize);
          }
        }
      }
      catch (Exception ex) {
        ex.printStackTrace();
      }
    }

  /**
    * <p>Sets the last access time for the given ConnectionObject
    */
  private void touch(ConnectionObject co)
    {
      if (co != null) {
        co.lastAccess = System.currentTimeMillis();
      }
    }

  /**
    * <p>Trace the given string
    */
  private void trace(String s)
    {
      System.out.println("ConnectionPool: " + s);
    }
}

// This package-private class is used to represent a single
// connection object
class ConnectionObject
{
  // The JDBC Connection
  public java.sql.Connection con;

  // true if this connection is currently in use
  public boolean inUse;

  // The last time (in milliseconds) that this connection was used
  public long lastAccess;

  // The number of times this connection has been used
  public int useCount;

  /**
    * <p>Determine if the connection is available
    *
    * @return true if the connection can be used
    */
  public boolean isAvailable()
    {
      boolean available = false;

      try {

        // To be available, the connection cannot be in use
        // and must be open
        if (con != null) {
          if (!inUse &&
              !con.isClosed()) {
            available = true;
          }
        }
      }
      catch (Exception ex) {
      }

      return available;
    }

  /**
    * <p>Convert the object contents to a String
    */
  public String toString()
    {
      return "Connection=" + con + ",inUse=" + inUse +
        ",lastAccess=" + lastAccess + ",useCount=" + useCount;
    }
}

⌨️ 快捷键说明

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