zoukankan      html  css  js  c++  java
  • tomcat下jndi的三种配置方式

    jndi(Java Naming and Directory Interface,Java命名和目录接口)是一组在Java应用中访问命名和目录服务的API。

    第一种:全局配置。

    1)在tomcat的conf文件夹下的context.xml配置文件中加入:

    <Resource name="jndi/mybatis"   
                auth="Container"   
                type="javax.sql.DataSource"   
                driverClassName="com.mysql.jdbc.Driver"   
                url="jdbc:mysql://localhost:3306/appdb"   
                username="root"   
                password="123456"   
                maxActive="20"   
                maxIdle="10"   
                maxWait="10000"/>    

    2)在项目的web.xml中加入资源引用:

        <resource-ref>  
          <description>JNDI DataSource</description>  
          <res-ref-name>jndi/mybatis</res-ref-name>  
          <res-ref-type>javax.sql.DataSource</res-ref-type>  
          <res-auth>Container</res-auth>  
        </resource-ref>  

    其中res-ref-name值要和context.xml的name值一致。

    3)jndi测试方法:

        public void testJNDI() throws NamingException, SQLException{  
            Context ctx = new InitialContext();  
            DataSource ds = (DataSource) ctx.lookup("java:comp/env/jndi/mybatis");  
            Connection conn = ds.getConnection();  
            System.out.println(conn.isClosed());  
          
        }  

    4)在jsp中调用加载jndi方式,不可以直接用main方法测试,必须通过启动容器从jsp中调用:

        TestPageAccessURL test = new TestPageAccessURL();  
        test.testJNDI();  

    第二种:局部配置(不推荐)。

    1)在tomcat的server.xml的<host>标签内,添加:

        <Context path="/demo_jndi" docBase="/demo_jndi">  
           <Resource  
             name="jndi/mybatis"  
             type="javax.sql.DataSource"  
             driverClassName="com.mysql.jdbc.Driver"  
             maxIdle="2"  
             maxWait="5000"  
             username="root"  
             password="123456"  
             url="jdbc:mysql://localhost:3306/appdb"  
             maxActive="4"/>  
        </Context>  

    其他配置同第一种方式。

    第三种:局部配置。

    1)在项目的META-INFO下面新建context.xml。加入:

        <?xml version="1.0" encoding="UTF-8"?>  
        <Context>  
            <Resource name="jndi/mybatis"   
                        auth="Container"   
                        type="javax.sql.DataSource"   
                        driverClassName="com.mysql.jdbc.Driver"   
                        url="jdbc:mysql://localhost:3306/appdb"   
                        username="root"   
                        password="123456"   
                        maxActive="20"   
                        maxIdle="10"   
                        maxWait="10000"/>      
        </Context>  

    其他配置同第一种方式。

    总结:如果要配置局部的话,推荐使用第三种方式,这样不依赖tomcat了。但是还是推荐使用第一种方式好,虽然依赖tomat,但是是全局的,而且可以配置
    多个。对于以后切换使用方便。
    在项目的web.xml中添加的资源引用可有可无。

    转载自:http://www.cnblogs.com/springmvc3/archive/2011/08/23/2224397.html

  • 相关阅读:
    解决myeclipse2014 中使用低版本的maven插件
    菜鸟成长之路-------使用过滤器实现自动登录
    动态代理
    JSON资料整理
    转账案例中引入事务
    ThreadLocal来管理事务
    【临窗旋墨-leetcode】0001-两数之和-[简单]
    shiro是如何清除过期session的(源码版本shiro1.6)
    [临窗旋墨]javaMelody初始化以及销毁时的处理逻辑及监控日志丢失问题排查
    Eclipse 的 git 插件操作 "代码提交"以及"代码冲突"
  • 原文地址:https://www.cnblogs.com/guoyansi19900907/p/6654836.html
Copyright © 2011-2022 走看看