摘要數(shù)據(jù)庫處理是應用系統(tǒng)業(yè)務處理中最耗時的步驟。在應用服務器系統(tǒng)中,一般都采用數(shù)據(jù)庫連接池(Connection Pool)的技術來解決。本文論述了連接池的工作原理及程序?qū)崿F(xiàn),分析并給出了一個基本實現(xiàn),以更好地理解這種模型。
關鍵詞JDBC;數(shù)據(jù)庫連接池;緩沖池
中圖分類號 TP3文獻標識碼A 文章編號1673-9671-(2009)121-0016-02
1引言
在使用JDBC技術開發(fā)Java和數(shù)據(jù)庫有關應用的時候,隨著使用人數(shù)的增加系統(tǒng)的性能下降非常明顯,深入研究發(fā)現(xiàn)系統(tǒng)性能下降和Connection這個對象的創(chuàng)建有關系。問題的根源就在于對數(shù)據(jù)庫連接資源的低效管理上。對于共享資源,有一個很聞名的設計模式:資源池。該模式正是為了解決資源的頻繁分配、釋放所造成的問題。為解決這一問題,在應用服務器系統(tǒng)中,比如:Tomcat、Weblogic等,一般都采用數(shù)據(jù)庫連接池(Connection Pool)的技術。本文對此項技術進行了深入分析。
2數(shù)據(jù)庫連接池技術
2.1概述
數(shù)據(jù)庫連接池的基本思想就是為數(shù)據(jù)庫連接建立一個“緩沖池”。預先在緩沖池中放入一定數(shù)量的連接,當需要建立數(shù)據(jù)庫連接時,只需從“緩沖池”中取出一個,使用完畢之后再放回去。即在系統(tǒng)初起,或者初次使用時,完成數(shù)據(jù)庫的連接,此后不再釋放此連接,而是在處理后面的請求時反復使用這些已經(jīng)建立的連接。這種方式可以大大減少數(shù)據(jù)庫的處理時間,有利于提高系統(tǒng)的整體性能,因此被廣泛地應用在各種應用服務器產(chǎn)品中。
2.2運行原理
其工作原理如圖所示:
1)建立數(shù)據(jù)庫連接池對象—ConnectionPool,先設定Connection的數(shù)量;
2)對于數(shù)據(jù)庫的訪問請求,直接從連接池中取得一個連接。如果數(shù)據(jù)庫連接池對象中沒有空閑的連接,則等待;
3)進行數(shù)據(jù)存取操作,操作完畢釋放連接,回收到連接池;
4)關閉數(shù)據(jù)庫,釋放所有數(shù)據(jù)庫連接;
5)系統(tǒng)退出時,釋放數(shù)據(jù)庫連接池對象。
2.3程序的實現(xiàn)
實現(xiàn)程序基于MS SQL Server 2005,在MyEclipse環(huán)境下實現(xiàn)。
首先,把直連jar添加到項目中;
其次,在應用中要有一個屬性文件:db.properties用來配置連接池連接數(shù)據(jù)庫的信息,包含以下內(nèi)容;
driver=com.microsoft.sqlserver.jdbc.SQLServerDriver
url=jdbc:sqlserver://localhost:1433;databaseName=zf
user=sa
password=123456
poolSize=10
通過分析,連接池類(ConnectionPool.java)是管理某一數(shù)據(jù)庫所有連接的“緩沖池”,可使用Vector對象來實現(xiàn)。主要實現(xiàn)以下功能:(1)根據(jù)屬性文件創(chuàng)建連接池對象;(2)增加連接對象到連接池中;(3)從連接池中申請連接對象;(4)回收客戶釋放的連接對象;(5)在系統(tǒng)退出前,關閉所有連接對象并釋放連接池占用的系統(tǒng)資源。
由于系統(tǒng)中只能有一個連接池并且某個連接對象(Connection)在某個時刻是獨占的,因此連接池的實現(xiàn)符合單態(tài)設計模式。具體程序代碼如下:
public class ConnectionPool {
private Vector
private String driver; //驅(qū)動
private String url; //連接路徑
private String userName;//用戶名
private String password;//密碼
private int poolSize = 1;//連接池中的最大連接對象數(shù)
private static ConnectionPool instance = 1;
//構(gòu)造方法
private ConnectionPool( ){
init( );
}
//使用單態(tài)設計模式獲得連接池實例
public static ConnectionPool getInstance( ){
if(instance==1){
return new ConnectionPool( );
}
return instance;
}
//初始化連接池
private void init( ) {
pool = new Vector
readConfig( );
addConnection( );
}
//在連接池中創(chuàng)建所有連接對象
private void addConnection() {
Connection conn = 1;
for(int i=0; i try{ Class.forName(driver);//加載JDBC驅(qū)動程序 conn = DriverManager.getConnection(url, userName, password); //建立連接 pool.add(conn); }catch(ClassNotFoundException e){ e.printStackTrace( ); } catch (SQLException e) { e.printStackTrace( ); } } } //讀取屬性文件中的數(shù)據(jù)庫連接信息 private void readConfig( ) { try{ String path = System.getProperty(\"user.dir\") + \"\\\\db.properties\"; FileInputStream is = new FileInputStream(path); Properties pf = new Properties( ); pf.load(is); this.driver = pf.getProperty(\"driver\"); this.url = pf.getProperty(\"url\"); this.userName = pf.getProperty(\"user\"); this.password = pf.getProperty(\"password\"); this.poolSize = Integer.parseInt(pf.getProperty(\"poolSize\")); }catch(Exception e){ e.printStackTrace(); } } //交給用戶使用的釋放連接 public synchronized void release(Connection conn){ pool.add(conn); } //用戶從連接池中申請連接 public synchronized Connection getConnection( ){ if(pool.size( )>0){ Connection conn = (Connection)pool.get(0); pool.remove(conn); return conn; }else{ return 1; } } //關閉所有連接 public synchronized void closePool( ){ for(int i=0; i try { ((Connection)pool.get(i)).close( ); } catch (SQLException e) { e.printStackTrace( ); } pool.remove(i); } } } 3 結(jié)束語 在使用JDBC進行與數(shù)據(jù)庫有關的應用開發(fā)中,采用連接池技術的系統(tǒng)在效率和穩(wěn)定性上比單純地使用JDBC技術的系統(tǒng)要好很多。目前的應用服務器系統(tǒng)在連接池方面的功能上已經(jīng)很完備,直接使用更加方便,剖析它并給出了一個基本實現(xiàn)是為了更好地理解這種模型,在此基礎上進行很多有意義的擴展。 參考文獻 [1]馬亨冰,葉東毅.軟件平臺與中間件技術,2004 [2]鄧萬宇.基于JDBC的數(shù)據(jù)庫連接池高效管理策略[J].現(xiàn)代電子技術,2006