1. configuration MySql Connection DataSource

原理介绍

java 调用 Tomcat 中的 ConnectionPool 通过Context 中去查找  jndi 的方式

那么目标就明确了 Java ==jndi==> Tomcat ===> Databases

1) 因为是连接池所以需要 $CATALINA_HOME/lib/tomcat-dbcp.jar 包

     把这个jar 包放到对就的Tomcat 目录下当然一般Tomcat 目录下有。

2) 要连接数据库 所以要jdbc 驱动 $CATALINA_HOME/lib/ mysql-connector-java-5.1.6-bin.jar

注意:tomcat 4.x 放在 $CATALINA_HOME/ common/lib目录下

   将这个jar 包放到Tomcat 目录下同上 OK 在以上物理条件都存在的条

件了。。

 

 3) 配置逻辑条件 $CATALINA_HOME/conf/ context.xml 配置

  1. <Context>
  2. <!-- Default set of monitored resources -->
  3. <WatchedResource>WEB-INF/web.xml</WatchedResource>
  4. <!-- Uncomment this to disable session persistence across Tomcat restarts -->
  5. <!--
  6. <Manager pathname="" />
  7. -->
  8. <!-- Uncomment this to enable Comet connection tacking (provides events
  9. on session expiration as well as webapp lifecycle) -->
  10. <!--
  11. <Valve className="org.apache.catalina.valves.CometConnectionManagerValve" />
  12. -->
  13. <Resource name="jdbc/mysqlDB" auth="Container" type="javax.sql.DataSource"
  14. maxActive="100" maxIdle="30" maxWait="10000"
  15. username="root" password="123456" driverClassName="com.mysql.jdbc.Driver"
  16. url="jdbc:mysql://localhost:3306/test?autoReconnect=true"/>
  17. </Context>

 4) 服务端配置好后 我们就来配置 工程下的web.xml 文件中来告诉Tomcat 容器 我要什么连接。

  1. <?xml version="1.0" encoding="UTF-8"?>
  2. <web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://java.sun.com/xml/ns/javaee" xmlns:web="http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd" id="WebApp_ID" version="2.5">
  3. <display-name>webJNdi</display-name>
  4. <description>MySQL Test App</description>
  5. <!-- 告诉 Tomcat Container 我要jdbc/mysqlDb 数据源 -->
  6. <resource-ref>
  7. <description>DB Connection</description>
  8. <res-ref-name>jdbc/mysqlDB</res-ref-name>
  9. <res-type>javax.sql.DataSource</res-type>
  10. <res-auth>Container</res-auth>
  11. </resource-ref>
  12. <welcome-file-list>
  13. <welcome-file>index.html</welcome-file>
  14. <welcome-file>index.htm</welcome-file>
  15. <welcome-file>index.jsp</welcome-file>
  16. <welcome-file>default.html</welcome-file>
  17. <welcome-file>default.htm</welcome-file>
  18. <welcome-file>default.jsp</welcome-file>
  19. </welcome-file-list>
  20. </web-app>

5)测试页面

方式一:

  1. <%@page import="javax.naming.InitialContext"%>
  2. <%@page import="javax.naming.Context"%>
  3. <%@ page language="java" contentType="text/html; charset=UTF-8"
  4. pageEncoding="UTF-8"%>
  5. <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
  6. <html>
  7. <head>
  8. <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
  9. <title>测试Tomcat Container</title>
  10. </head>
  11. <body>
  12. <%
  13. Context ctx = new InitialContext();
  14. Context env = (Context)ctx.lookup("java:/comp/env");
  15. Object ob = env.lookup("jdbc/mysqlDB");
  16. %>
  17. <h1>Tomcat Container Connection MySqlObjectName:<%=ob %>
  18. </body>
  19. </html>

