精华区 [关闭][返回]

当前位置:网易精华区>>讨论区精华>>编程开发>>● Java>>JAVA编程>>数据库接口>>Re:关于连接池!请哪位老兄给我上一课!!

主题:Re:关于连接池!请哪位老兄给我上一课!!
发信人: huanghwh(五云人)
整理人: zjxyz(2002-12-19 11:15:50), 站内信件
【 在 liuzhenai 的大作中提到:】
:
:连接池,可以提高JSP程序的效率,jsp使用连接池是很有必要的,但是兄弟我对连接池还知之甚少,请哪位老兄用精简、通俗的语言给我补一课,谢谢!!
:
:1、从本质上讲,连接池到底有什么作用(主要结合jsp,mssql/oracle)?
:2、从形式上讲,连接池的使用,需要掌哪几个类?(最好举个例,这样会清楚很多)
:
:......
 /*
 * @(#)ConnectionPool
 *
 * Copyright (c) 1998 Karl Moss. All Rights Reserved.
 *
 * You may study, use, modify, and distribute this software for any
 * purpose provided that this copyright notice appears in all copies.
 *
 * This software is provided WITHOUT WARRANTY either expressed or
 * implied.
 *
 * @author  Karl Moss
 * @version 1.0
 * @date    11Mar98
 *
 */

package cn.gd.db;

import cn.gd.util.Timer;
import java.io.FileInputStream;
import java.sql.*;
import java.lang.Object;

/**
 * <p>This class serves as a JDBC connection repository. Since
 * creating database connections is one of the most time
 * intensive aspects of JDBC, we'll create a pool of connections.
 * Each connection can be used and then replaced back into the
 * pool.
 *
 * <p>A properties file 'ConnectionPool.cfg' will be used to
 * specify the types of JDBC connections to create, as well as
 * the minimum and maximum size of the pool. The format of
 * the configuration file is:
 *
 *   #(comment)
 *   JDBCDriver=<JDBC driver name>
 *   JDBCConnectionURL=<JDBC Connection URL>
 *   ConnectionPoolSize=<minimum size of the pool>
 *   ConnectionPoolMax=<maximum size of the pool, or -1 for none>
 *   ConnectionUseCount=<maximum usage count for one connection>
 *   ConnectionTimeout=<maximum idle lifetime (in minutes) of
* a connection>
 *   <other property for JDBC connection>=<value>
 *
 * <p>Any number of additional properties may be given (such
 * as username and password) as required by the JDBC driver.
 *
 */

