国产日韩欧美一区二区三区三州_亚洲少妇熟女av_久久久久亚洲av国产精品_波多野结衣网站一区二区_亚洲欧美色片在线91_国产亚洲精品精品国产优播av_日本一区二区三区波多野结衣 _久久国产av不卡

?

WEB開發(fā)中數(shù)據(jù)庫連接池的使用技巧

2019-12-19 17:50:25王磊孟祥武李平
數(shù)碼設(shè)計(jì) 2019年3期
關(guān)鍵詞:李平王磊調(diào)用

王磊,孟祥武,李平

WEB開發(fā)中數(shù)據(jù)庫連接池的使用技巧

王磊,孟祥武,李平

(煙臺海港信息通信有限公司,山東煙臺, 264000)

本文結(jié)合自己在煙臺港生產(chǎn)管理系統(tǒng)中多年的軟件開發(fā)經(jīng)驗(yàn),創(chuàng)造性的提出一種性能更加優(yōu)異的數(shù)據(jù)庫連接池技術(shù),大大提高了港口業(yè)務(wù)系統(tǒng)海量數(shù)據(jù)的訪問性能,具有重要的推廣價(jià)值。

Web開發(fā);JSP;數(shù)據(jù)庫連接池

1 引言

隨著信息技術(shù)的進(jìn)步,B/S結(jié)構(gòu)開發(fā)的各種系統(tǒng)應(yīng)用得愈發(fā)廣泛,港口很多系統(tǒng)都使用B/S結(jié)構(gòu)進(jìn)行WEB開發(fā)的,而這其中如何連接數(shù)據(jù)庫便成為無法繞開的問題。傳統(tǒng)的JDBC連接方式固然應(yīng)用簡單,但在面對海量的數(shù)據(jù)訪問要求時(shí),無論在資源配置還是反應(yīng)時(shí)間上都顯得力不從心,于是表現(xiàn)更加優(yōu)異的數(shù)據(jù)庫連接池技術(shù)便成為非常有利的選擇。

2 問題的提出

連接池基本的思想是在系統(tǒng)初始化的時(shí)候,將數(shù)據(jù)庫連接作為對象存儲在內(nèi)存中,當(dāng)用戶需要訪問數(shù)據(jù)庫時(shí),并非建立一個(gè)新的連接,而是從連接池中取出一個(gè)已建立的空閑連接對象。使用完畢后,用戶也并非將連接關(guān)閉,而是將連接放回連接池中,以供下一個(gè)請求訪問使用。而連接的建立、斷開都由連接池自身來管理。同時(shí),還可以通過設(shè)置連接池的參數(shù)來控制連接池中的初始連接數(shù)、連接的上下限數(shù)以及每個(gè)連接的最大使用次數(shù)、最大空閑時(shí)間等等。也可以通過其自身的管理機(jī)制來監(jiān)視數(shù)據(jù)庫連接的數(shù)量、使用情況等。

3 問題的原因與解決方法

為了更好的理解和使用數(shù)據(jù)庫連接池,我們可以嘗試封裝一個(gè)數(shù)據(jù)庫連接池類,并在開發(fā)過程中調(diào)用它,這樣我們更加靈活的掌握數(shù)據(jù)庫連接池技術(shù)。關(guān)鍵代碼如下:

public synchronized void createPool() throws Exception {

// 確保連接池沒有創(chuàng)建

// 如果連接池己經(jīng)創(chuàng)建了,保存連接的向量 connections 不會為空

if (connections != null) {

return; // 如果己經(jīng)創(chuàng)建,則返回

}

// 實(shí)例化 JDBC Driver 中指定的驅(qū)動類實(shí)例

Driver driver = (Driver) (Class.forName(this.jdbcDriver).newInstance());

DriverManager.registerDriver(driver); // 注冊 JDBC 驅(qū)動程序

// 創(chuàng)建保存連接的向量 , 初始時(shí)有 0 個(gè)元素

connections = new Vector();

// 根據(jù) initialConnections 中設(shè)置的值,創(chuàng)建連接。

createConnections(this.initialConnections);

System.out.println("create pool");

}

然后創(chuàng)建由 numConnections 指定數(shù)目的數(shù)據(jù)庫連接,并把這些連接放入 connections 向量中。

private void createConnections(int numConnections) throws SQLException {

// 循環(huán)創(chuàng)建指定數(shù)目的數(shù)據(jù)庫連接

for (int x = 0; x < numConnections; x++) {

System.out.println(this.connections.size() + "," + this.maxConnections);

if (this.maxConnections > 0 && this.connections.size() >= this.maxConnections) {

System.out.println("連接數(shù)己經(jīng)達(dá)到最大");

break;

}

try {

connections.addElement(new PooledConnection(newConnection()));

} catch (SQLException e) {

System.out.println(" 創(chuàng)建數(shù)據(jù)庫連接失??! " + e.getMessage());

throw new SQLException();

}

System.out.println(" 數(shù)據(jù)庫連接己創(chuàng)建 ......");

}

}

