1

I am doing a Jndi look up using <jee:jndi-lookup> . Everything works fine but as per the Jndi resource reference in Tomcat site , we need the <resource-ref> in Web.xml . I am not able to understand why we need this. Everything works fine if I have a context.xml under the conf folder without the web.xml definitions .

My question is whether <resource-ref> is required only when we lookup Jndi through plain Java code(InitialContext().lookup("...")) and not via Spring .I am not sure if we need this here also.

I saw a discussion on this at What is resource-ref in web.xml used for? but the configuration below

<resource-ref>
  <res-ref-name>jdbc/primaryDB</res-ref-name>
  <jndi-name>jdbc/PrimaryDBInTheContainer</jndi-name>
</resource-ref>

does not seem to work as stated when I use it in the application's web.xml(under WEB-INF) . I think it's specific to JBoss web.xml. Please help me understand .

Community
  • 1
  • 1
Aravind A
  • 9,507
  • 4
  • 36
  • 45

1 Answers1

4

Spring is plain Java code. It's no black magic.

Using a resource-ref is a good practice because it allows using a name that is local to the web app, rather than using a global name, shared by all the webapps in the container.

Suppose that you want to deploy two different versions of the same app in the same container, and that both use the same global JNDI name. They're now forced to use the same database, which is wrong. Whereas if you use a local name, declared in the web.xml file, you can associate each version with it own global name, and thus have both versions use two different databases.

The same goes if you deploy two completely different applications, and both happen to use the same jdbc/OracleDS global name.

JB Nizet
  • 678,734
  • 91
  • 1,224
  • 1,255
  • how the the local name connect to the global name ? The tag `` does not seem to be a child element of `` (Eclipse says so). Putting it in doesn't give any errors but it doesn't match to the global name in context.xml file . The only child elements of `` seem to be res-auth,res-ref-name,res-sharing-scope,res-type and description . – Aravind A Jan 31 '12 at 11:34
  • This is container specific. In Tomcat, it's done using the context.xml file. See http://tomcat.apache.org/tomcat-5.5-doc/jndi-resources-howto.html#JDBC_Data_Sources and http://tomcat.apache.org/tomcat-5.5-doc/config/context.html#Resource%20Links – JB Nizet Jan 31 '12 at 11:42
  • Does it mean that `` is redundant in The app's web.xml while using Tomcat ? – Aravind A Jan 31 '12 at 11:47
  • No it's not. Developers choose a local name for their datasource, and set this name in web.xml resource-ref. The deployer chooses to deploy on Tomcat, and creates or edits the context.xml file with a Resource or a ResourceLink. If he chooses to deploy on JBoss, WebLogic, or any other JEE container, he'll use what the container provides to associate the resource-ref with the actual data source. – JB Nizet Jan 31 '12 at 11:52