0

I've been creating java web applications for many years. I'm starting a new project and I thought I'd revisit some processes. I'm using Java 8 and Tomcat 7.

I typically define a JNDI resource entry and pull this out as a DataSource. I define the entry by creating a resource in my context, e.g.:

<Context>
  <Resource name="jdbc/db" .../>
</Context>

This entry contains everything I think I would need including name, driver, and config. Yet all instructions I've seen for setting this up also says you need a resource-ref in your web.xml, like this:

<resource-ref>
  <res-ref-name>jdbc/db</res-ref-name>
  ...
</resource-ref>

See here:

Or any of the other 100s of tutorials you can find online.

However, I accidentally forgot to put in the resource-ref, and yet everything still worked fine (in tomcat). Looking at things, it seems redundant. Then I found this:

But the accepted (and popular) answer didn't seem to really answer the question to me. It said the resource-ref was an abstraction but didn't explain how that abstraction is used. Also, part of the example given was specifically for jboss (the jndi-name) and wouldn't even work in tomcat.

So what am I missing? Is the resource-ref required? Maybe not for tomcat but for other containers? If it is an abstraction, how does that work?

Thanks!

Community
  • 1
  • 1
rjcarr
  • 2,072
  • 2
  • 22
  • 35
  • What part of the accepted answer don't you understand? It already mentions it is not required, but it allows you to map a global JNDI resource name to an application specific resource name. – Mark Rotteveel Feb 23 '17 at 20:04
  • @MarkRotteveel: Where does it say it isn't required? The answer talks about abstractions, but only abstractions for specific containers and not even how to use them. – rjcarr Feb 23 '17 at 20:09
  • The very first sentence does: _"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."_ It then explains the possibility to remap by defining a `resource-ref`. – Mark Rotteveel Feb 23 '17 at 20:11

0 Answers0