數(shù)據(jù)庫連接是一種關(guān)鍵的有限的昂貴的資源 , 這在多用戶網(wǎng)頁應(yīng)用程序中體現(xiàn)的尤為突出 . 對數(shù)據(jù)庫連接的管理能顯著影響到整個應(yīng)用程序的伸縮性和健壯性 , 影響到程序的性能指標(biāo) , 數(shù)據(jù)庫連接池正是針對這個問題提出的
?
數(shù)據(jù)庫連接池負(fù)責(zé)分配 , 管理和釋放數(shù)據(jù)庫連接 , 它允許應(yīng)用程序重復(fù)使用一個現(xiàn)有的數(shù)據(jù)庫連接 , 而不是再重新建立一個 ; 釋放空閑時間超過最大空閑時間的數(shù)據(jù)庫連接來避免因為沒有釋放數(shù)據(jù)庫連接而引起的數(shù)據(jù)庫連接遺漏 , 這樣可以明顯提高對數(shù)據(jù)庫操作的性能
?
數(shù)據(jù)庫連接池在初始化的時將創(chuàng)建一定數(shù)量的數(shù)據(jù)庫連接放到連接池中 , 這些數(shù)據(jù)庫連接的數(shù)量是又最小數(shù)據(jù)庫連接數(shù)來設(shè)定的 , 無論這些數(shù)據(jù)庫連接是否被使用 , 連接池都將一直保證至少擁有這么多的連接數(shù) , 當(dāng)應(yīng)用程序向連接池請求的連接數(shù)超過最大連接數(shù)量時 , 這些請求將被加入到等待隊列中 .
數(shù)據(jù)庫連接池的最小連接數(shù)和最大連接數(shù)的設(shè)置要考慮到下列幾個因素 :
1. 最小連接數(shù)是連接池一直保持的數(shù)據(jù)庫連接 , 所以如果應(yīng)用程序?qū)?shù)據(jù)庫連接的使用量不大 , 將會有大量的數(shù)據(jù)庫連接資源被浪費 .
2. 最大連接數(shù)是連接池申請的最大連接數(shù) , 如果數(shù)據(jù)庫連接請求超過次數(shù) , 后面的數(shù)據(jù)庫連接請求將被加入到等待對了中 , 這回影響之后的數(shù)據(jù)庫操作
如果最小連接數(shù)與最大連接數(shù)相差太大 , 那么最先的連接請求將會獲利 , 之后超過最小連接數(shù)量的連接請求等價于建立一個新的數(shù)據(jù)庫連接 , 不過 , 這些小于最小連接數(shù)的數(shù)據(jù)庫連接在使用完不會馬上被釋放 , 它將被放到連接池中等待重復(fù)使用或是空閑超時被釋放 .
實例使用的 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ū)動類放到tomcat的lib目錄下
maxIdle 連接池中最多可空閑maxIdle個連接
minIdle 連接池中最少空閑maxIdle個連接
initialSize 初始化連接數(shù)目
maxWait 連接池中連接用完時,新的請求等待時間,毫秒
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
>
配置好后需要注意的兩個步驟
1. 將對應(yīng)數(shù)據(jù)庫的驅(qū)動類放到 tomcat 的 lib 目錄西安
2. 重新啟動 tomcat 服務(wù)器 , 讓配置生效
在 web 應(yīng)用程序的 web.xml 中設(shè)置數(shù)據(jù)源參考 , 如下:
在 <web-app></web-app> 節(jié)點 中加入下面內(nèi)容
<
resource-ref
>
<
description
>
mysql數(shù)據(jù)庫連接池
</
description
>
<!--
參考數(shù)據(jù)源名字,同Tomcat中配置的Resource節(jié)點中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
>
?
錯誤解決
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方法測試的,這個數(shù)據(jù)源不支持這樣的測試方法,程序要運行在 Tomcat中才能找到相應(yīng)的數(shù)據(jù)源 .[我在測試時犯這樣的錯導(dǎo)致上面錯誤出現(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連接池測試,獲取數(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測試成功<span>"
);
}
catch
(NamingException e) {
e.printStackTrace();
}
catch
(SQLException e) {
e.printStackTrace();
}
%>
</body>
</html>
運行效果:
?
?
?
方法二 : 在 Tomcat 的 conf/ server.xml 中配置
打開 tomcat 的 conf/server.xml 文件,找到 <GlobalNamingResources></GlobalNamingResources> 節(jié)點 , 默認(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é)點中加入相關(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ū)動類放到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é)點中加入如下內(nèi)容
<
ResourceLink
name
="jdbc/mysqlds"
global
="jdbc/mysqlds"
type
="javax.sql.DataSource"
/>
然后在 web 項目中的 WEB-INF 目錄下的 web.xml 中配置
<
resource-ref
>
<
description
>
mysql數(shù)據(jù)庫連接池
</
description
>
<!--
參考數(shù)據(jù)源名字,同Tomcat中配置的Resource節(jié)點中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
>
同樣配置好后,需要重新啟動服務(wù)器,讓配置生效.
?
?
?
方法三 : 在 Tomcat 的 conf/server.xml 中配置虛擬目錄時配置 ?
在配置虛擬目錄時,也就是在配置 conf 下面的 server.xml 時 , 在 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 要改成你的項目目錄。
path 為虛擬路徑 , 訪問時的路徑,注意 : 一定要加 “/”? debug 建議設(shè)置為 0
reloadable 設(shè)置為 true 。??
這樣重新啟動 tomcat
實例中如下配置
<
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
>
啟動服務(wù)器 , 測試 , 注意因為我們配置了 path 值為 ” /website ” , 所以訪問的路徑應(yīng)該為 website. 如下圖:
?
方法四:在 Web 項目中的 META-INF 目錄下新建一個文件 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
>
?
轉(zhuǎn)載請注明出處:[ http://www.cnblogs.com/dennisit/archive/2013/04/04/2999657.html ]
更多文章、技術(shù)交流、商務(wù)合作、聯(lián)系博主
微信掃碼或搜索:z360901061
微信掃一掃加我為好友
QQ號聯(lián)系: 360901061
您的支持是博主寫作最大的動力,如果您喜歡我的文章,感覺我的文章對您有幫助,請用微信掃描下面二維碼支持博主2元、5元、10元、20元等您想捐的金額吧,狠狠點擊下面給點支持吧,站長非常感激您!手機微信長按不能支付解決辦法:請將微信支付二維碼保存到相冊,切換到微信,然后點擊微信右上角掃一掃功能,選擇支付二維碼完成支付。
【本文對您有幫助就好】元

