黑马程序员技术交流社区

标题: JNDI配置问题 [打印本页]

作者: 余宏    时间: 2012-5-18 16:12
标题: JNDI配置问题
Java中的tomcat ,JNDI的配置方式有哪些呢?使用哪种比较好??
作者: 韩国军    时间: 2012-5-18 22:07

tomcat中配置JNDI有全局配置和局部配置。大致的有以下两种配置方式:

第一种:全局配置。
1)在tomcat的conf文件夹下的context.xml配置文件中加入:

[html] view plaincopyprint?<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"/>      
        <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中加入资源引用:

[html] view plaincopyprint?<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>  
<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测试方法:

[java] view plaincopyprint?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());  
  
}  
        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中调用:

[java] view plaincopyprint?TestPageAccessURL test = new TestPageAccessURL();  
test.testJNDI();  
                TestPageAccessURL test = new TestPageAccessURL();
                test.testJNDI();



第二种:局部配置。

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

[html] view plaincopyprint?<?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>  

具体需要那种配置方式,看楼主的需求了
作者: 韩国军    时间: 2012-5-19 20:41
{:soso_e179:}




欢迎光临 黑马程序员技术交流社区 (http://bbs.itheima.com/) 黑马程序员IT技术论坛 X3.2