方式二:

  1. <%@page import="javax.naming.InitialContext"%>
  2. <%@page import="javax.naming.Context"%>
  3. <%@page import="java.sql.*"%>
  4. <%@page import="javax.sql.*"%>
  5. <%@ page language="java" contentType="text/html; charset=UTF-8"
  6. pageEncoding="UTF-8"%>
  7. <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
  8. <html>
  9. <head>
  10. <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
  11. <title>测试Tomcat Container</title>
  12. </head>
  13. <body>
  14. <%
  15. Context ctx = new InitialContext();
  16. DataSource ds = (DataSource)ctx.lookup( "java:/comp/env/jdbc/mysqlDB" );
  17. %>
  18. <h1>Tomcat Container Connection MySqlObjectName:<%=ds %>
  19. </body>
  20. </html>

Tomcat 4.x 的配置方法

   与上面版本不同配置在server.xml 中添加内容 

  1. <Resource name="jdbc/MysqlDB"
  2. auth="Container"
  3. type="javax.sql.DataSource"/>
  4. <ResourceParams name="jdbc/MysqlDB">
  5. <parameter>
  6. <name>factory</name>
  7. <value>org.apache.commons.dbcp.BasicDataSourceFactory</value>
  8. </parameter>
  9. <!-- Maximum number of dB connections in pool. Make sure you
  10. configure your mysqld max_connections large enough to handle
  11. all of your db connections. Set to 0 for no limit.
  12. -->
  13. <parameter>
  14. <name>maxActive</name>
  15. <value>100</value>
  16. </parameter>
  17. <!-- Maximum number of idle dB connections to retain in pool.
  18. Set to 0 for no limit.
  19. -->
  20. <parameter>
  21. <name>maxIdle</name>
  22. <value>30</value>
  23. </parameter>
  24. <!-- Maximum time to wait for a dB connection to become available
  25. in ms, in this example 10 seconds. An Exception is thrown if
  26. this timeout is exceeded.  Set to -1 to wait indefinitely.
  27. -->
  28. <parameter>
  29. <name>maxWait</name>
  30. <value>10000</value>
  31. </parameter>
  32. <!-- MySQL dB username and password for dB connections  -->
  33. <parameter>
  34. <name>username</name>
  35. <value>root</value>
  36. </parameter>
  37. <parameter>
  38. <name>password</name>
  39. <value>123456</value>
  40. </parameter>
  41. <!-- Class name for mm.mysql JDBC driver -->
  42. <parameter>
  43. <name>driverClassName</name>
  44. <value>org.gjt.mm.mysql.Driver</value>
  45. </parameter>
  46. <!-- The JDBC connection url for connecting to your MySQL dB.
  47. The autoReconnect=true argument to the url makes sure that the
  48. mm.mysql JDBC Driver will automatically reconnect if mysqld closed the
  49. connection.  mysqld by default closes idle connections after 8 hours.
  50. -->
  51. <parameter>
  52. <name>url</name>
  53. <value>jdbc:mysql://localhost:3306/test?autoReconnect=true</value>
  54. </parameter>
  55. </ResourceParams>

同样在web.xml 中指定这里不变

运行结果

  1. Tomcat Container Connection
  2. MySqlObjectName:org.apache.tomcat.dbcp.dbcp.BasicDataSource@17f409c

