jndi配置

ndi(Java Naming and Directory Interface,Java命名和目录接口)是一组在Java应用中访问命名和目录服务的API。命名服务将名称和对象联系起来,使得我们可以用名称

访问对象。目录服务是一种命名服务,在这种服务里,对象不但有名称,还有属性。

         tomcat配置jndi有全局配置和局部配置。大致的有以下三种配置方式:

 第一种:全局配置。

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());  
  
} 

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

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,但是是全局的,而且可以配置多个。

本人的一个项目使用的是第一种

<?xml version="1.0" encoding="UTF-8"?>
<!--
  Licensed to the Apache Software Foundation (ASF) under one or more
  contributor license agreements.  See the NOTICE file distributed with
  this work for additional information regarding copyright ownership.
  The ASF licenses this file to You under the Apache License, Version 2.0
  (the "License"); you may not use this file except in compliance with
  the License.  You may obtain a copy of the License at

      http://www.apache.org/licenses/LICENSE-2.0

  Unless required by applicable law or agreed to in writing, software
  distributed under the License is distributed on an "AS IS" BASIS,
  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  See the License for the specific language governing permissions and
  limitations under the License.
--><!-- The contents of this file will be loaded for each web application --><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="gd-datasource" auth="Container" type="com.mchange.v2.c3p0.ComboPooledDataSource"  
    	factory="org.apache.naming.factory.BeanFactory"  
    	driverClass="oracle.jdbc.driver.OracleDriver"  
        jdbcUrl="jdbc:oracle:thin:@172.16.10.27:1521:orcl"  
        user="test"  
        password="test"  
        minPoolSize="5"  
        maxPoolSize="30"  
        maxIdleTime="1800"  
        acquireIncrement="2"  
        maxStatements="0"  
        initialPoolSize="20"  
        idleConnectionTestPeriod="60"  
        acquireRetryAttempts="30"  
        acquireRetryDelay="1000"  
        testConnectionOnCheckin="true"  
        breakAfterAcquireFailure="false"  
		preferredTestQuery="select 1 from dual"
        testConnectionOnCheckout="true"/>
<ResourceLink name="gd-datasource" global="gd-datasource"  type="javax.sql.DataSource"/>
</Context>

 

猜你喜欢

转载自liuna718-163-com.iteye.com/blog/2354213