public class ConnectionPool
  implements cn.gd.util.TimerListener
{
  public static final String cvsid = "$Id: ConnectionPool.java,v 1.2 2001/12/19 06:45:10 hwh Exp $";
  // JDBC Driver name
  String m_JDBCDriver;

  // JDBC Connection URL
  String m_JDBCConnectionURL;

  // Minimum size of the pool
  int m_ConnectionPoolSize;

  // Maximum size of the pool
  int m_ConnectionPoolMax;

  // Maximum number of uses for a single connection, or -1 for
  // none
  int m_ConnectionUseCount;

  // Maximum connection idle time (in minutes)
  int m_ConnectionTimeout;

  // Additional JDBC properties
  java.util.Properties m_JDBCProperties;

  // The Connection pool. This is a vector of ConnectionObject
  // objects
  java.util.Vector m_pool;

  // The maximum number of simultaneous connections as reported
  // by the JDBC driver
  int m_MaxConnections = -1;

  // Our Timer object
  Timer m_timer;

  /**
    * <p>Initializes the ConnectionPool object using
    * 'ConnectionPool.cfg' as the configuration file
    *
    * @return true if the ConnectionPool was initialized
    * properly
    */
  public boolean initialize() throws Exception
    {
      return initialize("Server.properties");
    }

  /**
    * <p>Initializes the ConnectionPool object with the specified
    * configuration file
    *
    * @param config Configuration file name
    * @return true if the ConnectionPool was initialized
    * properly
    */
  public boolean initialize(String config) throws Exception
    {
      // Load the configuration parameters. Any leftover parameters
      // from the configuration file will be considered JDBC
      // connection attributes to be used to establish the
      // JDBC connections
      boolean rc = loadConfig(config);

      if (rc) {
        // Properties were loaded; attempt to create our pool
        // of connections
        createPool();

        // Start our timer so we can timeout connections. The
        // clock cycle will be 20 seconds
        m_timer = new Timer(this, 20);

        /** @todo fix freebsd sleep problem, need to change back later */
        //m_timer.start();
      }
      return rc;
    }

  /**
    * <p>Destroys the pool and it's contents. Closes any open
    * JDBC connections and frees all resources
    */
  public void destroy()
    {
      try {

        // Stop our timer thread
        if (m_timer != null) {
          m_timer.quit();
          m_timer = null;
        }

        // Clear our pool
        if (m_pool != null) {

          // Loop throught the pool and close each connection
          for (int i = 0; i < m_pool.size(); i++) {
close((ConnectionObject) m_pool.elementAt(i));
}
}
m_pool = null;
}
catch (Exception ex) {
ex.printStackTrace();
}
}

/**
* <p>Gets an available JDBC Connection. Connections will be
    * created if necessary, up to the maximum number of connections
    * as specified in the configuration file.
    *
    * @return JDBC Connection, or null if the maximum
    * number of connections has been exceeded
    */
  public synchronized java.sql.Connection getConnection()
    {
      // If there is no pool it must have been destroyed
      if (m_pool == null) {
        return null;
      }

      java.sql.Connection con = null;
      ConnectionObject connectionObject = null;
      int poolSize = m_pool.size();

      // Get the next available connection
      for (int i = 0; i < poolSize; i++) {

// Get the ConnectionObject from the pool
ConnectionObject co = (ConnectionObject)
m_pool.elementAt(i);

// If this is a valid connection and it is not in use,
// grab it
if (co.isAvailable()) {
connectionObject = co;
break;
}
}

// No more available connections. If we aren't at the
// maximum number of connections, create a new entry
// in the pool
if (connectionObject == null) {
if ((m_ConnectionPoolMax < 0) ||
((m_ConnectionPoolMax > 0) &&
             (poolSize < m_ConnectionPoolMax))) {

// Add a new connection.
int i = addConnection();

// If a new connection was created, use it
if (i >= 0) {
            connectionObject = (ConnectionObject)
              m_pool.elementAt(i);
          }
        }
        else {
          trace("Maximum number of connections exceeded");
        }
      }

      // If we have a connection, set the last time accessed,
      // the use count, and the in use flag
      if (connectionObject != null) {
        connectionObject.inUse = true;
        connectionObject.useCount++;
        touch(connectionObject);
        con = connectionObject.con;
      }

      return con;
    }

  /**
    * <p>Places the connection back into the connection pool,
    * or closes the connection if the maximum use count has
    * been reached
    *
    * @param Connection object to close
    */
  public synchronized void close(java.sql.Connection con)
    {
      // Find the connection in the pool
      int index = find(con);

      if (index != -1) {
        ConnectionObject co = (ConnectionObject)
          m_pool.elementAt(index);

        // If the use count exceeds the max, remove it from
        // the pool.
        if ((m_ConnectionUseCount > 0) &&
            (co.useCount >= m_ConnectionUseCount)) {
          trace("Connection use count exceeded");
          removeFromPool(index);
        }
        else {
          // Clear the use count and reset the time last used
          touch(co);
          co.useCount--;            //hwh add
          if(co.useCount <= 0) { //hwh add
co.useCount =0; //hwh add
co.inUse = false; //hwh add
}
// co.inUse = false; //orginal
}
}
}

public String getJdbcConnectionURL() {
return m_JDBCConnectionURL;
}

/**
* <p>Prints the contents of the connection pool to the
     * standard output device
     */
  public void printPool()
    {
      System.out.println("--ConnectionPool--");
      if (m_pool != null) {
        for (int i = 0; i < m_pool.size(); i++) {
ConnectionObject co = (ConnectionObject)
m_pool.elementAt(i);
System.out.println("" + i + "=" + co);
}
}
}

/**
* <p>Prints the contents of the connection pool to the
    * standard output device
    */
  public String printPoolStatus(){
    StringBuffer sbuf = new StringBuffer();
    sbuf.append("--ConnectionPool--\r\n");
    if (m_pool != null) {
      for (int i = 0; i < m_pool.size(); i++) {
ConnectionObject co = (ConnectionObject)
m_pool.elementAt(i);
sbuf.append("" + i + "=" + co + "\r\n");
}
}
return sbuf.toString();
}

/**
* <p>Removes the ConnectionObject from the pool at the
    * given index
    *
    * @param index Index into the pool vector
    */
  private synchronized void removeFromPool(int index)
    {
      // Make sure the pool and index are valid
      if (m_pool != null) {

        if (index < m_pool.size()) {

// Get the ConnectionObject and close the connection
ConnectionObject co = (ConnectionObject)
m_pool.elementAt(index);
close(co);

// Remove the element from the pool
m_pool.removeElementAt(index);
}
}
}

/**
* <p>Closes the connection in the given ConnectionObject
    *
    * @param connectObject ConnectionObject
    */
  private void close(ConnectionObject connectionObject)
    {
      if (connectionObject != null) {
        if (connectionObject.con != null) {
          try {

            // Close the connection
            connectionObject.con.close();
          }
          catch (Exception ex) {
            // Ignore any exceptions during close
          }

          // Clear the connection object reference
          connectionObject.con = null;
        }
      }
    }




----
Power OS is FreeBSD.
Network is computing.
Java is network language.
Web is Application.

[关闭][返回]