`

tomca中配置jndi数据源的方法

 
阅读更多

准备条件:

1、jdk1.6(或jdk1.7)

2、tomcat6(或tomcat7)

3、数据库连接驱动,以mysql为例:mysql-connector-java-5.1.18.jar

 

配置jndi的步骤:

先来看非全局的配置方法(只有当前应用可以访问该jndi)

1、将数据连接驱动jar包拷贝到tomcat6下的lib目录下;

2、打开tomcat6下的conf目录下context.xml文件;

3、在<Context>......</Context>节点里添加如下代码: 

 

<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" />
    -->
    
    <!--JNDI配置-->
    <Resource name="jdbc/rpepTest"
    	auth="Container"
    	type="javax.sql.DataSource"
    	maxActive="100"
    	maxIdle="30"
    	maxWait="5000"
    	username="testuser"
    	password="testpass"
    	driverClassName="com.mysql.jdbc.Driver"
    	url="jdbc:mysql://10.111.222.333:3306/rpep?zeroDateTimeBehavior=convertToNull"/>

</Context>

 

各参数说明: 

name="jdbc/rpepTest" -------jndi的名称

auth="Container" ------- jndi的认证方式,可选Container或Application

type="javax.sql.DataSource" -------  jndi所使用的类

maxActive="100" ------- 最大活跃的连接数

maxIdle="30" ------- 最大空闲连接数

maxWait="5000" ------- 最大等待时间

username="testuser" ------- 数据库用户名

password="testpass" ------- 数据库密码

driverClassName="com.mysql.jdbc.Driver" ------- 数据库驱动

url="jdbc:mysql://10.111.222.333:3306/rpep?zeroDateTimeBehavior=convertToNull" ------- 数据库连接URL

 

4、在程序中使用jndi:

4.1.新建一个web工程test(注意jndi需要在tomcat容器里使用);

4.2 修改test工程的web.xml,添加如下代码:

 

<!DOCTYPE web-app PUBLIC
 "-//Sun Microsystems, Inc.//DTD Web Application 2.3//EN"
 "http://java.sun.com/dtd/web-app_2_3.dtd" >

<web-app>
  <display-name>Archetype Created Web Application</display-name>
  <resource-ref>
      <description>MySQL DataSource</description>
      <res-ref-name>jdbc/rpepTest</res-ref-name>
      <res-type>javax.sql.DataSource</res-type>
      <res-auth>Container</res-auth>
  </resource-ref>
  
</web-app>
 

 

4.3 编写测试类:

package com.mycom.test;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;

import javax.naming.Context;
import javax.naming.InitialContext;
import javax.naming.NamingException;
import javax.sql.DataSource;

/**
 * 测试JNDI配置和连接
 * 
 * @author guweiqiang
 */
public class TestJNDI {

	private Connection getConnection() throws NamingException {
		Connection conn = null;
		String jndi = "jdbc/rpepTest";
		Context initContext = new InitialContext();
		Context envContext = (Context) initContext.lookup("java:/comp/env");// 固定,不需要修改
		DataSource ds = (DataSource) envContext.lookup(jndi);
		if (ds != null) {
			try {
				conn = ds.getConnection();
			} catch (SQLException e) {
				e.printStackTrace();
			}
		}

		return conn;
	}

	public List<String> selectById(int id) {
		Connection con = null;
		try {
			con = getConnection();
		} catch (NamingException e1) {
			e1.printStackTrace();
		}

		List<String> list = new ArrayList<String>();
		String sql = "select * from rpep_activity where id=?";
		try {
			PreparedStatement pstmt = con.prepareStatement(sql);
			pstmt.setInt(1, id);
			ResultSet rs = pstmt.executeQuery();
			if (rs.next()) {
				list.add(rs.getString(1));
				list.add(rs.getString(2));
				list.add(rs.getString(3));
				System.out.println(rs.getString(1));
				System.out.println(rs.getString(2));
				System.out.println(rs.getString(3));
			}
		} catch (SQLException e) {
			e.printStackTrace();
		}
		return list;
	}

	public static void main(String[] args) {
		new TestJNDI().selectById(100051);
	}

}

 4.4.再编写一个测试jsp:

<%@page import="com.mycom.test.TestJNDI"%> 
<%
	new TestJNDI().selectById(100051);
%>
<html>
<body>
<h2>Hello World!</h2>
</body>
</html>

 

启动tomcat,访问:http://127.0.0.1:8080/test/

测试结果:

100051

5.1测试

5.6测试

 

 

再来看全局的配置方法(所有应用都可以访问该jndi

与非全局的配置基本相同,只是具体的数据源配置信息时配置在conf下的server.xml文件里,然后在context.xml里进行引用。

1. 同样需要将mysql驱动连接的jar拷贝到tomcat的lib目录下;

2. 打开tomcat的conf目录下的server.xml文件,将数据源配置添加到<GlobalNamingResources></GlobalNamingResources>节点下:

 

  <GlobalNamingResources>
    <!-- Editable user database that can also be used by
         UserDatabaseRealm to authenticate users
    -->
    <Resource name="UserDatabase" auth="Container"
              type="org.apache.catalina.UserDatabase"
              description="User database that can be updated and saved"
              factory="org.apache.catalina.users.MemoryUserDatabaseFactory"
              pathname="conf/tomcat-users.xml" />
              
    <!--JNDI配置-->
    <Resource name="jdbc/rpepTest"
    	auth="Container"
    	type="javax.sql.DataSource"
    	maxActive="100"
    	maxIdle="30"
    	maxWait="5000"
    	username="testuser"
    	password="testpass"
    	driverClassName="com.mysql.jdbc.Driver"
    	url="jdbc:mysql://10.111.222.333:3306/rpep?zeroDateTimeBehavior=convertToNull"/>
    	
  </GlobalNamingResources>
 

 

3. 再在context.xml文件里添加全局引用:

 

<ResourceLink global="jdbc/rpepTest" name="jdbc/rpepTest1" type="javax.sql.DataSource"/>
 说明:jdbc/rpepTest是全局的JNDI,jdbc/rpepTest1是你的应用中使用的JNDI。

 

           这一步也可以在应用程序的WebRoot\META-INF\下新建context.xml,然后中增加:

 

<ResourceLink global="jdbc/rpepTest" name="jdbc/rpepTest1" type="javax.sql.DataSource"/>
 以上配置完成之后,无需在web.xml里配置数据源引用了(配置了也没有作用)。

 

 

4. 测试过程同上。

 

 

 

 

 

 

 

 

 

 

 

分享到:
评论

相关推荐

Global site tag (gtag.js) - Google Analytics