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- ...
随机推荐
- NuGet包断线续传下载
NuGet包断线续传下载(金庆的专栏)NuGet是VC的扩展,用来下载依赖包.NuGet下载没有断线续传,下载源又很容易断开. https://nuget.org/api/v2/ https:// ...
- 全废话SQL Server统计信息(1)——统计信息简介
当心空无一物,它便无边无涯.树在.山在.大地在.岁月在.我在.你还要怎样更好的世界?--张晓风<我在> 为什么要写这个内容? 随着工作经历的积累,越来越感觉到,大量的关系型数据库的性能问题 ...
- [Python]print vs sys.stdout.write
之前只是在项目中看到过,没怎么注意,正好跟对象一起看python学习手册,看到了这个部分于是来研究下. python版本 2.7.x os win7 print 一般就是执行脚本的时候,把信息直接 ...
- lucene内存索引库、分词器
内存索引库 特点 在内存中开辟一块空间,专门为索引库存放.这样有以下几个特征: 1) 因为索引库在内存中,所以访问速度更快. 2) 在程序退出时,索引库中的文件也相应的消失了. 3) ...
- T-SQL注意事项(1)——SET NOCOUNT ON的去与留
前言 用了一段时间T-SQL之后,哪怕自己没用过,也多多少少看过SSMS中的SET NOCOUNT ON命令,很多性能优化文章中都有提到这个东西,它们建议尽可能使用这个命令减少网络传输的压力,那么今天 ...
- Centos7下Redis3.2的安装配置与JReid测试
环境 Centos7 Redis版本 3.2.0 安装目录 /usr/local/redis/redis-3.2.0 Redis的介绍 参见官网 安装 1 安装gcc与tcl # yum instal ...
- 01基于配置文件方式的SpringMVC,三种HandlerMapping,三种控制器
1 添加Spring MVC所需的jar包. 2 创建一个以下项目结构的springmvc项目 3 web.xml的配置如下: <?xmlversion="1.0"en ...
- Postgre: How to import UUID function into Postgre 9.3
1. Open a command console and go to the directory where you installed Postgre server. e.g. D:\Progra ...
- Linux驱动基础:MSM平台AP/CP通信机制
点击打开链接 概述 MSM平台AP和CP封装到一个芯片,共享内容.所以之前也说过,高通的MSM解决方案中,CP的代码都是由AP放到指定地址的内存中以供CP运行.那上传完代码,CP开始跑之后,AP/CP ...
- 多态原理探究-从C++编译器角度理解多态的实现原理
理论知识: 当类中声明虚函数时,编译器会在类中生成一个虚函数表. 虚函数表是一个存储类成员函数指针的数据结构. 虚函数表是由编译器自动生成与维护的. virtual成员函数会被编译器放入虚函数表中. ...