views:

61

answers:

1

Hi everyone,

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>

Thanks!

+4  A: 

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 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.

candiru
I don't understand the first part of your first sentence. "java:comp/env" is not a "full JNDI name"; it only refers to resources declared in the WAR. +1 if you fix.
bkail
Fixed. I hope this clarifies it, thank you for your remark.
candiru
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
Good updates, +1.
bkail