About JNDI definition of Jboss/Tomcat/Jetty

  • Jboss 4

      The most common way to configure JNDI in Jboss is when configuring the data source, in a xxx-ds.xml file under the server/default/deploy directory, the content is as follows:

 

[c-sharp]  view plain copy print ?
 
  1. <?xml version="1.0" encoding="UTF-8"?>  
  2. <datasources>  
  3.   <local-tx-datasource>  
  4.       <jndi-name>jdbc/DataSource</jndi-name>  
  5.       <connection-url>jdbc:jtds:sqlserver://localhost:1433/fnx</connection-url>  
  6.       <driver-class>net.sourceforge.jtds.jdbc.Driver</driver-class>  
  7.       <user-name>sa</user-name>  
  8.       <password>1234</password>  
  9.      <metadata>  
  10.      <type-mapping>MS SQLSERVER2000</type-mapping>  
  11.      </metadata>  
  12.      </local-tx-datasource>  
  13. </datasources>  

 

 

In this way, after we start the server, we can directly access the data source in the web application through the following code:

 

[c-sharp]  view plain copy print ?
 
  1. Context ctx = new InitialContext();  
  2. Object o=ctx.lookup("java:jdbc/DataSource");  

 

However, the java:jdbc/DataSource here is a global resource name, and Jboss automatically adds the resource to the global resource. What about the java:comp/env/jdbc/DataSource we often see in applications? This is actually an application

If you want to use this reference name, you need to do two things (must be done): 1. Create jboss-web.xml in the WEB-INF directory of the application, and set the global to local reference name mapping

 

[c-sharp]  view plain copy print ?
 
  1. <jboss-web>  
  2.     <resource-ref>  
  3.         <res-ref-name>jdbc/DataSource</res-ref-name>  
  4.         <res-type>javax.sql.DataSource</res-type>  
  5.         <jndi-name>java:/jdbc/DataSource</jndi-name>  
  6.     </resource-ref>     
  7.  <context-root>/</context-root>  
  8. </jboss-web>  

 

2、在web.xml中设置

 

[c-sharp]  view plain copy print ?
 
  1. <web-app>  
  2.   <display-name>Archetype Created Web Application</display-name>  
  3.     
  4.   <resource-ref>  
  5. <description>dataSource</description>  
  6. <res-ref-name>jdbc/DataSource</res-ref-name>  
  7. <res-type>javax.sql.DataSource </res-type>  
  8. <res-auth>Container</res-auth>  
  9.  </resource-ref>   
  10. </web-app>  

 

这样就可以在web应用中使用 java:comp/env/jdbc/DataSource 私有名了。

  • Tomcat 6

 tomcat中配置JNDI有三个地方:

 1、在服务器的Server元素下配置全局的JNDI<GlobalNamingResources>

 2、在host元素的<DefaultContext>中配置,配置后对该host下所有的context应用都有效

 3、在context元素下配置,这里配置的只对该应用有效。

 

需要注意的是,第一种方式配置的全局JNDI,需要在context中使用<ResourceLink>元素转换成局部名才可以使用,功能跟jboss-web.xml中的转换类似。另外,tomcat 中配置的资源不需要在web.xml中进行声明,也可以通过java:comp/env形式进行访问,这一点跟Jboss/jetty有区别。

  上面三个地方可以配置的资源元素是Resource/Environment,ResourceParams是配合Resource使用,而ResourceLink是为了连接转换全局资源。

举个简单例子,在context元素下的配置:(其他的各位自己去看元素如何使用)

 

[c-sharp]  view plain copy print ?
 
  1. <Resource name="mybase" auth="Container"  
  2.             type="org.apache.catalina.UserDatabase"  
  3.             description="User database that can be updated and saved"  
  4.             factory="org.apache.catalina.users.MemoryUserDatabaseFactory"  
  5.             pathname="conf/tomcat-users.xml" />  

 

这样就可以直接在web中使用 java:comp/env/mybase 来查询到资源了。不过还是建议在web.xml中声明一下。

  • Jetty 7

Jetty的JNDI配置也分3个范围

 1、基于JVM全局的,也就是同一个JVM下不同的Server都可以访问

 2、基于Server的,只能在该server下可以访问

 3、基于应用context上下文的,只能在该context下能够访问

配置方式见我上面一篇《JNDI和在JETTY中的运用》

不过这里要注意的是,采用java:comp/env方式访问时,一定要在web.xml中声明资源(除了配置的EnvEntry元素除外,他自动会加入到私有资源),否则你是拿不到资源的。

 

 

  • 总结

可以看到,每个服务器都有他自己的JNDI配置方式和全局访问方式,如果我们直接采用全局访问方式,第一对于一些只对某些应用有关的资源配置可能访问不到,例如JETTY,第二每个

应用服务器的全局访问方式可能不太一致。因此一般建议在应用程序中采用私有方式(java:comp/env)访问资源,并且无论如何都在web.xml中进行申明。这是一个良好的习惯。

 

 

  Also: Speaking of private environment ENC (java:comp/env), how did he become private? In fact, private is for each application context. Generally, each application context will use its own classloader to load the application, so it can be done by using the classloader. The specific method is:  use each application's own context cloassloader  to initialize a  JNDI context.  And save it with the  classloader  as the  key  , and the application's own context classloader needs to be used to find this JNDI resource when the application retrieves it later  . Because other applications use Different classloader instances cannot be shared with each other, which is private to each application. In addition, if the resource naming method is:  java:resourceName,  or  prefix:resourceName  , although we can directly access it through java:resourceName,  or  prefix:resourceName  , it is also destined that this is not a private resource, and all applications can access it. It needs to be placed under  ENC  to be a private resource. Binding  is accessed under ENC :  java:comp/env/prefix:resourceName 

 

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=326310192&siteId=291194637