Tomcat数据库连接池配置

设置基本上主要有两种方法我们以MySQL+TOMCAT 为例
第一种.把DataSource 设置到我们的WEB 项目中,下面详细的介绍下:
第一步:在我们的WEB 项目中的META-INF 文件夹下建立一个
context.xml
Xml 代码
1. <?xml version='1.0' encoding='utf-8'?>
2.
3. <Context>
4.
5. <Resource name="jdbc/mysql"
6. auth="Container"
7. type="javax.sql.DataSource"
8. driverClassName="com.mysql.jdbc.Driver"
9. url="jdbc:mysql://localhost/bbs"
10. username="root"
11. password="root"
12. maxActive="50"
13. maxIdle="20"
14. maxWait="10000" />
15.
16. </Context>
第二步:在我们的WEB 项目下的WEB-INF 文件夹下建立一个
web.xml(如果存在了就不用了,直接修改就行了)
(这几天测试了一下,不做这步也可以,O(∩_∩)O 哈哈~省事了)
Xml 代码
1. <resource-ref>
2. <description>DB Connection</description>
3. <res-ref-name>jdbc/mysql</res-ref-name>
4. <res-type>javax.sql.DataSource</res-type>
5. <res-auth>Container</res-auth>
6. </resource-ref>

第三步:我们就可以用代码来获取Connection 对象了
Java 代码
1. package xushun.util;
2.
3. import java.sql.*;
4. import javax.sql.*;
5. import javax.naming.*;
6.
7. public class DBHelper {
8.
9. public static Connection getConnection() throws SQ
LException,NamingException
10. {
11. // 初始化查找命名空间
12. Context initContext = new InitialContext()
;
13. Context envContext = (Context)initContext.
lookup("java:/comp/env");
14. // 找到DataSource
15. DataSource ds = (DataSource)envContext.lo
okup("jdbc/mysql");
16. return ds.getConnection();
17. }
18. }

 


第二种.把DataSource 设置到我们的Tomcat 中,下面详细的介绍下(测
试用的JAVA 代码和上面的一样就不帖出了):
这里我查到的设置方法就有了一点区别了。有的人把DataSource 设
置在Tomcat 的server.xml 文件的GlobalNamingResources 下面,
然后在context.xml 中去映射。有的直接就写在context.xml 中了
先说下在server.xml 添加DataSource
第一步:在Tomcat 的conf 中的server.xml 文件中找到
Xml 代码
1. <GlobalNamingResources>

2. <!--
Editable user database that can also be used by
3. UserDatabaseRealm to authenticate users
4. -->
5. <Resource name="UserDatabase" auth="Container"
6. type="org.apache.catalina.UserDatabase"
7. description="User database that can be upd
ated and saved"
8. factory="org.apache.catalina.users.MemoryU
serDatabaseFactory"
9. pathname="conf/tomcat-users.xml" />
10. </GlobalNamingResources>
修改为
Xml 代码
1. <GlobalNamingResources>
2. <!--
Editable user database that can also be used by
3. UserDatabaseRealm to authenticate users
4. -->
5. <Resource name="UserDatabase" auth="Container"
6. type="org.apache.catalina.UserDatabase"
7. description="User database that can be upd
ated and saved"
8. factory="org.apache.catalina.users.MemoryU
serDatabaseFactory"
9. pathname="conf/tomcat-users.xml" />
10. <Resource name="jdbc/bbs"
11. auth="Container" type="javax.sql.DataSou
rce"
12. driverClassName="com.mysql.jdbc.Driver"
13. maxIdle="20"
14. maxWait="5000"
15. username="root"
16. password="admin"

17. url="jdbc:mysql://localhost:3306/bbs"
18. maxActive="100"
19. removeAbandoned="true"
20. removeAbandonedTimeout="60"
21. logAbandoned="true"/>
22. </GlobalNamingResources>
第二步:在Tomcat 的conf 文件夹下的context.xml 中加入
Xml 代码
1. <ResourceLink name="jdbc/bbs" global="jdbc/bbs" type="
javax.sql.DataSource"/>
第三步:就是在WEB 项目的WEB-INF 中的web.xml 添加
Xml 代码
1. <resource-ref>
2. <description>DB Connection</description>
3. <res-ref-name>jdbc/mysql</res-ref-name>
4. <res-type>javax.sql.DataSource</res-type>
5. <res-auth>Container</res-auth>
6. </resource-ref>


还有就是在Tomcat 文档中提到的方法,直接修改context.xml 文件

在Tomcat 的conf 文件夹下的context.xml 中加入
Xml 代码
1. <Resource name="jdbc/bbs"
2. auth="Container" type="javax.sql.DataSou
rce"
3. driverClassName="com.mysql.jdbc.Driver"
4. maxIdle="20"
5. maxWait="5000"
6. username="root"
7. password="admin"
8. url="jdbc:mysql://localhost:3306/bbs"
9. maxActive="100"
10. removeAbandoned="true"
11. removeAbandonedTimeout="60"
12. logAbandoned="true"/>
然后就是在WEB 项目的WEB-INF 中的web.xml 添加
Xml 代码
1. <resource-ref>
2. <description>DB Connection</description>
3. <res-ref-name>jdbc/mysql</res-ref-name>
4. <res-type>javax.sql.DataSource</res-type>
5. <res-auth>Container</res-auth>
6. </resource-ref>

posted @ 2013-04-08 13:55  寂静沙滩  阅读(249)  评论(0)    收藏  举报