Aravind A
Aravind A

Reputation: 9697

<resource-ref> usage in Web.xml with Tomcat 5.5 and Spring

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 .

Upvotes: 1

Views: 4756

Answers (1)

JB Nizet
JB Nizet

Reputation: 692003

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.

Upvotes: 4

Related Questions