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 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>
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?
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.
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");
Any help would be welcome, because good documentation on JNDI is scars.
Tomcat provides a JNDI InitialContext implementation instance for each web application running under it, in a manner that is compatible with those provided by a Java Enterprise Edition application server. The Java EE standard provides a standard set of elements in the /WEB-INF/web.
Individual XML files located in $CATALINA_HOME/conf/[enginename]/[hostname]/ The name of each individual XML file will define a new context path.
context. xml file is the application deployment descriptor for the Apache Tomcat server. In a deployed application, this file is located in the META-INF folder of the web application directory or the WAR file, for example, tomcat/webapps/app-core/META-INF/context.
The Java Naming and Directory Interface™ (JNDI) is an application programming interface (API) that provides naming and directory functionality to applications written using the Java™ programming language. It is defined to be independent of any specific directory service implementation.
BTW,
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.
Only optionally, you can put it in /tomcat6/conf/context.xml, but this will require restart of tomcat after each configuration change.
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"/>
Well, I was revisiting the JNDI territory again and encountered a similar use case. Now I succeeded.
If you need to access plain JNDI variables in a Web application, they should be defined in the element of your web application. But the syntax for defining an environment entry in the context differs from the syntax that is to be used in the web app descriptor (see my original post). This is where my confusion started.
In the context it should be like:
For global variables I still presume {CATALINA_HOME}/conf/context.xml will be the place to put these Environment entries.
See the docs at apache: http://tomcat.apache.org/tomcat-5.5-doc/config/context.html#Environment_Entries
Anyway, thanks for your help!
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With