Servlet涓璲dbc搴旂敤楂樼骇绡囷紙涓夛級
鍙戝竷: 2008-12-03 23:06 | 浣滆: 缃戠粶杞浇 | 鏉ユ簮: 缃戠粶杞浇 | 鏌ョ湅: 9娆
銆銆涓銆丏BConnectionPool鐨勬瀯閫
銆銆鏋勯犲嚱鏁板彇寰椾笂杩扮殑鎵鏈夊弬鏁帮細
public DBConnectionPool(String name, String URL, String user,
String password, int maxConn) {
this.name = name;
this.URL = URL;
this.user = user;
this.password = password;
this.maxConn = maxConn;
}
銆銆灏嗘墍鏈夌殑鍙傛暟淇濆瓨鍦ㄥ疄渚嬪彉閲忎腑銆
銆銆浜屻佷粠姹犱腑鎵撳紑涓涓繛鎺
銆銆DBConnectionPool鎻愪緵涓ょ鏂规硶鏉ユ鏌ヨ繛鎺ャ備袱绉嶆柟娉曢兘杩斿洖涓涓彲鐢ㄧ殑杩炴帴锛屽鏋滄病鏈夊浣欑殑杩炴帴锛屽垯鍒涘缓涓涓柊鐨勮繛鎺ャ傚鏋滄渶澶ц繛鎺ユ暟宸茬粡杈惧埌锛岀涓涓柟娉曡繑鍥瀗ull锛岀浜屼釜鏂规硶鍒欑瓑寰呬竴涓繛鎺ヨ鍏朵粬杩涚▼閲婃斁銆
public synchronized Connection getConnection() {
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("Removed bad connection from " + name);
// Try again recursively
con = getConnection();
}
}
catch (SQLException e) {
log("Removed bad connection from " + name);
// Try again recursively
con = getConnection();
}
}
else if (maxConn == 0 || checkedOut < maxConn) {
con = newConnection();
}
if (con != null) {
checkedOut++;
}
return con;
}
銆銆鎵鏈夌┖闂茬殑杩炴帴瀵硅薄淇濆瓨鍦ㄤ竴涓彨freeConnections 鐨刅ector涓傚鏋滃瓨鍦ㄨ嚦灏戜竴涓┖闂茬殑杩炴帴锛実etConnection()杩斿洖鍏朵腑绗竴涓繛鎺ャ備笅闈紝灏嗕細鐪嬪埌锛岃繘绋嬮噴鏀剧殑杩炴帴杩斿洖鍒 freeConnections鐨勬湯灏俱傝繖鏍凤紝鏈澶ч檺搴﹀湴閬垮厤浜嗘暟鎹簱鍥犱竴涓繛鎺ヤ笉娲诲姩鑰屾剰澶栧皢鍏跺叧闂殑椋庨櫓銆
銆銆鍐嶈繑鍥炲鎴蜂箣鍓嶏紝isClosed()妫鏌ヨ繛鎺ユ槸鍚︽湁鏁堛傚鏋滆繛鎺ヨ鍏抽棴浜嗭紝鎴栬呬竴涓敊璇彂鐢燂紝璇ユ柟娉曢掑綊璋冪敤鍙栧緱鍙︿竴涓繛鎺ャ
銆銆濡傛灉娌℃湁鍙敤鐨勮繛鎺ワ紝璇ユ柟娉曟鏌ユ槸鍚︽渶澶ц繛鎺ユ暟琚缃负0琛ㄧず鏃犻檺杩炴帴鏁帮紝鎴栬呰揪鍒颁簡鏈澶ц繛鎺ユ暟銆傚鏋滃彲浠ュ垱寤烘柊鐨勮繛鎺ワ紝鍒欏垱寤轰竴涓柊鐨勮繛鎺ャ傚惁鍒欙紝杩斿洖null銆
銆銆鏂规硶newConnection()鐢ㄦ潵鍒涘缓涓涓柊鐨勮繛鎺ャ傝繖鏄竴涓鏈夋柟娉曪紝鍩轰簬鐢ㄦ埛鍚嶅拰瀵嗙爜鏉ョ‘瀹氭槸鍚﹀彲浠ュ垱寤烘柊鐨勮繛鎺ャ
private Connection newConnection() {
Connection con = null;
try {
if (user == null) {
con = DriverManager.getConnection(URL);
}
else {
con = DriverManager.getConnection(URL, user, password);
}
log("Created a new connection in pool " + name);
}
catch (SQLException e) {
log(e, "Can not create a new connection for " + URL);
return null;
}
return con;
}
銆銆jdbc鐨凞riverManager鎻愪緵涓绯诲垪鐨刧etConnection锛堬級鏂规硶锛屽彲浠ヤ娇鐢╱rl鍜岀敤鎴峰悕锛屽瘑鐮佺瓑鍙傛暟鍒涘缓涓涓繛鎺ャ
銆銆绗簩涓猤etConnection()鏂规硶甯︽湁涓涓秴鏃跺弬鏁 timeout,褰撹鍙傛暟鎸囧畾鐨勬绉掓暟琛ㄧず瀹㈡埛鎰挎剰涓轰竴涓繛鎺ョ瓑寰呯殑鏃堕棿銆傝繖涓柟娉曡皟鐢ㄥ墠涓涓柟娉曘
public synchronized Connection getConnection(long timeout) {
long startTime = new Date().getTime();
Connection con;
while ((con = getConnection()) == null) {
try {
wait(timeout);
}
catch (InterruptedException e) {}
if ((new Date().getTime() - startTime) >= timeout) {
// Timeout has expired
return null;
}
}
return con;
}
銆銆灞閮ㄥ彉閲弒tartTime鍒濆鍖栧綋鍓嶇殑鏃堕棿銆備竴涓獁hile寰幆棣栧厛灏濊瘯鑾峰緱涓涓繛鎺ワ紝濡傛灉澶辫触锛寃ait()鍑芥暟琚皟鐢ㄦ潵绛夊緟闇瑕佺殑鏃堕棿銆傚悗闈細鐪嬪埌锛學ait()鍑芥暟浼氬湪鍙︿竴涓繘绋嬭皟鐢╪otify()鎴栬卬otifyAll()鏃惰繑鍥烇紝鎴栬呯瓑鍒版椂闂存祦閫濆畬姣曘備负浜嗙‘瀹歸ait()鏄洜涓轰綍绉嶅師鍥犺繑鍥烇紝鎴戜滑鐢ㄥ紑濮嬫椂闂村噺鍘诲綋鍓嶆椂闂达紝妫鏌ユ槸鍚﹀ぇ浜巘imeout銆傚鏋滅粨鏋滃ぇ浜巘imeout,杩斿洖null,鍚﹀垯锛屽湪姝よ皟鐢 getConnection()鍑芥暟銆
銆銆鍥涖佸皢涓涓繛鎺ヨ繑鍥炴睜涓
銆銆DBConnectionPool绫讳腑鏈変竴涓猣reeConnection鏂规硶浠ヨ繑鍥炵殑杩炴帴浣滀负鍙傛暟锛屽皢杩炴帴杩斿洖杩炴帴姹犮
public synchronized void freeConnection(Connection con) {
// Put the connection at the end of the Vector
freeConnections.addElement(con);
checkedOut--;
notifyAll();
}
銆銆杩炴帴琚姞鍦╢reeConnections鍚戦噺鐨勬渶鍚庯紝鍗犵敤鐨勮繛鎺ユ暟鍑1锛岃皟鐢╪otifyAll()鍑芥暟閫氱煡鍏朵粬绛夊緟鐨勫鎴风幇鍦ㄦ湁浜嗕竴涓繛鎺ャ
浜斻佸叧闂
銆銆澶у鏁皊ervlet寮曟搸鎻愪緵瀹屾暣鐨勫叧闂柟娉曘傛暟鎹簱杩炴帴姹犻渶瑕佸緱鍒伴氱煡浠ユ纭湴鍏抽棴鎵鏈夌殑杩炴帴銆侱BConnectionManager璐熻矗鍗忚皟鍏抽棴浜嬩欢锛屼絾杩炴帴鐢卞悇涓繛鎺ユ睜鑷繁璐熻矗鍏抽棴銆傛柟娉時elase()鐢盌BConnectionManager璋冪敤銆
public synchronized void release() {
Enumeration allConnections = freeConnections.elements();
while (allConnections.hasMoreElements()) {
Connection con = (Connection) allConnections.nextElement();
try {
con.close();
log("Closed connection for pool " + name);
}
catch (SQLException e) {
log(e, "Can not close connection for pool " + name);
}
}
freeConnections.removeAllElements();
}
鏈柟娉曢亶鍘唂reeConnections鍚戦噺浠ュ叧闂墍鏈夌殑杩炴帴銆




发表于 2009-1-5 19:48
| 











