Tomcat configuration DataSource
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 配置
- <Context>
- <!-- Default set of monitored resources -->
- <WatchedResource>WEB-INF/web.xml</WatchedResource>
- <!-- Uncomment this to disable session persistence across Tomcat restarts -->
- <!--
- <Manager pathname="" />
- -->
- <!-- Uncomment this to enable Comet connection tacking (provides events
- on session expiration as well as webapp lifecycle) -->
- <!--
- <Valve className="org.apache.catalina.valves.CometConnectionManagerValve" />
- -->
- <Resource name="jdbc/mysqlDB" auth="Container" type="javax.sql.DataSource"
- maxActive="100" maxIdle="30" maxWait="10000"
- username="root" password="123456" driverClassName="com.mysql.jdbc.Driver"
- url="jdbc:mysql://localhost:3306/test?autoReconnect=true"/>
- </Context>
4) 服务端配置好后 我们就来配置 工程下的web.xml 文件中来告诉Tomcat 容器 我要什么连接。
- <?xml version="1.0" encoding="UTF-8"?>
- <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">
- <display-name>webJNdi</display-name>
- <description>MySQL Test App</description>
- <!-- 告诉 Tomcat Container 我要jdbc/mysqlDb 数据源 -->
- <resource-ref>
- <description>DB Connection</description>
- <res-ref-name>jdbc/mysqlDB</res-ref-name>
- <res-type>javax.sql.DataSource</res-type>
- <res-auth>Container</res-auth>
- </resource-ref>
- <welcome-file-list>
- <welcome-file>index.html</welcome-file>
- <welcome-file>index.htm</welcome-file>
- <welcome-file>index.jsp</welcome-file>
- <welcome-file>default.html</welcome-file>
- <welcome-file>default.htm</welcome-file>
- <welcome-file>default.jsp</welcome-file>
- </welcome-file-list>
- </web-app>
5)测试页面
方式一:
- <%@page import="javax.naming.InitialContext"%>
- <%@page import="javax.naming.Context"%>
- <%@ page language="java" contentType="text/html; charset=UTF-8"
- pageEncoding="UTF-8"%>
- <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
- <html>
- <head>
- <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
- <title>测试Tomcat Container</title>
- </head>
- <body>
- <%
- Context ctx = new InitialContext();
- Context env = (Context)ctx.lookup("java:/comp/env");
- Object ob = env.lookup("jdbc/mysqlDB");
- %>
- <h1>Tomcat Container Connection MySqlObjectName:<%=ob %>
- </body>
- </html>
方式二:
- <%@page import="javax.naming.InitialContext"%>
- <%@page import="javax.naming.Context"%>
- <%@page import="java.sql.*"%>
- <%@page import="javax.sql.*"%>
- <%@ page language="java" contentType="text/html; charset=UTF-8"
- pageEncoding="UTF-8"%>
- <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
- <html>
- <head>
- <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
- <title>测试Tomcat Container</title>
- </head>
- <body>
- <%
- Context ctx = new InitialContext();
- DataSource ds = (DataSource)ctx.lookup( "java:/comp/env/jdbc/mysqlDB" );
- %>
- <h1>Tomcat Container Connection MySqlObjectName:<%=ds %>
- </body>
- </html>
Tomcat 4.x 的配置方法
与上面版本不同配置在server.xml 中添加内容
- <Resource name="jdbc/MysqlDB"
- auth="Container"
- type="javax.sql.DataSource"/>
- <ResourceParams name="jdbc/MysqlDB">
- <parameter>
- <name>factory</name>
- <value>org.apache.commons.dbcp.BasicDataSourceFactory</value>
- </parameter>
- <!-- Maximum number of dB connections in pool. Make sure you
- configure your mysqld max_connections large enough to handle
- all of your db connections. Set to 0 for no limit.
- -->
- <parameter>
- <name>maxActive</name>
- <value>100</value>
- </parameter>
- <!-- Maximum number of idle dB connections to retain in pool.
- Set to 0 for no limit.
- -->
- <parameter>
- <name>maxIdle</name>
- <value>30</value>
- </parameter>
- <!-- Maximum time to wait for a dB connection to become available
- in ms, in this example 10 seconds. An Exception is thrown if
- this timeout is exceeded. Set to -1 to wait indefinitely.
- -->
- <parameter>
- <name>maxWait</name>
- <value>10000</value>
- </parameter>
- <!-- MySQL dB username and password for dB connections -->
- <parameter>
- <name>username</name>
- <value>root</value>
- </parameter>
- <parameter>
- <name>password</name>
- <value>123456</value>
- </parameter>
- <!-- Class name for mm.mysql JDBC driver -->
- <parameter>
- <name>driverClassName</name>
- <value>org.gjt.mm.mysql.Driver</value>
- </parameter>
- <!-- The JDBC connection url for connecting to your MySQL dB.
- The autoReconnect=true argument to the url makes sure that the
- mm.mysql JDBC Driver will automatically reconnect if mysqld closed the
- connection. mysqld by default closes idle connections after 8 hours.
- -->
- <parameter>
- <name>url</name>
- <value>jdbc:mysql://localhost:3306/test?autoReconnect=true</value>
- </parameter>
- </ResourceParams>
同样在web.xml 中指定这里不变
运行结果
- Tomcat Container Connection
- MySqlObjectName:org.apache.tomcat.dbcp.dbcp.BasicDataSource@17f409c
Tomcat configuration DataSource的更多相关文章
- Tomcat Server Configuration Automation Reinforcement
目录 . 引言 . 黑客针对WEB Server会有那些攻击面 . 针对Tomcat Server可以做的安全加固 . Managing Security Realms with JMX . 实现对T ...
- Tomcat数据源(DataSource)简介
JDBC2.0提供了javax.sql.DataSource接口,它负责建立与数据库的连接,在应用程序中访问数据库时不必编写连接数据库的代码,可以直接从数据源获得数据库连接 1.数据库和连接池 在Da ...
- Spring Boot JDBC:加载DataSource过程的源码分析及yml中DataSource的配置
装载至:https://www.cnblogs.com/storml/p/8611388.html Spring Boot实现了自动加载DataSource及相关配置.当然,使用时加上@EnableA ...
- tomcat 部署指南
下载与安装 个人建议不要使用发行版带的版本, 始终从主页来下载安装, 下载地址位于[1], 安装方法很简单, 直接解压即可, 建议解压到 /usr/local/ 目录, 再链接到 /usr/local ...
- CentOS RHEL 安装 Tomcat 7
http://www.davidghedini.com/pg/entry/install_tomcat_7_on_centos This post will cover installing and ...
- Tomcat应用中post方式传参数长度限制
Tomcat应用中post方式传参数长度限制 jsp页面上是没有限制的,但是在tomcat服务器上有限制,Tomcat 默认的post参数的最大大小为2M, 当超过时将会出错,可以配置maxPostS ...
- Redirect HTTP to HTTPS on Tomcat
I have bought my SSL secure certificate and successfully installed on Tomcat with the keytool but ho ...
- Idea 使用maven+tomcat的时候,编译指定的Profile
To build a artifact with a profile you have to create a Maven Run/Debug configuration as in the foll ...
- Tomcat 7.0配置SSL的问题及解决办法
http://dong-shuai22-126-com.iteye.com/blog/1830209 以前一直在用Tomcat 6.0.29版本,今下载了apache-tomcat-7.0.33- ...
随机推荐
- 剑指Offer——知识点储备-J2EE基础
剑指Offer--知识点储备-J2EE基础 9.2 jdk 1.8的新特性(核心是Lambda 表达式) 参考链接:http://www.bubuko.com/infodetail-690646.ht ...
- Android自定义View(二、深入解析自定义属性)
转载请标明出处: http://blog.csdn.net/xmxkf/article/details/51468648 本文出自:[openXu的博客] 目录: 为什么要自定义属性 怎样自定义属性 ...
- Xcode8之后,苹果列出了最新App被拒十大原因
开发者在开发应用程序之前,熟悉苹果审核应用的技术.内容以及设计准则是非常重要的,可以大大降低应用审核被拒的可能性. 最近,苹果通过一个专门的页面给出了截止2016年10月10日应用提交审核被拒的十大原 ...
- lucene查询索引库、分页、过滤、排序、高亮
2.查询索引库 插入测试数据 xx.xx. index. ArticleIndex @Test public void testCreateIndexBatch() throws Exception{ ...
- React Native开发工具Nuclide使用
之前写RN的时候首选webstorm,这是之前做前端已经习惯的工具,其实RN开发官网推荐的是Nuclide工具, Nuclide是Fackbook专门为React开发IDE,今天也来尝试下,如果对we ...
- Android简易实战教程--第八话《短信备份~一》
各种手机助手里面都包含了短信备份这一项.短信的本分主要包含四项:内容body.事件date.方式type.号码address. 短信备份~一.使用一种很笨的方式来保存短信到xml文件中,而且保存在外部 ...
- Android初级教程初谈自定义view自定义属性
有些时候,自己要在布局文件中重复书写大量的代码来定义一个布局.这是最基本的使用,当然要掌握:但是有些场景都去对应的布局里面写对应的属性,就显得很无力.会发现,系统自带的控件无法满足我们的要求,这个时候 ...
- Android Demo---实现从底部弹出窗口
在前面的博文中,小编简单的介绍了如何制作圆角的按钮以及圆角的图片,伴着键盘和手指之间的舞步,迎来新的问题,不知道小伙伴有没有这样的经历,以App为例,点击头像的时候,会从底部弹出一个窗口,有从相册中选 ...
- 对Bitmap的内存优化
在Android应用里,最耗费内存的就是图片资源.而且在Android系统中,读取位图Bitmap时,分给虚拟机中的图片的堆栈大小只有8M,如果超出了,就会出现OutOfMemory异常.所以,对于图 ...
- 动手实现linux中的cp命令(可自行拓展)
我们在学习系统编程的时候,一定会有这样的经历,让你动手实现一个简单的cp命令,也就是拷贝相应的文件到对应的目录,或者说是复制吧,当然,实现非常的简单,我们来看看源码吧: #include <st ...