創(chuàng)建一個(gè)新的數(shù)據(jù)庫連接并通過調(diào)用 getFreeConnection() 函數(shù)返回一個(gè)可用的數(shù)據(jù)庫連接,使用完畢關(guān)閉連接。

再創(chuàng)建一個(gè)DBManager來對連接池進(jìn)行相應(yīng)的控制。關(guān)鍵代碼如下:

public DBManager() {

if (inst != null)

return;

// TODO Auto-generated constructor stub

String connStr = String.format("jdbc:mysql://%s:%d/%s", Config.getInstance().mysqlHost, Config.getInstance().mysqlPort,

Config.getInstance().mysqlDB);

connectionPool = new ConnectionPool("com.mysql.jdbc.Driver", connStr, Config.getInstance().mysqlUser, Config.getInstance().mysqlPassword);

try {

connectionPool.createPool();

inst = this;

} catch (Exception e) {

// TODO Auto-generated catch block

e.printStackTrace();

}

}

public static PooledConnection getConnection() {

if (inst == null)

new DBManager();

try {

conn = connectionPool.getConnection();

} catch (SQLException e) {

// TODO Auto-generated catch block

e.printStackTrace();

}

return conn;

}

這樣就完成了一個(gè)數(shù)據(jù)庫連接池類的封裝,我們可以在程序中加以調(diào)用,滿足所需要的數(shù)據(jù)庫連接要求。

4 結(jié)束語:

B/S結(jié)構(gòu)系統(tǒng)開發(fā)的過程中,數(shù)據(jù)庫的連接效率是非常重要的技術(shù)指標(biāo),在工作與學(xué)習(xí)的過程中不斷的研究與使用,會極大地提高自己的開發(fā)水準(zhǔn),滿足用戶不斷提高的要求與體驗(yàn)。

[1] 劉曉華、周慧貞.JSP開發(fā)應(yīng)用詳解. 北京:電子工業(yè)出版社

[2] Stephens.數(shù)據(jù)庫設(shè)計(jì). 北京:機(jī)械工業(yè)出版社

[3] 趙松濤. Oracle 9i中文版基礎(chǔ)培訓(xùn)教程. 北京:人民郵電出版社.

[4] 楊瑤.新課程研究(下旬刊)基于Java的Web數(shù)據(jù)庫連接池技術(shù)研究.2008

[5] 周彩蘭,陳才賢.武漢理工大學(xué)學(xué)報(bào)(信息與管理工程版) 2004

Application skills of database connection pool in WEB development

Wang Lei,Meng,Xiangwu,Li Ping

(Yantai SeaPort Information Communication Co., Ltd. , Yantai 264000, China)

Combining with many years of software development experience in Yantai Port production management system, this paper creatively proposes a database connection pool technology with better performance, which greatly improves the access performance of massive data in port business system and has important popularization value.

Web development JSP database connection pool

10.19551/j.cnki.issn1672-9129.2019.03.007

TP311.10

A

1672-9129(2019)03-0021-03

王磊(1972-),男,主要研究方向或從事的工作:港口生產(chǎn)系統(tǒng)管理及推廣應(yīng)用。E-mail:bianjibu20080808@163.com

猜你喜歡
李平王磊調(diào)用
Structure of continuous matrix product operator for transverse field Ising model: An analytic and numerical study
四季的美
核電項(xiàng)目物項(xiàng)調(diào)用管理的應(yīng)用研究
Carriage to eternity: image of death in Dickinson and Donne
青年生活(2019年29期)2019-09-10 06:46:01
第三個(gè)小板凳
LabWindows/CVI下基于ActiveX技術(shù)的Excel調(diào)用
THE OSCILLATION OF THE POISSON SEMIGROUP ASSOCIATED TO PARABOLIC HERMITE OPERATOR?
不再被“圓”困住
基于系統(tǒng)調(diào)用的惡意軟件檢測技術(shù)研究
“根本停不下來”
华容县| 阿巴嘎旗| 炎陵县| 高陵县| 会东县| 怀集县| 太仆寺旗| 镇原县| 荣成市| 双辽市| 仁怀市| 武强县| 云安县| 斗六市| 泰宁县| 监利县| 博白县| 永胜县| 隆子县| 巴林右旗| 荣成市| 南丰县| 武鸣县| 确山县| 青阳县| 东乡县| 望都县| 天祝| 芒康县| 龙岩市| 安图县| 雅安市| 原平市| 韶关市| 玉树县| 桃江县| 靖州| 青冈县| 章丘市| 冷水江市| 临澧县|