數(shù)據(jù)庫連接是一種關(guān)鍵的有限的昂貴的資源 ,這在多用戶網(wǎng)頁應(yīng)用程序中體現(xiàn)的尤為突出.對(duì)數(shù)據(jù)庫連接的管理能顯著影響到整個(gè)應(yīng)用程序的伸縮性和健壯性,影響到程序的性能指標(biāo),數(shù)據(jù)庫連接池正是針對(duì)這個(gè)問題提出的
數(shù)據(jù)庫連接池負(fù)責(zé)分配 , 管理和釋放數(shù)據(jù)庫連接 , 它允許應(yīng)用程序重復(fù)使用一個(gè)現(xiàn)有的數(shù)據(jù)庫連接 , 而不是再重新建立一個(gè) ; 釋放空閑時(shí)間超過最大空閑時(shí)間的數(shù)據(jù)庫連接來避免因?yàn)闆]有釋放數(shù)據(jù)庫連接而引起的數(shù)據(jù)庫連接遺漏 , 這樣可以明顯提高對(duì)數(shù)據(jù)庫操作的性能
數(shù)據(jù)庫連接池在初始化的時(shí)將創(chuàng)建一定數(shù)量的數(shù)據(jù)庫連接放到連接池中 , 這些數(shù)據(jù)庫連接的數(shù)量是又最小數(shù)據(jù)庫連接數(shù)來設(shè)定的 , 無論這些數(shù)據(jù)庫連接是否被使用 , 連接池都將一直保證至少擁有這么多的連接數(shù) , 當(dāng)應(yīng)用程序向連接池請(qǐng)求的連接數(shù)超過最大連接數(shù)量時(shí) , 這些請(qǐng)求將被加入到等待隊(duì)列中 .
數(shù)據(jù)庫連接池的最小連接數(shù)和最大連接數(shù)的設(shè)置要考慮到下列幾個(gè)因素 :
1. 最小連接數(shù)是連接池一直保持的數(shù)據(jù)庫連接 , 所以如果應(yīng)用程序?qū)?shù)據(jù)庫連接的使用量不大 , 將會(huì)有大量的數(shù)據(jù)庫連接資源被浪費(fèi) .
2. 最大連接數(shù)是連接池申請(qǐng)的最大連接數(shù) , 如果數(shù)據(jù)庫連接請(qǐng)求超過次數(shù) , 后面的數(shù)據(jù)庫連接請(qǐng)求將被加入到等待對(duì)了中 , 這回影響之后的數(shù)據(jù)庫操作
如果最小連接數(shù)與最大連接數(shù)相差太大 , 那么最先的連接請(qǐng)求將會(huì)獲利 , 之后超過最小連接數(shù)量的連接請(qǐng)求等價(jià)于建立一個(gè)新的數(shù)據(jù)庫連接 , 不過 , 這些小于最小連接數(shù)的數(shù)據(jù)庫連接在使用完不會(huì)馬上被釋放 , 它將被放到連接池中等待重復(fù)使用或是空閑超時(shí)被釋放 .
實(shí)例使用的 Tomcat 版本為 6.0
方法一:?在
Tomcat的conf/context.xml
中配置
在
Tomcat\apache-tomcat-6.0.33\conf
目錄下的
context.xml
文件中配置
默認(rèn)值如下:
<?
xml version='1.0' encoding='utf-8'
?>
<
Context
>
<
WatchedResource
>
WEB-INF/web.xml
</
WatchedResource
>
</
Context
>
配置連接池
<?
xml version='1.0' encoding='utf-8'
?>
<
Context
>
<
WatchedResource
>
WEB-INF/web.xml
</
WatchedResource
>
<!--
配置oracle數(shù)據(jù)庫的連接池
-->
<
Resource
name
="jdbc/oracleds"
author
="Container"
type
="javax.sql.DataSource"
maxActive
="100"
maxIdle
="30"
maxWait
="10000"
username
="scott"
password
="tiger"
driverClassName
="oracle.jdbc.dirver.OracleDriver"
url
="jdbc:oracle:thin:@127.0.0.1:1521:ora9"
/>
<!--
配置mysql數(shù)據(jù)庫的連接池,
需要做的額外步驟是將mysql的Java驅(qū)動(dòng)類放到tomcat的lib目錄下
maxIdle 連接池中最多可空閑maxIdle個(gè)連接
minIdle 連接池中最少空閑maxIdle個(gè)連接
initialSize 初始化連接數(shù)目
maxWait 連接池中連接用完時(shí),新的請(qǐng)求等待時(shí)間,毫秒
username 數(shù)據(jù)庫用戶名
password 數(shù)據(jù)庫密碼
-->
<
Resource
name
="jdbc/mysqlds"
auth
="Container"
type
="javax.sql.DataSource"
username
="root"
password
="root"
maxIdle
="30"
maxWait
="10000"
maxActive
="100"
driverClassName
="com.mysql.jdbc.Driver"
url
="jdbc:mysql://127.0.0.1:3306/db_blog"
/>
</
Context
>
?
配置好后需要注意的兩個(gè)步驟
1. 將對(duì)應(yīng)數(shù)據(jù)庫的驅(qū)動(dòng)類放到 tomcat 的 lib 目錄西安
2. 重新啟動(dòng) tomcat 服務(wù)器 , 讓配置生效
在 web 應(yīng)用程序的 web.xml 中設(shè)置數(shù)據(jù)源參考 , 如下:
在 <web-app></web-app> 節(jié)點(diǎn) 中加入下面內(nèi)容
<
resource-ref
>
<
description
>
mysql數(shù)據(jù)庫連接池
</
description
>
<!--
參考數(shù)據(jù)源名字,同Tomcat中配置的Resource節(jié)點(diǎn)中name屬性值"jdbc/mysqlds"一致
-->
<
res-ref-name
>
jdbc/mysqlds
</
res-ref-name
>
<!--
資源類型
-->
<
res-type
>
javax.sql.DataSource
</
res-type
>
<
res-auth
>
Container
</
res-auth
>
<
res-sharing-scope
>
Shareable
</
res-sharing-scope
>
</
resource-ref
>
?
錯(cuò)誤解決
javax.naming.NoInitialContextException: Need to specify class name in environment or system property, or as an applet parameter, or in an application resource file: java.naming.factory.initial
at javax.naming.spi.NamingManager.getInitialContext(NamingManager.java:645)
at javax.naming.InitialContext.getDefaultInitCtx(InitialContext.java:288)
at javax.naming.InitialContext.getURLOrDefaultInitCtx(InitialContext.java:325)
at javax.naming.InitialContext.lookup(InitialContext.java:392)
at com.iblog.util.DBPoolUtil.
<
clinit
>
(DBPoolUtil.java:34)
解決方案:
上面的異常信息是配置文件中JNDI沒有初始化造成的
如果下面的問題都不存在
1.要去檢查下配置文件中連接數(shù)據(jù)庫的URL參數(shù)是否正確 2.以及是否導(dǎo)入了正常的包 3.檢查在Tomcat中conf/server.xml文件,檢查是 否設(shè)置useNaming="false",如果是,去掉
2.那就是通過 main方法測(cè)試的,這個(gè)數(shù)據(jù)源不支持這樣的測(cè)試方法,程序要運(yùn)行在 Tomcat中才能找到相應(yīng)的數(shù)據(jù)源 .[我在測(cè)試時(shí)犯這樣的錯(cuò)導(dǎo)致上面錯(cuò)誤出現(xiàn)]
?
<
%@ page
language
="java"
pageEncoding
="UTF-8"
contentType
="text/html; charset=UTF-8"
%
>
<
%@ page
import
="java.sql.*"
%
>
<
%@ page
import
="javax.naming.*"
%
>
<
%@ page
import
="javax.sql.DataSource"
%
>
<
html
>
<
head
>
<
title
>
Tomcat6.0 JNDI!
</
title
>
</
head
>
<
body
>
Tomcat連接池測(cè)試,獲取數(shù)據(jù)源
<
br
>
<
%
try
{
//初始化查找命名空間
Context ctx
= new
InitialContext();
//參數(shù)java:/comp/env為固定路徑
Context envContext
= (Context)ctx.lookup("java:/comp/env");
//參數(shù)jdbc/mysqlds為數(shù)據(jù)源和JNDI綁定的名字
DataSource ds
= (DataSource)envContext.lookup("jdbc/mysqlds");
Connection conn
= ds.getConnection();
conn.close();
out.println("<span style
='color:red;'
>
JNDI測(cè)試成功
<
span
>
");
} catch (NamingException e) {
e.printStackTrace();
} catch (SQLException e) {
e.printStackTrace();
}
%>
</
body
>
</
html
>
運(yùn)行效果:
?
?
?
方法二:在Tomcat的conf/server.xml中配置
打開 tomcat 的 conf/server.xml 文件,找到 <GlobalNamingResources></GlobalNamingResources> 節(jié)點(diǎn) , 默認(rèn)的內(nèi)容如下
<
GlobalNamingResources
>
<
Resource
name
="UserDatabase"
auth
="Container"
type
="org.apache.catalina.UserDatabase"
description
="User database that can be updated and saved"
factory
="org.apache.catalina.users.MemoryUserDatabaseFactory"
pathname
="conf/tomcat-users.xml"
/>
</
GlobalNamingResources
>
在該節(jié)點(diǎn)中加入相關(guān)的池配置信息,如下
<
GlobalNamingResources
>
<
Resource
name
="UserDatabase"
auth
="Container"
type
="org.apache.catalina.UserDatabase"
description
="User database that can be updated and saved"
factory
="org.apache.catalina.users.MemoryUserDatabaseFactory"
pathname
="conf/tomcat-users.xml"
/>
<!--
配置mysql數(shù)據(jù)庫的連接池,
需要做的額外步驟是將mysql的Java驅(qū)動(dòng)類放到tomcat的lib目錄下
-->
<
Resource
name
="jdbc/mysqlds"
auth
="Container"
type
="javax.sql.DataSource"
username
="root"
password
="root"
maxIdle
="30"
maxWait
="10000"
maxActive
="100"
driverClassName
="com.mysql.jdbc.Driver"
url
="jdbc:mysql://127.0.0.1:3306/db_blog"
/>
</
GlobalNamingResources
>
?
在 tomcat 的 conf/context.xml 文件中的 <Context></Context> 節(jié)點(diǎn)中加入如下內(nèi)容
<
ResourceLink
name
="jdbc/mysqlds"
global
="jdbc/mysqlds"
type
="javax.sql.DataSource"
/>
然后在 web 項(xiàng)目中的 WEB-INF 目錄下的 web.xml 中配置
<
resource-ref
>
<
description
>
mysql數(shù)據(jù)庫連接池
</
description
>
<!--
參考數(shù)據(jù)源名字,同Tomcat中配置的Resource節(jié)點(diǎn)中name屬性值"jdbc/mysqlds"一致
-->
<
res-ref-name
>
jdbc/mysqlds
</
res-ref-name
>
<!--
資源類型
-->
<
res-type
>
javax.sql.DataSource
</
res-type
>
<
res-auth
>
Container
</
res-auth
>
<
res-sharing-scope
>
Shareable
</
res-sharing-scope
>
</
resource-ref
>
同樣配置好后,需要重新啟動(dòng)服務(wù)器,讓配置生效.
??
方法三:在Tomcat的conf/server.xml中配置虛擬目錄時(shí)配置?
在配置虛擬目錄時(shí),也就是在配置 conf 下面的 server.xml 時(shí) , 在 context 標(biāo)簽內(nèi) 添加池配置 .
在說該方法之前 , 先說一下 , 如何用 tomcat 配置虛擬目錄
在 tomcat\conf 下 server.xml 中找到
<
Host
name
="localhost"
appBase
="webapps"
unpackWARs
="true"
autoDeploy
="true"
xmlValidation
="false"
xmlNamespaceAware
="false"
>
</
Host
>
在其中添加:
<
Context
path
="/website"
docBase
="F:/myweb"
reloadable
="true"
></
Context
>
注意 :
docBase 要改成你的項(xiàng)目目錄。
path 為虛擬路徑 , 訪問時(shí)的路徑,注意 : 一定要加 “/”? debug 建議設(shè)置為 0
reloadable 設(shè)置為 true 。??
這樣重新啟動(dòng) tomcat
實(shí)例中如下配置
<
Context
path
="/website"
docBase
="D:/program files/Tomcat/apache-tomcat-6.0.33/webapps/iblog.war"
reloadable
="true"
>
</
Context
>
接下來添加池配置, 如下
<!--
配置虛擬目錄
-->
<
Context
path
="/website"
docBase
="D:/program files/Tomcat/apache-tomcat-6.0.33/webapps/iblog.war"
reloadable
="true"
>
<
Resource
name
="jdbc/mysqlds"
auth
="Container"
type
="javax.sql.DataSource"
username
="root"
password
="root"
maxIdle
="30"
maxWait
="10000"
maxActive
="100"
driverClassName
="com.mysql.jdbc.Driver"
url
="jdbc:mysql://127.0.0.1:3306/db_blog"
/>
</
Context
>
啟動(dòng)服務(wù)器,測(cè)試,注意因?yàn)槲覀兣渲昧藀ath值為”/website”,所以訪問的路徑應(yīng)該為website.如下圖:
?
方法四:在Web項(xiàng)目中的META-INF目錄下新建一個(gè)文件context.xml,寫入配置
注意:是META-INF目錄下,不是WEB-INF目錄下
<?
xml version='1.0' encoding='utf-8'
?>
<
Context
>
<
Resource
name
="jdbc/mysqlds"
auth
="Container"
type
="javax.sql.DataSource"
username
="root"
password
="root"
maxIdle
="30"
maxWait
="10000"
maxActive
="100"
driverClassName
="com.mysql.jdbc.Driver"
url
="jdbc:mysql://127.0.0.1:3306/db_blog"
logAbandoned
="true"
/>
</
Context
>
?
更多文章、技術(shù)交流、商務(wù)合作、聯(lián)系博主
微信掃碼或搜索:z360901061
微信掃一掃加我為好友
QQ號(hào)聯(lián)系: 360901061
您的支持是博主寫作最大的動(dòng)力,如果您喜歡我的文章,感覺我的文章對(duì)您有幫助,請(qǐng)用微信掃描下面二維碼支持博主2元、5元、10元、20元等您想捐的金額吧,狠狠點(diǎn)擊下面給點(diǎn)支持吧,站長(zhǎng)非常感激您!手機(jī)微信長(zhǎng)按不能支付解決辦法:請(qǐng)將微信支付二維碼保存到相冊(cè),切換到微信,然后點(diǎn)擊微信右上角掃一掃功能,選擇支付二維碼完成支付。
【本文對(duì)您有幫助就好】元

