Servlet涓璲dbc搴旂敤楂樼骇绡囷紙鍥涳級
鍙戝竷: 2008-12-03 23:04 | 浣滆: 缃戠粶杞浇 | 鏉ユ簮: 缃戠粶杞浇 | 鏌ョ湅: 12娆
private DBConnectionManager() {
init();
}
銆銆DBConnetionManager鐨勫鎴疯皟鐢╣etInstance()鏂规硶鏉ュ緱鍒拌绫荤殑鍗曚竴瀹炰緥鐨勫紩鐢ㄣ
static synchronized public DBConnectionManager getInstance() {
if (instance == null) {
instance = new DBConnectionManager();
}
clients++;
return instance;
}
銆銆鍗曚竴鐨勫疄渚嬪湪绗竴娆¤皟鐢ㄦ椂鍒涘缓锛屼互鍚庣殑璋冪敤杩斿洖璇ュ疄渚嬬殑闈欐佸簲鐢ㄣ備竴涓鏁板櫒绾綍鎵鏈夌殑瀹㈡埛鏁帮紝鐩村埌瀹㈡埛閲婃斁寮曠敤銆傝繖涓鏁板櫒鍦ㄤ互鍚庣敤鏉ュ崗璋冨叧闂繛鎺ユ睜銆
銆銆涓銆佸垵濮嬪寲
銆銆鏋勯犲嚱鏁拌皟鐢ㄤ竴涓鏈夌殑init()鍑芥暟鍒濆鍖栧璞°
private void init() {
InputStream is = getClass().getResourceAsStream("/db.properties");
Properties dbProps = new Properties();
try {
dbProps.load(is);
}
catch (Exception e) {
System.err.println("Can not read the properties file. " +
"Make sure db.properties is in the CLASSPATH");
return;
}
String logFile = dbProps.getProperty("logfile",
"DBConnectionManager.log");
try {
log = new PrintWriter(new FileWriter(logFile, true), true);
}
catch (IOException e) {
System.err.println("Can not open the log file: " + logFile);
log = new PrintWriter(System.err);
}
loadDrivers(dbProps);
createPools(dbProps);
}
銆銆鏂规硶getResourceAsStream()鏄竴涓爣鍑嗘柟娉曪紝鐢ㄦ潵鎵撳紑涓涓閮ㄨ緭鍏ユ枃浠躲傛枃浠剁殑浣嶇疆鍙栧喅浜庣被鍔犺浇鍣紝鑰屾爣鍑嗙殑绫诲姞杞藉櫒浠 classpath寮濮嬫悳绱€侱b.properties鏂囦欢鏄竴涓狿orperties鏍煎紡鐨勬枃浠讹紝淇濆瓨鍦ㄨ繛鎺ユ睜涓畾涔夌殑key-value瀵广備笅闈竴浜涘父鐢ㄧ殑灞炴у彲浠ュ畾涔夛細
銆銆銆drivers 浠ョ┖鏍煎垎寮鐨刯dbc椹卞姩绋嬪簭鐨勫垪琛
銆銆銆logfile 鏃ュ織鏂囦欢鐨勭粷瀵硅矾寰
銆銆姣忎釜杩炴帴姹犱腑杩樹娇鐢ㄥ彟涓浜涘睘鎬с傝繖浜涘睘鎬т互杩炴帴姹犵殑鍚嶅瓧寮澶达細
銆銆銆.url鏁版嵁搴撶殑JDBC URL
銆銆銆.maxconn鏈澶ц繛鎺ユ暟銆0琛ㄧず鏃犻檺銆
銆銆銆.user杩炴帴姹犵殑鐢ㄦ埛鍚
銆銆銆.password鐩稿叧鐨勫瘑鐮
銆銆url灞炴ф槸蹇呴』鐨勶紝鍏朵粬灞炴у彲閫夈傜敤鎴峰悕鍜屽瘑鐮佸繀椤诲拰鎵瀹氫箟鐨勬暟鎹簱鍖归厤銆
銆銆涓嬮潰鏄痺indows骞冲彴涓嬬殑涓涓猟b.properties鏂囦欢鐨勪緥瀛愩傛湁涓涓狪nstantDB杩炴帴姹犲拰涓涓氳繃odbc杩炴帴鐨刟ccess鏁版嵁搴撶殑鏁版嵁婧愶紝鍚嶅瓧鍙玠emo銆
drivers=sun.jdbc.odbc.JdbcOdbcDriver jdbc.idbDriver
logfile=D:\\user\\src\\java\\DBConnectionManager\\log.txt
idb.url=jdbc:idb:c:\\local\\javawebserver1.1\\db\\db.prp
idb.maxconn=2
access.url=jdbc:odbc:demo
access.user=demo
access.password=demopw
娉ㄦ剰锛屽弽鏂滅嚎鍦╳indows骞冲彴涓嬪繀椤诲弻鍐欍
銆銆鍒濆鍖栨柟娉昳nit()鍒涘缓涓涓狿orperties瀵硅薄骞惰杞絛b.properties鏂囦欢锛岀劧鍚庤鍙栨棩蹇楁枃浠跺睘鎬с傚鏋滄棩蹇楁枃浠舵病鏈夊懡鍚嶏紝鍒欎娇鐢ㄧ己鐪佺殑鍚嶅瓧DBConnectionManager.log鍦ㄥ綋鍓嶇洰褰曚笅鍒涘缓銆傚湪姝ゆ儏鍐典笅锛屼竴涓郴缁熼敊璇绾綍銆
鏂规硶loadDrivers()灏嗘寚瀹氱殑鎵鏈塲dbc椹卞姩绋嬪簭娉ㄥ唽锛岃杞姐
private void loadDrivers(Properties props) {
String driverClasses = props.getProperty("drivers");
StringTokenizer st = new StringTokenizer(driverClasses);
while (st.hasMoreElements()) {
String driverClassName = st.nextToken().trim();
try {
Driver driver = (Driver)
Class.forName(driverClassName).newInstance();
DriverManager.registerDriver(driver);
drivers.addElement(driver);
log("Registered JDBC driver " + driverClassName);
}
catch (Exception e) {
log("Can not register JDBC driver: " +
driverClassName + ", Exception: " + e);
}
}
}
銆銆loadDrivers()浣跨敤StringTokenizer灏哾irvers灞炴у垎鎴愬崟鐙殑driver涓诧紝骞跺皢姣忎釜椹卞姩绋嬪簭瑁呭叆java铏氭嫙鏈恒傞┍鍔ㄧ▼搴忕殑瀹炰緥鍦↗DBC 鐨凞riverManager涓敞鍐岋紝骞跺姞鍏ヤ竴涓鏈夌殑鍚戦噺drivers涓傚悜閲廳rivers鐢ㄦ潵鍏抽棴鍜屾敞閿鎵鏈夌殑椹卞姩绋嬪簭銆
銆銆鐒跺悗锛孌BConnectionPool瀵硅薄鐢辩鏈夋柟娉昪reatePools()鍒涘缓銆
private void createPools(Properties props) {
Enumeration propNames = props.propertyNames();
while (propNames.hasMoreElements()) {
String name = (String) propNames.nextElement();
if (name.endsWith(".url")) {
String poolName = name.substring(0, name.lastIndexOf("."));
String url = props.getProperty(poolName + ".url");
if (url == null) {
log("No URL specified for " + poolName);
continue;
}
String user = props.getProperty(poolName + ".user");
String password = props.getProperty(poolName + ".password");
String maxconn = props.getProperty(poolName + ".maxconn", "0");
int max;
try {
max = Integer.valueOf(maxconn).intValue();
}
catch (NumberFormatException e) {
log("Invalid maxconn value " + maxconn + " for " +
poolName);
max = 0;
}
DBConnectionPool pool =
new DBConnectionPool(poolName, url, user, password, max);
pools.put(poolName, pool);
log("Initialized pool " + poolName);
}
}
}
銆銆涓涓灇涓惧璞′繚瀛樻墍鏈夌殑灞炴у悕锛屽鏋滃睘鎬у悕甯︽湁.url缁撳熬锛屽垯琛ㄧず鏄竴涓繛鎺ユ睜瀵硅薄闇瑕佽瀹炰緥鍖栥傚垱寤虹殑杩炴帴姹犲璞′繚瀛樺湪涓涓狧ashtable瀹炰緥鍙橀噺涓傝繛鎺ユ睜鍚嶅瓧浣滀负绱㈠紩锛岃繛鎺ユ睜瀵硅薄浣滀负鍊笺
銆銆浜屻佸緱鍒板拰杩斿洖杩炴帴
銆銆DBConnectionManager鎻愪緵getConnection锛堬級鏂规硶鍜宖reeConnection鏂规硶锛岃繖浜涙柟娉曟湁瀹㈡埛绋嬪簭浣跨敤銆傛墍鏈夌殑鏂规硶浠ヨ繛鎺ユ睜鍚嶅瓧鎵鍙傛暟锛屽苟璋冪敤鐗瑰畾鐨勮繛鎺ユ睜瀵硅薄銆
public Connection getConnection(String name) {
DBConnectionPool pool = (DBConnectionPool) pools.get(name);
if (pool != null) {
return pool.getConnection();
}
return null;
}
public Connection getConnection(String name, long time) {
DBConnectionPool pool = (DBConnectionPool) pools.get(name);
if (pool != null) {
return pool.getConnection(time);
}
return null;
}
public void freeConnection(String name, Connection con) {
DBConnectionPool pool = (DBConnectionPool) pools.get(name);
if (pool != null) {
pool.freeConnection(con);
}
}
銆銆涓夈佸叧闂
銆銆鏈鍚庯紝鐢变竴涓猺elease锛堬級鏂规硶锛岀敤鏉ュ畬濂藉湴鍏抽棴杩炴帴姹犮傛瘡涓狣BConnectionManager瀹㈡埛蹇呴』璋冪敤getInstance()鏂规硶寮曠敤銆傛湁涓涓鏁板櫒璺熻釜瀹㈡埛鐨勬暟閲忋傛柟娉時elease()鍦ㄥ鎴峰叧闂椂璋冪敤锛屾妧鏈櫒鍑1銆傚綋鏈鍚庝竴涓鎴烽噴鏀撅紝DBConnectionManager 鍏抽棴鎵鏈夌殑杩炴帴姹犮
List 11-14
public synchronized void release() {
// Wait until called by the last client
if (--clients != 0) {
return;
}
Enumeration allPools = pools.elements();
while (allPools.hasMoreElements()) {
DBConnectionPool pool = (DBConnectionPool) allPools.nextElement();
pool.release();
}
Enumeration allDrivers = drivers.elements();
while (allDrivers.hasMoreElements()) {
Driver driver = (Driver) allDrivers.nextElement();
try {
DriverManager.deregisterDriver(driver);
log("Deregistered JDBC driver " + driver.getClass().getName());
}
catch (SQLException e) {
log(e, "Can not deregister JDBC driver: " +
driver.getClass().getName());
}
}
}
褰撴墍鏈夎繛鎺ユ睜鍏抽棴锛屾墍鏈塲dbc椹卞姩绋嬪簭涔熻娉ㄩ攢銆




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











