---
title: "TOMCAT数据库连接池的配置方法总结"
author: "gnnan"
date: "2011-09-08T12:02:01Z"
tags: []
categories: ["编程语言"]
slug: "tomcat"
draft: false
lastmod: "2011-09-08T12:03:29Z"
id: 1168377
url: "https://gnnan.iteye.com/blog/1168377"
---

以MySQL+TOMCAT 为例  
1\. 把DataSource 设置到我们的WEB 项目中，下面详细的介绍下：  
第一步：在我们的WEB 项目中的META-INF 文件夹下建立一个context.xml

Xml 代码 [](http://www.javaeye.com/topic/245596 "复制代码")

 

&lt;?xml version='1.0' encoding='utf-8'?&gt;

 

&lt;Context&gt;

 

    &lt;Resource name="jdbc/mysql"  

       auth="Container"  

       type="javax.sql.DataSource"  

       driverClassName="com.mysql.jdbc.Driver"  

       url="jdbc:mysql://localhost/bbs"  

       username="root"  

       password="root"  

       maxActive="50"  

       maxIdle="20"  

       maxWait="10000" /&gt;  

 

&lt;/Context&gt;

第二步：在我们的WEB 项目下的WEB-INF 文件夹下建立一个web.xml ( 如果存在了就不用了，直接修改就行了)  
( 这几天测试了一下，不做这步也可以，O(∩\_∩)O 哈哈~ 省事了)

Xml 代码 [](http://www.javaeye.com/topic/245596 "复制代码")

```
<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>  
```

 

  &lt;resource-ref&gt;

      &lt;description&gt;DB Connection&lt;/description&gt;

      &lt;res-ref-name&gt;jdbc/mysql&lt;/res-ref-name&gt;

      &lt;res-type&gt;javax.sql.DataSource&lt;/res-type&gt;

      &lt;res-auth&gt;Container&lt;/res-auth&gt;

  &lt;/resource-ref&gt;

第三步：我们就可以用代码来获取Connection 对象了

 

 

```
<%@page contentType="text/html;charset=utf-8" %>

<%@ page import ="java.sql.*"%>

<%@ page import = "javax.sql.*"%>

<%@ page import ="javax.naming.*"%>

 

<html>

<head>

<title></title>

</head>

<body>

<%

    DataSource ds = null;

    Context env = null;

    Connection conn = null;

    Statement stmt=null;

    ResultSet rs=null;

    String sql="select * from person";

    try

    {

        System.out.println (System.getProperty("java.naming.factory.initial"));

        env = (Context) new InitialContext().lookup("java:comp/env"); 

        ds = (DataSource) env.lookup("jdbc/mysql");

        conn = ds.getConnection();

        stmt=conn.createStatement();

        rs=stmt.executeQuery(sql);

        while(rs.next())

        {

        System.out.println("ID>>"+rs.getInt("id")+"  name>>"+rs.getString("name")+"  password>>"+rs.getString("password"));

        }

    }

    catch (Exception e)

    {

        e.printStackTrace();

    }

%>

 

 

</body>
```

 

 

 

 

package xushun.util;

 

import java.sql.\*;

import javax.sql.\*;

import javax.naming.\*;

 

public class DBHelper {

   

    public static Connection getConnection() throws SQLException,NamingException

    {

        // 初始化查找命名空间

        Context initContext = new InitialContext();

        Context envContext = (Context)initContext.lookup("java:/comp/env");

        // 找到DataSource

        DataSource ds = (DataSource)envContext.lookup("jdbc/mysql");

        return ds.getConnection();

    }

}

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

Xml 代码 [](http://www.javaeye.com/topic/245596 "复制代码")

```
 <GlobalNamingResources>  
  <!-- Editable user database that can also be used by   
       UserDatabaseRealm to authenticate users   
   -->  
   <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>  
```

 

  &lt;GlobalNamingResources&gt;

    &lt;!-- Editable user database that can also be used by

         UserDatabaseRealm to authenticate users

    --&gt;

    &lt;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" /&gt;

  &lt;/GlobalNamingResources&gt;

修改为

Xml 代码 [](http://www.javaeye.com/topic/245596 "复制代码")

01. **&lt;GlobalNamingResources&gt;**
02. &lt;!-- Editable user database that can also be used by
03. UserDatabaseRealm to authenticate users
04. --**&gt;**
05. **&lt;Resource**  name ="UserDatabase"  auth ="Container"
06. type ="org.apache.catalina.UserDatabase"
07. description ="User database that can be updated and saved"
08. factory ="org.apache.catalina.users.MemoryUserDatabaseFactory"
09. pathname ="conf/tomcat-users.xml"  **/&gt;**
10. **&lt;Resource**  name ="jdbc/bbs"
11. auth ="Container"  type ="javax.sql.DataSource"
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" **/&gt;**
22. **&lt;/GlobalNamingResources&gt;**

  &lt;GlobalNamingResources&gt;

    &lt;!-- Editable user database that can also be used by

         UserDatabaseRealm to authenticate users

    --&gt;

    &lt;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" /&gt;

    &lt;Resource name="jdbc/bbs"    

            auth="Container" type="javax.sql.DataSource"

            driverClassName="com.mysql.jdbc.Driver"

            maxIdle="20"

            maxWait="5000"

            username="root"

            password="admin"

            url="jdbc:mysql://localhost:3306/bbs"    

            maxActive="100"

            removeAbandoned="true"

            removeAbandonedTimeout="60"

            logAbandoned="true"/&gt;

  &lt;/GlobalNamingResources&gt;

第二步：在Tomcat 的conf 文件夹下的context.xml 中加入

Xml 代码 [](http://www.javaeye.com/topic/245596 "复制代码")

1. **&lt;ResourceLink**  name ="jdbc/bbs"  global ="jdbc/bbs"  type ="javax.sql.DataSource" **/&gt;**

&lt;ResourceLink name="jdbc/bbs" global="jdbc/bbs" type="javax.sql.DataSource"/&gt;

第三步：就是在WEB 项目的WEB-INF 中的web.xml 添加

Xml 代码 [](http://www.javaeye.com/topic/245596 "复制代码")

1. **&lt;resource-ref&gt;**
2. **&lt;description&gt;** DB Connection**&lt;/description&gt;**
3. **&lt;res-ref-name&gt;** jdbc/mysql**&lt;/res-ref-name&gt;**
4. **&lt;res-type&gt;** javax.sql.DataSource**&lt;/res-type&gt;**
5. **&lt;res-auth&gt;** Container**&lt;/res-auth&gt;**
6. **&lt;/resource-ref&gt;**

  &lt;resource-ref&gt;

      &lt;description&gt;DB Connection&lt;/description&gt;

      &lt;res-ref-name&gt;jdbc/mysql&lt;/res-ref-name&gt;

      &lt;res-type&gt;javax.sql.DataSource&lt;/res-type&gt;

      &lt;res-auth&gt;Container&lt;/res-auth&gt;

  &lt;/resource-ref&gt;

还有就是在Tomcat 文档中提到的方法，直接修改context.xml 文件了  
在Tomcat 的conf 文件夹下的context.xml 中加入

Xml 代码 [](http://www.javaeye.com/topic/245596 "复制代码")

01. **&lt;Resource**  name ="jdbc/bbs"
02. auth ="Container"  type ="javax.sql.DataSource"
03. driverClassName ="com.mysql.jdbc.Driver"
04. maxIdle ="20"
05. maxWait ="5000"
06. username ="root"
07. password ="admin"
08. url ="jdbc:mysql://localhost:3306/bbs"
09. maxActive ="100"
10. removeAbandoned ="true"
11. removeAbandonedTimeout ="60"
12. logAbandoned ="true" **/&gt;**

&lt;Resource name="jdbc/bbs"    

            auth="Container" type="javax.sql.DataSource"

            driverClassName="com.mysql.jdbc.Driver"

            maxIdle="20"

            maxWait="5000"

            username="root"

            password="admin"

            url="jdbc:mysql://localhost:3306/bbs"    

            maxActive="100"

            removeAbandoned="true"

            removeAbandonedTimeout="60"

            logAbandoned="true"/&gt;

然后就是在WEB 项目的WEB-INF 中的web.xml 添加

Xml 代码 [](http://www.javaeye.com/topic/245596 "复制代码")

1. **&lt;resource-ref&gt;**
2. **&lt;description&gt;** DB Connection**&lt;/description&gt;**
3. **&lt;res-ref-name&gt;** jdbc/mysql**&lt;/res-ref-name&gt;**
4. **&lt;res-type&gt;** javax.sql.DataSource**&lt;/res-type&gt;**
5. **&lt;res-auth&gt;** Container**&lt;/res-auth&gt;**
6. **&lt;/resource-ref&gt;**

  &lt;resource-ref&gt;

      &lt;description&gt;DB Connection&lt;/description&gt;

      &lt;res-ref-name&gt;jdbc/mysql&lt;/res-ref-name&gt;

      &lt;res-type&gt;javax.sql.DataSource&lt;/res-type&gt;

      &lt;res-auth&gt;Container&lt;/res-auth&gt;

  &lt;/resource-ref&gt;

就是这些了，如果有什么不太清楚的就留言，一起研究下。