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 配置

 

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

 

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

 

Xml代码  收藏代码
  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)测试页面

 

    方式一:

 

Java代码  收藏代码
  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>  

 

   方式二:

 

Java代码  收藏代码
  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. %>  
  19. <h1>Tomcat Container Connection MySqlObjectName:<%=ds %>  
  20. </body>  
  21. </html>  

 

 

   Tomcat 4.x 的配置方法

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

 

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

 

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

 

 

 

   运行结果

 

Html代码  收藏代码
  1. Tomcat Container Connection   
  2. MySqlObjectName:org.apache.tomcat.dbcp.dbcp.BasicDataSource@17f409c   
原文地址:https://www.cnblogs.com/wanghang/p/6299071.html