Tomcat5下配置MySQL數(shù)據(jù)庫(kù)連接池
Posted on 2006-08-03 09:55 小李飛刀 閱讀(2645) 評(píng)論(2) 編輯 收藏 所屬分類: 開發(fā)經(jīng)驗(yàn)轉(zhuǎn)錄筆記:不過(guò)遺憾的是,如下幾種方法都沒有在我的機(jī)器上配置成功(Tomcat5.5.17 + WinXPSP2)。正確配置見我自己的評(píng)論,Tomcat 的日志中沒發(fā)現(xiàn)什么錯(cuò)誤,看上去都很正常,但是測(cè)試程序卻老是提示同樣的錯(cuò)誤:
Error occurred:org.apache.tomcat.dbcp.dbcp.SQLNestedException: Cannot create JDBC driver of class '' for connect URL 'null'
但Driver名和URL都設(shè)置對(duì)了,在Admin的DataSource中也看得到。
Tomcat版本之間變化太大了,請(qǐng)大家?guī)兔纯矗降啄睦镉袉?wèn)題,謝謝!
1.在$CATALINA_HOME/conf/server.xml中添加配置信息,聲明連接池的具體信息,添加內(nèi)容如下:
<!--聲明連接池-->
<Resource name="jdbc/mysql" auth="Container" type="javax.sql.DataSource"/>
<!-- 對(duì)連接池的參數(shù)進(jìn)行設(shè)置 -->
<ResourceParams name="jdbc/mysql">
<parameter>
<name>factory</name>
<value>org.apache.commons.dbcp.BasicDataSourceFactory</value>
</parameter>
<parameter>
<name>maxWait</name>
<value>5000</value>
</parameter>
<parameter>
<name>maxActive</name>
<value>20</value>
</parameter>
<parameter>
<name>username</name>
<value>shopadm</value>
</parameter>
<parameter>
<name>password</name>
<value>123</value>
</parameter>
<parameter>
<name>url</name>
<value>jdbc:mysql://localhost/shopdb?useUnicode=true&charact-erEncoding=gb2312</value>
</parameter>
<parameter>
<name>driverClassName</name>
<value>com.mysql.jdbc.Driver</value>
</parameter>
<parameter>
<name>maxIdle</name>
<value>10</value>
</parameter>
</ResourceParams>
2. 在$CATALINA_HOME/conf/web.xml的</web-app>前添加如下信息:
<resource-ref>
<description>DB Connection</description>
<res-ref-name>jdbc/mysql</res-ref-name>
<res-type>javax.sql.DataSource</res-type>
<res-auth>Container</res-auth>
</resource-ref>
其中<res-ref-name>中的參數(shù)名必須和server.xml中聲明的連接名一樣。
3. 在$CATALINA_HOME/conf/catalina/localhost目錄下找到需要進(jìn)行數(shù)據(jù)庫(kù)連接的當(dāng)前程序的配置信息,比如這里是shopping.xml,在這個(gè)文件中添加如下信息:
<Context …>
…
<ResourceLink name=”jdbc/mysql” global=”jdbc/mysql” type=”javax.sql.DataSource”/>
…
</Context>
大功告成!
在此基礎(chǔ)上,參考Tomcat官方網(wǎng)站的用戶手冊(cè),摸索出另外一種配置連接池的方法,這個(gè)方法不需要對(duì)server.xml進(jìn)行修改,只要對(duì)需要使用到連接池的程序的配置文檔進(jìn)行修改就可以了。方法如下:
1.$CATALINA_HOME/conf/catalina/localhost目錄下找到需要數(shù)據(jù)庫(kù)連接池的程序的配置文檔,此處是 shopping.xml。在<Context> </Context>之間添加如下信息,聲明一個(gè)數(shù)據(jù)庫(kù)連接池:
<Resource name="jdbc/mysql" auth="Container" type="javax.sql.DataSource"/>
<ResourceParams name="jdbc/mysql">
<parameter>
<name>factory</name>
<value>org.apache.commons.dbcp.BasicDataSourceFactory</value>
</parameter>
<parameter>
<name>maxWait</name>
<value>5000</value>
</parameter>
<parameter>
<name>maxActive</name>
<value>20</value>
</parameter>
<parameter>
<name>password</name>
<value>123</value>
</parameter>
<parameter>
<name>url</name>
<value>jdbc:mysql://localhost/shopdb?useUnicode=true&characterEncoding=gb2312</value>
</parameter>
<parameter>
<name>driverClassName</name>
<value>com.mysql.jdbc.Driver</value>
</parameter>
<parameter>
<name>maxIdle</name>
<value>10</value>
</parameter>
<parameter>
<name>username</name>
<value>shopadm</value>
</parameter>
</ResourceParams>
這里的參數(shù)和上一種方法中添加到server.xml里的信息幾乎是完全一樣的。
2.在對(duì)應(yīng)程序的WEB-INF下建立一個(gè)web.xml文檔,添加如下信息:
<?xml version="1.0" encoding="ISO-8859-1"?>
<web-app xmlns="http://java.sun.com/xml/ns/j2ee"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://java.sun.com/xml/ns/j2ee
http://java.sun.com/xml/ns/j2ee/web-app_2_4.xsd"
version="2.4">
<resource-ref>
<description>DB Connection</description>
<res-ref-name>jdbc/mysql</res-ref-name>
<res-type>javax.sql.DataSource</res-type>
<res-auth>Container</res-auth>
</resource-ref>
</web-app>
另外一種方法,與Admin 頁(yè)面配置結(jié)果比較接近,同樣是放在GlobalNamingResources中
1.將數(shù)據(jù)庫(kù)驅(qū)動(dòng)程序的JAR文件放在Tomcat的?common/lib?中;
2.在server.xml中設(shè)置數(shù)據(jù)源,以MySQL數(shù)據(jù)庫(kù)為例,如下:
在<GlobalNamingResources>?</GlobalNamingResources>節(jié)點(diǎn)中加入,
??????<Resource
??????name="jdbc/DBPool"
??????type="javax.sql.DataSource"
??????password="root"
??????driverClassName="com.mysql.jdbc.Driver"
??????maxIdle="2"
??????maxWait="5000"
??????username="root"
??????url="jdbc:mysql://127.0.0.1:3306/test"
??????maxActive="4"/>
???屬性說(shuō)明:name,數(shù)據(jù)源名稱,通常取”jdbc/XXX”的格式;
????????????type,”javax.sql.DataSource”;
????????????password,數(shù)據(jù)庫(kù)用戶密碼;
????????????driveClassName,數(shù)據(jù)庫(kù)驅(qū)動(dòng);
????????????maxIdle,最大空閑數(shù),數(shù)據(jù)庫(kù)連接的最大空閑時(shí)間。超過(guò)空閑時(shí)間,數(shù)據(jù)庫(kù)連
?????????????????????接將被標(biāo)記為不可用,然后被釋放。設(shè)為0表示無(wú)限制。
????????????MaxActive,連接池的最大數(shù)據(jù)庫(kù)連接數(shù)。設(shè)為0表示無(wú)限制。
????????????maxWait?,最大建立連接等待時(shí)間。如果超過(guò)此時(shí)間將接到異常。設(shè)為-1表示
?????????????????????無(wú)限制。
3.在你的web應(yīng)用程序的web.xml中設(shè)置數(shù)據(jù)源參考,如下:
??在<web-app></web-app>節(jié)點(diǎn)中加入,
??<resource-ref>
????<description>MySQL?DB?Connection?Pool</description>
????<res-ref-name>jdbc/DBPool</res-ref-name>
????<res-type>javax.sql.DataSource</res-type>
????<res-auth>Container</res-auth>
????<res-sharing-scope>Shareable</res-sharing-scope>
?</resource-ref>
??子節(jié)點(diǎn)說(shuō)明:?description,描述信息;
???????????????res-ref-name,參考數(shù)據(jù)源名字,同上一步的屬性name;
???????????????res-type,資源類型,”javax.sql.DataSource”;
???????????????res-auth,”Container”;
???????????????res-sharing-scope,”Shareable”;
4.在web應(yīng)用程序的context.xml中設(shè)置數(shù)據(jù)源鏈接,如下:
??在<Context></Context>節(jié)點(diǎn)中加入,
??<ResourceLink
???name="jdbc/DBPool"?
???type="javax.sql.DataSource"?
???global="jdbc/DBPool"/>
???屬性說(shuō)明:name,同第2步和第3步的屬性name值,和子節(jié)點(diǎn)res-ref-name值;
?????????????type,同樣取”javax.sql.DataSource”;
?????????????global,同name值。
?
至此,設(shè)置完成,下面是如何使用數(shù)據(jù)庫(kù)連接池。
1.建立一個(gè)連接池類,DBPool.java,用來(lái)創(chuàng)建連接池,代碼如下:
import?javax.naming.Context;
import?javax.naming.InitialContext;
import?javax.naming.NamingException;
import?javax.sql.DataSource;
public?class?DBPool?{
????private?static?DataSource?pool;
????static?{
?????????Context?env?=?null;
??????????try?{
??????????????env?=?(Context)?new?InitialContext().lookup("java:comp/env");
??????????????pool?=?(DataSource)env.lookup("jdbc/DBPool");
??????????????if(pool==null)?
??????????????????System.err.println("'DBPool'?is?an?unknown?DataSource");
???????????????}?catch(NamingException?ne)?{
??????????????????ne.printStackTrace();
??????????}
??????}
????public?static?DataSource?getPool()?{
????????return?pool;
????}
}
2. 在要用到數(shù)據(jù)庫(kù)操作的類或jsp頁(yè)面中,用DBPool.getPool().getConnection(),獲得一個(gè)Connection對(duì)象,就可 以進(jìn)行數(shù)據(jù)庫(kù)操作,最后別忘了對(duì)Connection對(duì)象調(diào)用close()方法,注意:這里不會(huì)關(guān)閉這個(gè)Connection,而是將這個(gè) Connection放回?cái)?shù)據(jù)庫(kù)連接池。
Error occurred:org.apache.tomcat.dbcp.dbcp.SQLNestedException: Cannot create JDBC driver of class '' for connect URL 'null'
但Driver名和URL都設(shè)置對(duì)了,在Admin的DataSource中也看得到。
Tomcat版本之間變化太大了,請(qǐng)大家?guī)兔纯矗降啄睦镉袉?wèn)題,謝謝!
1.在$CATALINA_HOME/conf/server.xml中添加配置信息,聲明連接池的具體信息,添加內(nèi)容如下:
<!--聲明連接池-->
<Resource name="jdbc/mysql" auth="Container" type="javax.sql.DataSource"/>
<!-- 對(duì)連接池的參數(shù)進(jìn)行設(shè)置 -->
<ResourceParams name="jdbc/mysql">
<parameter>
<name>factory</name>
<value>org.apache.commons.dbcp.BasicDataSourceFactory</value>
</parameter>
<parameter>
<name>maxWait</name>
<value>5000</value>
</parameter>
<parameter>
<name>maxActive</name>
<value>20</value>
</parameter>
<parameter>
<name>username</name>
<value>shopadm</value>
</parameter>
<parameter>
<name>password</name>
<value>123</value>
</parameter>
<parameter>
<name>url</name>
<value>jdbc:mysql://localhost/shopdb?useUnicode=true&charact-erEncoding=gb2312</value>
</parameter>
<parameter>
<name>driverClassName</name>
<value>com.mysql.jdbc.Driver</value>
</parameter>
<parameter>
<name>maxIdle</name>
<value>10</value>
</parameter>
</ResourceParams>
2. 在$CATALINA_HOME/conf/web.xml的</web-app>前添加如下信息:
<resource-ref>
<description>DB Connection</description>
<res-ref-name>jdbc/mysql</res-ref-name>
<res-type>javax.sql.DataSource</res-type>
<res-auth>Container</res-auth>
</resource-ref>
其中<res-ref-name>中的參數(shù)名必須和server.xml中聲明的連接名一樣。
3. 在$CATALINA_HOME/conf/catalina/localhost目錄下找到需要進(jìn)行數(shù)據(jù)庫(kù)連接的當(dāng)前程序的配置信息,比如這里是shopping.xml,在這個(gè)文件中添加如下信息:
<Context …>
…
<ResourceLink name=”jdbc/mysql” global=”jdbc/mysql” type=”javax.sql.DataSource”/>
…
</Context>
大功告成!
在此基礎(chǔ)上,參考Tomcat官方網(wǎng)站的用戶手冊(cè),摸索出另外一種配置連接池的方法,這個(gè)方法不需要對(duì)server.xml進(jìn)行修改,只要對(duì)需要使用到連接池的程序的配置文檔進(jìn)行修改就可以了。方法如下:
1.$CATALINA_HOME/conf/catalina/localhost目錄下找到需要數(shù)據(jù)庫(kù)連接池的程序的配置文檔,此處是 shopping.xml。在<Context> </Context>之間添加如下信息,聲明一個(gè)數(shù)據(jù)庫(kù)連接池:
<Resource name="jdbc/mysql" auth="Container" type="javax.sql.DataSource"/>
<ResourceParams name="jdbc/mysql">
<parameter>
<name>factory</name>
<value>org.apache.commons.dbcp.BasicDataSourceFactory</value>
</parameter>
<parameter>
<name>maxWait</name>
<value>5000</value>
</parameter>
<parameter>
<name>maxActive</name>
<value>20</value>
</parameter>
<parameter>
<name>password</name>
<value>123</value>
</parameter>
<parameter>
<name>url</name>
<value>jdbc:mysql://localhost/shopdb?useUnicode=true&characterEncoding=gb2312</value>
</parameter>
<parameter>
<name>driverClassName</name>
<value>com.mysql.jdbc.Driver</value>
</parameter>
<parameter>
<name>maxIdle</name>
<value>10</value>
</parameter>
<parameter>
<name>username</name>
<value>shopadm</value>
</parameter>
</ResourceParams>
這里的參數(shù)和上一種方法中添加到server.xml里的信息幾乎是完全一樣的。
2.在對(duì)應(yīng)程序的WEB-INF下建立一個(gè)web.xml文檔,添加如下信息:
<?xml version="1.0" encoding="ISO-8859-1"?>
<web-app xmlns="http://java.sun.com/xml/ns/j2ee"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://java.sun.com/xml/ns/j2ee
http://java.sun.com/xml/ns/j2ee/web-app_2_4.xsd"
version="2.4">
<resource-ref>
<description>DB Connection</description>
<res-ref-name>jdbc/mysql</res-ref-name>
<res-type>javax.sql.DataSource</res-type>
<res-auth>Container</res-auth>
</resource-ref>
</web-app>
另外一種方法,與Admin 頁(yè)面配置結(jié)果比較接近,同樣是放在GlobalNamingResources中
1.將數(shù)據(jù)庫(kù)驅(qū)動(dòng)程序的JAR文件放在Tomcat的?common/lib?中;
2.在server.xml中設(shè)置數(shù)據(jù)源,以MySQL數(shù)據(jù)庫(kù)為例,如下:
在<GlobalNamingResources>?</GlobalNamingResources>節(jié)點(diǎn)中加入,
??????<Resource
??????name="jdbc/DBPool"
??????type="javax.sql.DataSource"
??????password="root"
??????driverClassName="com.mysql.jdbc.Driver"
??????maxIdle="2"
??????maxWait="5000"
??????username="root"
??????url="jdbc:mysql://127.0.0.1:3306/test"
??????maxActive="4"/>
???屬性說(shuō)明:name,數(shù)據(jù)源名稱,通常取”jdbc/XXX”的格式;
????????????type,”javax.sql.DataSource”;
????????????password,數(shù)據(jù)庫(kù)用戶密碼;
????????????driveClassName,數(shù)據(jù)庫(kù)驅(qū)動(dòng);
????????????maxIdle,最大空閑數(shù),數(shù)據(jù)庫(kù)連接的最大空閑時(shí)間。超過(guò)空閑時(shí)間,數(shù)據(jù)庫(kù)連
?????????????????????接將被標(biāo)記為不可用,然后被釋放。設(shè)為0表示無(wú)限制。
????????????MaxActive,連接池的最大數(shù)據(jù)庫(kù)連接數(shù)。設(shè)為0表示無(wú)限制。
????????????maxWait?,最大建立連接等待時(shí)間。如果超過(guò)此時(shí)間將接到異常。設(shè)為-1表示
?????????????????????無(wú)限制。
3.在你的web應(yīng)用程序的web.xml中設(shè)置數(shù)據(jù)源參考,如下:
??在<web-app></web-app>節(jié)點(diǎn)中加入,
??<resource-ref>
????<description>MySQL?DB?Connection?Pool</description>
????<res-ref-name>jdbc/DBPool</res-ref-name>
????<res-type>javax.sql.DataSource</res-type>
????<res-auth>Container</res-auth>
????<res-sharing-scope>Shareable</res-sharing-scope>
?</resource-ref>
??子節(jié)點(diǎn)說(shuō)明:?description,描述信息;
???????????????res-ref-name,參考數(shù)據(jù)源名字,同上一步的屬性name;
???????????????res-type,資源類型,”javax.sql.DataSource”;
???????????????res-auth,”Container”;
???????????????res-sharing-scope,”Shareable”;
4.在web應(yīng)用程序的context.xml中設(shè)置數(shù)據(jù)源鏈接,如下:
??在<Context></Context>節(jié)點(diǎn)中加入,
??<ResourceLink
???name="jdbc/DBPool"?
???type="javax.sql.DataSource"?
???global="jdbc/DBPool"/>
???屬性說(shuō)明:name,同第2步和第3步的屬性name值,和子節(jié)點(diǎn)res-ref-name值;
?????????????type,同樣取”javax.sql.DataSource”;
?????????????global,同name值。
?
至此,設(shè)置完成,下面是如何使用數(shù)據(jù)庫(kù)連接池。
1.建立一個(gè)連接池類,DBPool.java,用來(lái)創(chuàng)建連接池,代碼如下:
import?javax.naming.Context;
import?javax.naming.InitialContext;
import?javax.naming.NamingException;
import?javax.sql.DataSource;
public?class?DBPool?{
????private?static?DataSource?pool;
????static?{
?????????Context?env?=?null;
??????????try?{
??????????????env?=?(Context)?new?InitialContext().lookup("java:comp/env");
??????????????pool?=?(DataSource)env.lookup("jdbc/DBPool");
??????????????if(pool==null)?
??????????????????System.err.println("'DBPool'?is?an?unknown?DataSource");
???????????????}?catch(NamingException?ne)?{
??????????????????ne.printStackTrace();
??????????}
??????}
????public?static?DataSource?getPool()?{
????????return?pool;
????}
}
2. 在要用到數(shù)據(jù)庫(kù)操作的類或jsp頁(yè)面中,用DBPool.getPool().getConnection(),獲得一個(gè)Connection對(duì)象,就可 以進(jìn)行數(shù)據(jù)庫(kù)操作,最后別忘了對(duì)Connection對(duì)象調(diào)用close()方法,注意:這里不會(huì)關(guān)閉這個(gè)Connection,而是將這個(gè) Connection放回?cái)?shù)據(jù)庫(kù)連接池。