Tomcat configuration DataSource的更多相关文章

  1. Tomcat Server Configuration Automation Reinforcement

    目录 . 引言 . 黑客针对WEB Server会有那些攻击面 . 针对Tomcat Server可以做的安全加固 . Managing Security Realms with JMX . 实现对T ...

  2. Tomcat数据源(DataSource)简介

    JDBC2.0提供了javax.sql.DataSource接口,它负责建立与数据库的连接,在应用程序中访问数据库时不必编写连接数据库的代码,可以直接从数据源获得数据库连接 1.数据库和连接池 在Da ...

  3. Spring Boot JDBC:加载DataSource过程的源码分析及yml中DataSource的配置

    装载至:https://www.cnblogs.com/storml/p/8611388.html Spring Boot实现了自动加载DataSource及相关配置.当然,使用时加上@EnableA ...

  4. tomcat 部署指南

    下载与安装 个人建议不要使用发行版带的版本, 始终从主页来下载安装, 下载地址位于[1], 安装方法很简单, 直接解压即可, 建议解压到 /usr/local/ 目录, 再链接到 /usr/local ...

  5. CentOS RHEL 安装 Tomcat 7

    http://www.davidghedini.com/pg/entry/install_tomcat_7_on_centos This post will cover installing and ...

  6. Tomcat应用中post方式传参数长度限制

    Tomcat应用中post方式传参数长度限制 jsp页面上是没有限制的,但是在tomcat服务器上有限制,Tomcat 默认的post参数的最大大小为2M, 当超过时将会出错,可以配置maxPostS ...

  7. Redirect HTTP to HTTPS on Tomcat

    I have bought my SSL secure certificate and successfully installed on Tomcat with the keytool but ho ...

  8. Idea 使用maven+tomcat的时候,编译指定的Profile

    To build a artifact with a profile you have to create a Maven Run/Debug configuration as in the foll ...

  9. Tomcat 7.0配置SSL的问题及解决办法

    http://dong-shuai22-126-com.iteye.com/blog/1830209   以前一直在用Tomcat 6.0.29版本,今下载了apache-tomcat-7.0.33- ...

随机推荐

  1. CSS3 滤镜学习

    html篇 样式篇 grayscale sepia saturate hue-rotate invert opactiy brightness contrast blur drop-shadow 综合 ...

  2. WmS详解(二)之如何理解Window和窗口的关系?基于Android7.0源码

    上篇博客(WmS详解(一)之token到底是什么?基于Android7.0源码)中我们简要介绍了token的作用,这里涉及到的概念非常多,其中出现频率最高的要数Window和窗口这一对搭档了,那么我们 ...

  3. x264源代码简单分析:宏块编码(Encode)部分

    ===================================================== H.264源代码分析文章列表: [编码 - x264] x264源代码简单分析:概述 x26 ...

  4. C/C++的mem函数和strcpy函数的区别和应用

    mem系列函数是面试的时候常考的知识点,我们需要熟练掌握这三个函数的原理和代码实现,要能准确无误的写出代码. memcpy.memset和memset三个函数在使用过程中,均需包含以下头文件: //在 ...

  5. Linux系统编程-----进程fork()

    在开始之前,我们先来了解一些基本的概念: 1. 程序, 没有在运行的可执行文件 进程, 运行中的程序 2. 进程调度的方法: 按时间片轮转 先来先服务 短时间优先 按优先级别 3. 进程的状态: 就绪 ...

  6. 初探linux子系统集之timer子系统(三)

    因为现在的linux虽然还是可以使用低精度的timer,但是趋势是高精度hrtimer,所以上一篇试着翻译一下hrtimer的一些介绍,翻译的不是很好,看来英语还得好好学习啊,下面还是好好学习下lin ...

  7. Android初级教程获取手机系统联系人信息

    在手机内部,对联系人信息存在对应的数据库.我们创建的而联系人信息都存在这张表中.如下是对数据库的截图,我已经对表和应该注意的地方做了红笔标注: 好了,现在可以根据数据库里面的数据来写代码了. 代码如下 ...

  8. 学习TensorFlow,多层卷积神经网络

    一.网络结构 二.代码 from tensorflow.examples.tutorials.mnist import input_data mnist = input_data.read_data_ ...

  9. androidstudio各版本下载地址

    文件夹如下,含android studio多个版本的下载地址:http://pan.baidu.com/s/1ntmdCPN 目前有以下版本: 0.5.2: windows 0.5.4: linux ...

  10. mysql的left jion:就是left outer join(right join同理)

    左外连接: A left jion B on A.id=B.id 就是A表数据不动,将B表里面能和A对应上的数据补充到A表数据后 而右外连接: rignt jion 则是将A补充到B,B不动,保存全部 ...