Tomcat配置连接c3p0连接池

一、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 }

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值