123

I'm just wondering when/why you would define a <resource-ref> element in your web.xml file?

I would have thought that it would be defined in your web/app server using JNDI and then look up the JNDI reference in your Java code?

The resource-ref definition seems a bit redundant to me and I can't think of when it might be useful. Example:

<resource-ref>
  <description>Primary database</description>
  <res-ref-name>jdbc/primaryDB</res-ref-name>
  <res-type>javax.sql.DataSource</res-type>
  <res-auth>CONTAINER</res-auth>
</resource-ref>
Basil Bourque
  • 303,325
  • 100
  • 852
  • 1,154
JMM
  • 3,922
  • 6
  • 39
  • 46

1 Answers1

162

You can always refer to resources in your application directly by their JNDI name as configured in the container, but if you do so, essentially you are wiring the container-specific name into your code. This has some disadvantages, for example, if you'll ever want to change the name later for some reason, you'll need to update all the references in all your applications, and then rebuild and redeploy them.

<resource-ref> introduces another layer of indirection: you specify the name you want to use in the web.xml, and, depending on the container, provide a binding in a container-specific configuration file.

So here's what happens: let's say you want to lookup the java:comp/env/jdbc/primaryDB name. The container finds that web.xml has a <resource-ref> element for jdbc/primaryDB, so it will look into the container-specific configuration, that contains something similar to the following:

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

Finally, it returns the object registered under the name of jdbc/PrimaryDBInTheContainer.

The idea is that specifying resources in the web.xml has the advantage of separating the developer role from the deployer role. In other words, as a developer, you don't have to know what your required resources are actually called in production, and as the guy deploying the application, you will have a nice list of names to map to real resources.

Joshua Taylor
  • 84,998
  • 9
  • 154
  • 353
candiru
  • 4,424
  • 2
  • 22
  • 16
  • 7
    IMHO, it goes beyond separating the roles. The idea is that you can't assume that a jndi name is available on a server so you need a way to map the name used in your application on the "real" JNDI name chosen by the deployer. – Pascal Thivent May 22 '10 at 16:21
  • 5
    This must be elementary, since I can't find it with Google, but if I want to look up "java:comp/env/jdbc/primaryDB", then why is the res-ref-name "jdbc/primaryDB"? – Torben Aug 23 '12 at 07:29
  • 7
    For what I know, the "jndi-name" element is NOT part of standard web.xml, but rather of specific vendors deployment descriptors. – Ramon Chiara Mar 16 '16 at 17:25
  • `jndi-name` is part of jboss-web.xml but not web.xml. But +1 For Good explaination. [Example](https://docs.jboss.org/jbossas/docs/Server_Configuration_Guide/4/html/ENC_Usage_Conventions-Resource_Manager_Connection_Factory_References_with_jboss.xml_and_jboss_web.xml.html#Resource_Manager_Connection_Factory_References_with_jboss.xml_and_jboss_web.xml-A_sample_jboss_web.xml_resource_ref_descriptor_fragment) – Ravi Parekh Apr 22 '16 at 13:06
  • 2
    @RaviParekh, he has mentioned "container-specific configuration", it might be jboss-web.xml or weblogic.xml or any other vendor specific deployment descriptor file – abhihello123 Aug 01 '16 at 11:48
  • 1
    Can we relate this to tomcat ? I mean datasource defined in context.xml in tomcat and resource-ref in web.xml – Atul Aug 25 '17 at 11:35
  • 2
    If some containers use `jndi-name` to map the indirection but this is not standard, what is the standard ? [Tomcat seems to require both names match](https://tomcat.apache.org/tomcat-9.0-doc/jndi-resources-howto.html#Generic_JavaBean_Resources:~:text=To%20configure%20Tomcat's%20resource%20factory%2C%20add%20an%20element%20like%20this%20to%20the%20%3CContext%3E%20element%20for%20this,in%20the%20web%20application%20deployment%20descriptor) which brings us back to the original question: what's the point? – fabmlk Aug 01 '20 at 08:47
  • 1
    As updating the resource JNDI name happens once in a blue moon, is this overhead worth it? I'd just update the resource name in all the web apps and redeploy them. – Alan Evangelista May 14 '21 at 16:42