?? dbconnectionpool.java
字號(hào):
/****************************************************************************** (C) Copyright 2004* 保留對(duì)所有使用、復(fù)制、修改和發(fā)布整個(gè)軟件和相關(guān)文檔的權(quán)利。* 本計(jì)算機(jī)程序受著作權(quán)法和國(guó)際公約的保護(hù),未經(jīng)授權(quán)擅自復(fù)制或* 傳播本程序的全部或部分,可能受到嚴(yán)厲的民事和刑事制裁,并* 在法律允許的范圍內(nèi)受到最大可能的起訴。*/ /***************************************************************************** * @作者:Golden Peng * @版本: 1.0 * @時(shí)間: 2002-10-08 */ /***************************************************************************** * 修改記錄清單 * 修改人 : * 修改記錄: * 修改時(shí)間: * 修改描述: * */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提供兩種方法來(lái)檢查連接。 兩種方法都返回一個(gè)可用的連接,如果沒(méi)有多余的連接, 則創(chuàng)建一個(gè)新的連接。 如果最大連接數(shù)已經(jīng)達(dá)到,第一個(gè)方法返回null, 第二個(gè)方法則等待一個(gè)連接被其他進(jìn)程釋放。*/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();//保存的數(shù)據(jù)庫(kù)連接 private int checkedOut; //申請(qǐng)的連接池 //構(gòu)造函數(shù)取得上述的所有參數(shù) //構(gòu)造DBConnectionPool對(duì)象 public DBConnectionPool(DSConfig dsconfig) { 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取得數(shù)據(jù)庫(kù)連接對(duì)象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("取數(shù)據(jù)庫(kù)連接失敗:" + ex.toString() ); } }else if (maxConn == 0 || checkedOut < maxConn) { con = newConnection(); } else { throw new DatabaseDeniedException("數(shù)據(jù)庫(kù)連接數(shù)達(dá)到系統(tǒng)的限制值." ); } if (con != null) { checkedOut++; } return con;}/******************************************************************************* *功能描述: Create New Connection創(chuàng)建一個(gè)新的連接 *方法newConnection()用來(lái)創(chuàng)建一個(gè)新的連接。這是一個(gè)私有方法, *基于用戶名和密碼來(lái)確定是否可以創(chuàng)建新的連接。 * */ private Connection newConnection() throws PortalException{ Connection con = null; try { if (user == null) { con = DriverManager.getConnection(URL); }else { con = DriverManager.getConnection(URL, user, password); } con.setAutoCommit(autoCommit); return con; }catch (Exception ex) { throw new DatabaseDeniedException("創(chuàng)建新的數(shù)據(jù)庫(kù)連接錯(cuò)誤:" + ex.toString()); } } /******************************************************************************* *功能描述: 從連接池中取得一個(gè)有效的Connection連接對(duì)象 * @param timeout 時(shí)間限制,單位為秒 * */ 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("取數(shù)據(jù)庫(kù)連接超時(shí)."); return null; } } return con; }/****************************************************************************** *功能描述: 連接被加在freeConnections向量的最后,占用的連接數(shù)減1, * 調(diào)用notifyAll()函數(shù)通知其他等待的客戶現(xiàn)在有了一個(gè)連接 *@param con 要釋放的數(shù)據(jù)連接 * */ public synchronized void freeConnection(Connection con) { // Put the connection at the end of the Vector freeConnections.addElement(con); checkedOut--; notifyAll(); }/******************************************************************************* *功能描述: 數(shù)據(jù)庫(kù)連接池需要得到通知以正確地關(guān)閉所有的連接。 * DBConnectionManager負(fù)責(zé)協(xié)調(diào)關(guān)閉事件, * 但連接由各個(gè)連接池自己負(fù)責(zé)關(guān)閉。 * 方法relase()由DBConnectionManager調(diào)用。 * */ 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("釋放數(shù)據(jù)庫(kù)連接錯(cuò)誤:" + ex.toString() ); } } freeConnections.removeAllElements(); }}
?? 快捷鍵說(shuō)明
復(fù)制代碼
Ctrl + C
搜索代碼
Ctrl + F
全屏模式
F11
切換主題
Ctrl + Shift + D
顯示快捷鍵
?
增大字號(hào)
Ctrl + =
減小字號(hào)
Ctrl + -