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)テストページ
 
    方法1:
 
<%@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>

 
   方式2:
 
<%@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