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:
- https://tomcat.apache.org/tomcat-7.0-doc/jndi-datasource-examples-howto.html
- https://tomcat.apache.org/tomcat-7.0-doc/jndi-resources-howto.html
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!