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

📄 poolmanager.java

📁 java版源代码,里面包含很多源代码,大家可以看看.
💻 JAVA
字号:
package com.trulytech.mantis.system;

import java.sql.*;
import java.util.*;

/**
 *
 * <p>Title: Mantis</p>
 *
 * <p>Description: 数据库连接池</p>
 *
 * <p>Copyright: Copyright (c) 2002</p>
 *
 * <p>Company: </p>
 *
 * @author Wang Xian
 * @version 1.0
 */
public class PoolManager {

  static private PoolManager instance = null;

  private DBConnectionPool pool = null;

  /**
   * 获得PoolManager实例
   * @return PoolManager
   */
  static synchronized public PoolManager getInstance() {

    if (instance == null) {

      instance = new PoolManager();

    }

    return instance;
  }

  /**
   * 初始化数据库连接
   */
  private PoolManager() {

    pool = new DBConnectionPool();
  }

  /**
   * 释放数据库连接
   * @param con Connection
   */
  public void freeConnection(Connection con) {

    if (pool != null) {
      pool.freeConnection(con);
    }
  }

  /**
   * 获得数据库连接
   * @return Connection
   */
  public Connection getConnection() {

    if (pool != null) {
      return pool.getConnection();
    }
    else return null;
  }

  /**
   * 释放所有数据库连接
   */
  public synchronized void release() {

    if (pool != null) {
      pool.release();
      pool = null;
      instance = null;

    }

  }

  /**
   *
   * <p>Title: Mantis</p>
   *
   * <p>Description:数据库池 </p>
   *
   * <p>Copyright: Copyright (c) 2002</p>
   *
   * <p>Company: </p>
   *
   * @author Wang Xian
   * @version 1.0
   */
  class DBConnectionPool {

    //记录使用连接数
    private int checkedOut;

    //连接池
    private Vector freeConnections = new Vector();

    /**
     * 释放连接到池中
     * @param con Connection
     */
    public synchronized void freeConnection(Connection con) {

      freeConnections.addElement(con);
      checkedOut--;
      notifyAll();
    }

    /**
     * 获得数据库连接,当使用连接超过最大连接,则返回null
     * @return Connection
     */
    public synchronized Connection getConnection() {
      Connection con = null;
      if (freeConnections.size() > 0) {

        con = (Connection) freeConnections.firstElement();
        freeConnections.removeElementAt(0);
        try {
          if (con.isClosed()) {

            con = getConnection();
          }
        }
        catch (SQLException e) {

          con = null;
        }
      }

      else if (Properties.MaxConnection == 0 ||
               checkedOut < Properties.MaxConnection) {

        con = newConnection();
      }
      if (con != null) {
        checkedOut++;

      }
      return con;
    }

    /**
     * 释放所有连接
     */
    public synchronized void release() {
      logWriter.Info("释放所有连接");
      Enumeration allConnections = freeConnections.elements();
      while (allConnections.hasMoreElements()) {
        Connection con = (Connection) allConnections.nextElement();
        try {
          con.close();
          con = null;

        }

        catch (SQLException e) {
          logWriter.Error(e.toString() + " (" + this.getClass().getName() +
                          ")");
        }
      }

      freeConnections.removeAllElements();
      freeConnections = null;
    }

    /**
     * 新建连接
     * @return Connection
     */
    private Connection newConnection() {
      Connection con = null;
      java.util.Properties props = new java.util.Properties();
      props.put("characterEncoding", Properties.Charset);
      props.put("useUnicode", Properties.isUnicode);
      props.put("user", Properties.UserName);
      props.put("password", Properties.Password);

      logWriter.Debug("创建新的连接 ( 当前使用连接数 " + checkedOut + " ) ");

      try {
        if (props != null) {
          con = DriverManager.getConnection(Properties.JDBCURL, props);
        }
      }
      catch (Exception e) {
        logWriter.Error(e.toString() + " (" + this.getClass().getName() +
                        ")");
      }

      return con;
    }
  }
}

⌨️ 快捷键说明

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