1. 程式人生 > >Tomcat配置連接c3p0連接池

Tomcat配置連接c3p0連接池

oid 文件 ext version ges isa scrip Coding trac

一、Tomcat配置JNDI資源

JNDI(Java Naming and Directory Interface),Java 命名和目錄接口。

JNDI的作用就是:在服務器上配置資源,然後通過統一的方式來獲取配置的資源。

我們這裏要配置的資源當然是連接池,這樣項目中就可以通過統一的方式來獲取連接池對象了。

1、導包

  需將這三個jar包置於Tomcat/lib/目錄下:c3p0-0.9.5.2.jar、mchange-commons-java-0.2.11.jar、mysql-connector-java-5.1.44-bin.jar(Driver實現類),此例連接的是MySQL數據庫,如果連接的是oracle還需c3p0-oracle-thin-extras-0.9.5.2.jar。

2、配置context.xml及web.xml文件

 apache-tomcat-9.0.0.M26/conf/context.xml中添加第14到25行內容

 1 <Context reloadable="true">
 2 
 3     <!-- Default set of monitored resources. If one of these changes, the    -->
 4     <!-- web application will be reloaded.                                   -->
 5     <WatchedResource>WEB-INF/web.xml</WatchedResource>
 6     <WatchedResource>WEB-INF/tomcat-web.xml</WatchedResource>
 7     <WatchedResource>${catalina.base}/conf/web.xml</WatchedResource>
 8 
 9     <!-- Uncomment this to disable session persistence across Tomcat restarts -->
10     <!--
11     <Manager pathname="" />
12     -->
13   <!-- 新配置內容 -->
14 <Resource auth="Container"
15     description="DB Connection"
16     driverClass="com.mysql.jdbc.Driver"
17     maxPoolSize="100"
18     minPoolSize="2"
19     acquireIncrement="2"
20     name="jdbc/mysqlds-c3p0"
21     user="root"
22     password=""
23     factory="org.apache.naming.factory.BeanFactory"
24     type="com.mchange.v2.c3p0.ComboPooledDataSource"
25     jdbcUrl="jdbc:mysql://localhost:3306/mydb1" />
26 </Context>

參數說明:

  • name:指定資源名稱,這個名稱可隨便給,在獲取資源時需要這個名稱;
  • factory:用來創建資源的工廠,這個值基本是固定的,不用修改;
  • type:資源的類型,我們要給出的類型是我們連接池的類型。
  • 其他參數為資源的屬性。

  在項目中web/WEB-INF/web.xml 文件中添加配置第6至10行內容

 1 <?xml version="1.0" encoding="UTF-8"?>
 2 <web-app xmlns="http://xmlns.jcp.org/xml/ns/javaee"
 3          xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
 4          xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee http://xmlns.jcp.org/xml/ns/javaee/web-app_3_1.xsd"
 5          version="3.1">
 6     <resource-ref>
 7         <res-ref-name>jdbc/mysqlds-c3p0</res-ref-name>  <!--與context.xml下的Resources的name屬性一致-->
 8         <res-type>javax.sql.DataSource</res-type>
 9         <res-auth>Container</res-auth>
10     </resource-ref>
11 </web-app>

二、獲取資源

 1 package servlet;
 2 
 3 import javax.naming.Context;
 4 import javax.naming.InitialContext;
 5 import javax.naming.NamingException;
 6 import javax.servlet.ServletException;
 7 import javax.servlet.annotation.WebServlet;
 8 import javax.servlet.http.HttpServlet;
 9 import javax.servlet.http.HttpServletRequest;
10 import javax.servlet.http.HttpServletResponse;
11 import javax.sql.DataSource;
12 import java.io.IOException;
13 import java.sql.Connection;
14 import java.sql.SQLException;
15 
16 @WebServlet(name = "AServlet",urlPatterns = "/AServlet")
17 public class AServlet extends HttpServlet {
18     protected void doGet(HttpServletRequest request, HttpServletResponse response)
19             throws ServletException, IOException {
20         //1、創建JNDI的上下文
21         try {
22             Context ctx = new InitialContext();
23             //2、查詢出入口
24 //            Context envCtx = (Context) ctx.lookup("java:comp/env");
25          //3、再進行二次查詢,找到我們的資源
26          //使用的是名稱與<Resource>元素的name對應
27 //            DataSource dataSource = (DataSource) envCtx.lookup("jdbc/mysqlds-c3p0");
28             DataSource dataSource = (DataSource) ctx.lookup("java:comp/env/jdbc/mysqlds-c3p0");
29             Connection con = dataSource.getConnection();
30             System.out.println(con);
31             con.close();
32         } catch (NamingException e) {
33             e.printStackTrace();
34         } catch (SQLException e) {
35             e.printStackTrace();
36         }
37 
38     }
39 }

Tomcat配置連接c3p0連接池