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

📄 dbconnectionpool.java~17~

📁 java系统通用框架 很实用的东东 一般人都看的懂,
💻 JAVA~17~
字号:
/****************************************************************************** (C) Copyright 2004* 保留对所有使用、复制、修改和发布整个软件和相关文档的权利。* 本计算机程序受著作权法和国际公约的保护,未经授权擅自复制或* 传播本程序的全部或部分,可能受到严厉的民事和刑事制裁,并* 在法律允许的范围内受到最大可能的起诉。*/ /*****************************************************************************  * @作者:Golden Peng  * @版本: 1.0  * @时间: 2002-10-08  */ /*****************************************************************************  * 修改记录清单  * 修改人  :  * 修改记录:  * 修改时间:  * 修改描述:  *  */package com.corp.bisc.ebiz.base;import java.sql.*;import java.util.*;import java.util.Date;import java.io.*;import com.corp.bisc.ebiz.exception.*;import com.corp.bisc.ebiz.base.*;/*DBConnectionPool提供两种方法来检查连接。 两种方法都返回一个可用的连接,如果没有多余的连接, 则创建一个新的连接。 如果最大连接数已经达到,第一个方法返回null, 第二个方法则等待一个连接被其他进程释放。*/public class DBConnectionPool extends ObjectBase {  private String name;  private String URL;  private String user;  private String password;  private int maxConn;  private boolean autoCommit;  private int timeout;  private Vector freeConnections =new Vector();//保存的数据库连接  private int checkedOut;         //申请的连接池  //构造函数取得上述的所有参数  //构造DBConnectionPool对象  public DBConnectionPool(DSConfig dsconfig)  {    super();    this.name = dsconfig.name;    this.URL = dsconfig.URL;    this.user = dsconfig.user;    this.password = dsconfig.password;    this.maxConn = dsconfig.maxConn;    this.autoCommit =dsconfig.autoCommit ;    this.timeout =dsconfig.timeout ;  }  public int getCheckedOut()  {    return checkedOut;  }/************************************************************* *功能描述:从DBConnectionPool取得数据库连接对象Connection * **/  public synchronized Connection getConnection() throws PortalException{    Connection con = null;    if (freeConnections.size() > 0) {          // Pick the first Connection in the Vector          // to get round-robin usage          con = (Connection) freeConnections.firstElement();          freeConnections.removeElementAt(0);          try {                if (con.isClosed()) {                    log.error("Removed bad connection from " + name);                    // Try again recursively                    con = getConnection();                }          }catch (Exception ex) {            con = getConnection();            throw new DatabaseDeniedException("取数据库连接失败:" + ex.toString() );          }      }else if (maxConn == 0 || checkedOut < maxConn) {        con = newConnection();     } else {         throw new DatabaseDeniedException("数据库连接数达到系统的限制值." );     }    if (con != null) {        checkedOut++;    }    return con;}/******************************************************************************* *功能描述: Create New Connection创建一个新的连接 *方法newConnection()用来创建一个新的连接。这是一个私有方法, *基于用户名和密码来确定是否可以创建新的连接。 * */   private Connection newConnection() throws PortalException{      Connection con = null;      try {           if (user == null) {                con = DriverManager.getConnection(URL);            }else {                con = DriverManager.getConnection(URL, user, password);            }            return con;      }catch (Exception ex) {          throw new DatabaseDeniedException("创建新的数据库连接错误:" + ex.toString());      } } /*******************************************************************************  *功能描述: 从连接池中取得一个有效的Connection连接对象  * @param timeout  时间限制,单位为秒  *  */    public synchronized Connection getConnection(long timeout) throws PortalException{      long startTime = new Date().getTime();      Connection con;      while ((con = getConnection()) == null) {          try {                  wait(timeout);              }catch (InterruptedException e) {              }              if ((new Date().getTime() - startTime) >= timeout) {                log.error("取数据库连接超时.");                return null;              }      }      return con;   }/****************************************************************************** *功能描述: 连接被加在freeConnections向量的最后,占用的连接数减1, * 调用notifyAll()函数通知其他等待的客户现在有了一个连接 *@param con 要释放的数据连接 * */ public synchronized void freeConnection(Connection con) {      // Put the connection at the end of the Vector      freeConnections.addElement(con);      checkedOut--;      notifyAll();  }/******************************************************************************* *功能描述: 数据库连接池需要得到通知以正确地关闭所有的连接。 * DBConnectionManager负责协调关闭事件, * 但连接由各个连接池自己负责关闭。 * 方法relase()由DBConnectionManager调用。 * */ public synchronized void release() throws PortalException {        Enumeration allConnections = freeConnections.elements();        while (allConnections.hasMoreElements()) {        Connection con = (Connection) allConnections.nextElement();        try {           con.close();        }catch (Exception ex) {            throw new DatabaseDeniedException("释放数据库连接错误:" + ex.toString() );        }      }      freeConnections.removeAllElements();  }}

⌨️ 快捷键说明

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