tomcat下JNDI的三种配置方式(转载+原创)

        JNDI(Java Naming and Directory Interface,Java命名和目录接口)是一组在Java应用中访问命名和目录服务的API。命名服务将名称和对象联系起来,使得咱们能够用名称访问对象。目录服务是一种命名服务,在这种服务里,对象不但有名称,还有属性。html

        tomcat配置jndi有全局配置和局部配置。大体的有如下三种配置方式:java

 第一种:全局配置。mysql

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

<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中加入资源引用:sql

<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测试方法:apache

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中调用:tomcat

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

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

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

<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中添加的资源引用无关紧要。

 

上面是转载的内容

下面还有另一种配置方式。

第四种:GlobalNamingResources(配置系统的JNDI(Java Naming and Directory Interface))

1)在tomcat的server.xml的<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" />
</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" />
    
  <!--配置mysql数据库的链接池, 
      须要作的额外步骤是将mysql的Java驱动类放到tomcat的lib目录下        
  -->
  <Resource name="jdbc/mysqlpool" 
    auth="Container" 
    type="javax.sql.DataSource" 
    username="root" 
    password="root" 
    maxIdle="30" 
    maxWait="10000" 
    maxActive="100"
    driverClassName="com.mysql.jdbc.Driver"
    url="jdbc:mysql://127.0.0.1:3306/test" />
    
</GlobalNamingResources>

2)在tomcat目录下的conf/context.xml的<Context>节点中添加

<ResourceLink name="jdbc/mysqlpool" global="jdbc/mysqlpool" type="javax.sql.DataSource"/>

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

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

Tomcat数据库链接池的配置方法总结

相关文章
相关标签/搜索