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 <!-- 新配置内容 --><br />14 <Resource auth="Container" <br />15 description="DB Connection" <br />16 driverClass="com.mysql.jdbc.Driver" <br />17 maxPoolSize="100" <br />18 minPoolSize="2" <br />19 acquireIncrement="2" <br />20 name="jdbc/mysqlds-c3p0" <br />21 user="root"<br />22 password="" <br />23 factory="org.apache.naming.factory.BeanFactory" <br />24 type="com.mchange.v2.c3p0.ComboPooledDataSource" <br />25 jdbcUrl="jdbc:mysql://localhost:3306/mydb1" /> <br />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<span> servlet;
2
3 import<span> javax.naming.Context;
4 import<span> javax.naming.InitialContext;
5 import<span> javax.naming.NamingException;
6 import<span> javax.servlet.ServletException;
7 import<span> javax.servlet.annotation.WebServlet;
8 import<span> javax.servlet.http.HttpServlet;
9 import<span> javax.servlet.http.HttpServletRequest;
10 import<span> javax.servlet.http.HttpServletResponse;
11 import<span> javax.sql.DataSource;
12 import<span> java.io.IOException;
13 import<span> java.sql.Connection;
14 import<span> java.sql.SQLException;
15
16 @WebServlet(name = "AServlet",urlPatterns = "/AServlet"<span>)
17 public class AServlet extends<span> HttpServlet {
18 protected void<span> doGet(HttpServletRequest request, HttpServletResponse response)
19 throws<span> ServletException, IOException {
20 //1、创建JNDI的上下文
21 try<span> {
22 Context ctx = new<span> 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"<span>);
29 Connection con =<span> dataSource.getConnection();
30 <span> System.out.println(con);
31 <span> con.close();
32 } catch<span> (NamingException e) {
33 <span> e.printStackTrace();
34 } catch<span> (SQLException e) {
35 <span> e.printStackTrace();
36 <span> }
37
38 <span> }
39 } 相关推荐
温攀峰 2020-08-17
幸运小侯子 2020-08-14
dongCSDN 2020-06-28
一恍过去 2020-06-26
qingmuluoyang 2020-06-26
jameszgw 2020-06-25
Rain 2020-06-25
MissFuTT 2020-06-16
标题无所谓 2020-06-14
xclxcl 2020-06-13
onlypersevere 2020-06-13
dongCSDN 2020-06-09
llltaotao 2020-06-03
GavinZhera 2020-06-03
langyue 2020-05-31
牧场SZShepherd 2020-05-27
geek00 2020-05-27
zhaolisha 2020-05-16