Tomcat 6中特定于上下文的JNDI参数问题 [英] Context specific JNDI parameters problem in tomcat 6

查看:67
本文介绍了Tomcat 6中特定于上下文的JNDI参数问题的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

当在Tomcat 6上部署了两个或更多应用程序时,我在使用JNDI时遇到了麻烦. 请考虑以下情形: 我有2个Web应用程序,其中每个web.xml都包含一个JNDI参数.

web.xml webapp A:

<env-entry>
<env-entry-name>testEntry</env-entry-name>
<env-entry-value>value A</env-entry-value>
<env-entry-type>java.lang.String</env-entry-type>
</env-entry>

I am having trouble using JNDI when two or more applications are deployed on Tomcat 6. Consider the following scenario: I have 2 webapps, where each web.xml contains one JNDI parameter.

web.xml webapp A:

<env-entry>
<env-entry-name>testEntry</env-entry-name>
<env-entry-value>value A</env-entry-value>
<env-entry-type>java.lang.String</env-entry-type>
</env-entry>

web.xml Web应用程序B:

<env-entry>
<env-entry-name>testEntry</env-entry-name>
<env-entry-value>value B</env-entry-value>
<env-entry-type>java.lang.String</env-entry-type>
</env-entry>

web.xml webapp B:

<env-entry>
<env-entry-name>testEntry</env-entry-name>
<env-entry-value>value B</env-entry-value>
<env-entry-type>java.lang.String</env-entry-type>
</env-entry>

当我部署两个Web应用程序并查找testEntry的值时,两者都返回值A.似乎只有第一个加载的web.xml中的JNDI参数可用. 根据我对JNDI的理解,每个web.xml都包含特定于Webapp的JNDI值,这些值仅在各自的上下文中可用.我在做什么/在这里认为错了?

When I deploy both webapps and lookup the value for testEntry, both webapps return value A. It seems only the JNDI params from the first loaded web.xml are available. Accoring to my understanding of JNDI, each web.xml contains webapp specific JNDI values that are only available in their own respective context. What am I doing/thinking wrong here?

这导致了我的下一个问题.如何定义在所有上下文中都可用的全局JNDI参数?在tomcat文档中,我读到您应该为此目的使用{CATALINA_HOME}/conf/context.xml.但是环境条目在上下文内部不可用.将它们放置在{CATALINA_HOME}/conf/web.xml中可以使它们在全球范围内访问,但是我怀疑这是正确的方法.

This leads to my next question. Howto define global JNDI parameters that are available in all contexts? In the tomcat docs I've read that you should use {CATALINA_HOME}/conf/context.xml for this purpose. But the environment entries are not available inside the contexts. Placing them in {CATALINA_HOME}/conf/web.xml makes them globally accessible, but I doubt this is the correct way.

这是我用来查找它们的Java代码:

Context initCtx = new InitialContext();
Context envCtx = (Context) initCtx.lookup("java:comp/env");
String value = (String)envCtx.lookup("testEntry");

This is my Java code to look them up:

Context initCtx = new InitialContext();
Context envCtx = (Context) initCtx.lookup("java:comp/env");
String value = (String)envCtx.lookup("testEntry");

任何帮助都将受到欢迎,因为关于JNDI的良好文档令人难忘.

Any help would be welcome, because good documentation on JNDI is scars.

推荐答案

BTW,

  1. 您不需要web.xml中的内容.
  2. 您不需要修改/tomcat6/conf/context.xml

您需要做的就是将context.xml放入war内的WebContent目录的META-INF中. JNDI将自动注册,并在应用程序加载时创建池.

All you need is to put context.xml in your META-INF of WebContent directory inside war. The JNDI will be register automatically, and pool will be created on application load.

只能(可选)将其放在/tomcat6/conf/context.xml中,但这需要在每次配置更改后重新启动tomcat.

Only optionally, you can put it in /tomcat6/conf/context.xml, but this will require restart of tomcat after each configuration change.

这是我的sql的一些好的配置.重点是logAbandoned(检测由于容器忘记关闭它而被容器抛出池中的未关闭的连接).对于验证查询,我建议您测试一下数据库表是否存在.

Here is some good configuration for my sql. Important point is logAbandoned (to detect unclosed connections that were thrown out of pool by container, because your code forgot to close it). For validation query i suggest something to test your database table presence.

<Resource name="jdbc/NAME" auth="Container" type="javax.sql.DataSource"
               maxActive="100" minIdle="10" maxWait="10000" removeAbandoned="true"
               removeAbandonedTimeout="60" logAbandoned="true"
               testWhileIdle="true" testOnBorrow="true" testOnReturn="false"
               timeBetweenEvictionRunsMillis="5000"
               validationQuery="SELECT 1" initialSize="10"
               username="usrname" password="password"   
               driverClassName="com.mysql.jdbc.Driver"
               url="jdbc:mysql://localhost:3306/databb?autoReconnect=true"/>

这篇关于Tomcat 6中特定于上下文的JNDI参数问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